-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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Julius
  • 387
  • 6
  • 15
  • Ah, even I need this... It would be nice. But well... Let's see... – Praveen Kumar Purushothaman Jan 02 '19 at 16:21
  • 3
    `isset($_GET)` check is redundant, you can be sure that it will be set – Artem Ilchenko Jan 02 '19 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. – Julius Jan 02 '19 at 19:49
  • @Praveen - The OP is contesting the hammer close. See the edit. – Funk Forty Niner Jan 02 '19 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](https://stackoverflow.com/questions/54009697/how-to-use-array-to-determine-empty-get-query-string-in-php#comment94856187_54009697). 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. – Funk Forty Niner Jan 02 '19 at 20:20
  • `if (isset($_GET) && !isset($_GET[(in_array($supportedrequests))]) )` - Pretty sure you want `OR - ||` and not `AND - &&` but I'd get rid of the first one and check if it's in the array and/or not empty. – Funk Forty Niner Jan 02 '19 at 20:22
  • @FunkFortyNiner The statement checks that 1. It is a GET request 2. All queries are present i.e a, b, c. otherwise it rejects the call. This simply means that, if the form action is changed to POST, it won't process either. – Julius Jan 02 '19 at 20:27
  • `if (isset($_GET)` - Again, don't use that and it could fail for a quite a few reasons and I can't go into detail, it would take too long. – Funk Forty Niner Jan 02 '19 at 20:29
  • @FunkFortyNiner Do you think this question needs to be opened? AFAIK, looks like a dupe again or can be solved from the comments. – Praveen Kumar Purushothaman Jan 02 '19 at 20:48
  • @Praveen I can't make that call and rather leave it to your discretion, TBH. I feel the question can't be answered and the duplicate seems to answer it, probably to a certain extent. – Funk Forty Niner Jan 02 '19 at 20:50
  • @Julius ^ What do you say for the above explanation by Funk? – Praveen Kumar Purushothaman Jan 02 '19 at 20:50
  • @Praveen Like i said, the stated duplicate isn’t related to my issue and didn’t answer my question. I will stick with my own code if no one can offer a shorter code i was asking for. – Julius Jan 02 '19 at 21:36
  • @Julius I have reopened your question coz of your determination... `:D` All the best and happy new year. – Praveen Kumar Purushothaman Jan 02 '19 at 21:55

1 Answers1

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
}
Artem Ilchenko
  • 1,050
  • 9
  • 20
  • Second method is exactly what i needed. I just want less code for my already made solution that's all. Thanks alot. – Julius Jan 03 '19 at 12:16