-2

I am trying to check that all query requests from a form is filled and not hindered before proceeding with further actions.

I have a code that works already, but i would like to make it an array in other to shorten my code.

My form queries are a,b,c
Below is my current code:

if ( isset($_GET) && !isset($_GET['a']) || !isset($_GET['b']) || !isset($_GET['c']) ) 

{ //Reject call
}else{
//Process call
}

I wish to shorten this code with an array, here is my current code but this isn't working.

$supportedrequests = array('a','b','c')
if (isset($_GET) && !isset($_GET[(in_array($supportedrequests))]) ) {
{ //Reject call
}else{
//Process call
}

Any help will be appreciated.

UPDATE This question is not a duplicate of Using if(!empty) with multiple variables not in an array because it is specifically based on checking isset($_GET) query itself if it exists, and aside that, no answer was fully rendered for the said topic in the stated link.

13
  • Ah, even I need this... It would be nice. But well... Let's see... Commented Jan 2, 2019 at 16:21
  • 3
    isset($_GET) check is redundant, you can be sure that it will be set Commented Jan 2, 2019 at 16:23
  • @ArtemIlchenko I am also rejecting POST requests and at some point serve POST requests with different response hence why that is there. Commented Jan 2, 2019 at 19:49
  • @Praveen - The OP is contesting the hammer close. See the edit. Commented Jan 2, 2019 at 20:18
  • if ( isset($_GET) && !isset($_GET['a']) || !isset($_GET['b']) || !isset($_GET['c']) ) - That whole statement doesn't make much sense here, as noted earlier by Artem. You might want to post the form for this as you say you are using one. At best, you can use a ternary operator, or a switch case. Commented Jan 2, 2019 at 20:20

1 Answer 1

0

If you want check request method GET or POST use this: $_SERVER['REQUEST_METHOD'] == 'POST'

For checking multiple parameters you can declare function and check them all.

function allIsset($params)
{
    foreach ($params as $param) {
        if (!isset($_GET[$param])) return false;
    }
    return true;
}

Or you can use this method (if you want just less lines of code)

$supportedrequests = array('a','b','c');
if (count(array_intersect(array_keys($_GET), $supportedrequests)) < count($supportedrequests)) {
//reject
}
Sign up to request clarification or add additional context in comments.

1 Comment

Second method is exactly what i needed. I just want less code for my already made solution that's all. Thanks alot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.