[PHP]Check all POST's or GET's.[PHP]

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Hello.

So, I'm going to start a new project and wanted to take some other techniques into hand when creating it.
Now, my project includes a lot of different POST and GET requests.
I was thinking, there must be a better way to check if they're set other than using:-
PHP:
if(isset($_POST['whatever']))
{
    //Do something
}

So, my question is, is there a better way?
It'd be nice to have something like an Array like...
PHP:
$requests = array("upload", "download", "photo", "video");
and just be able to check if the POST (or GET) contains either of those.

Thanks in advance.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Something like this, I guess.

PHP:
<?php
$reqs = array("upload", "download", "photo", "video");
 
foreach ($reqs as $v) {
    if (isset($_POST[$v])) {
        //do something
    } else {
        //nothing is set
    }
}
?>
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Yeah, but this will lead to the same action being called for each of those, right?
I got something like this, but it doesn't call the function because it's in quotes, however, if I remove the quotes, the function is called as the array is being built.

PHP:
<?php
    $get = $_GET;
    function hi()
    {
        echo "hi";
    }
    $req = array("login" => "hi();");
    foreach($req as $r=>$k)
    {
        if(array_key_exists($r, $get))
        {
            echo $k;
        }
    }
    print_r($get);
?>
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
...then something like this

PHP:
<?php
$reqs = array("upload", "download", "photo", "video");
 
foreach ($reqs as $v) {
    switch ($_POST[$v]) {
        case "upload":
            //upload selected
            break;
       
        case "download":
            //download selected
            break;
           
        /* ..etc*/
       
        default:
            //nothing selected
            break;
    }
}
?>
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
I'm really just looking for a more efficient way of doing this, rather than having a huge file filled with the different if statements, cases or whatever.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
I'm really just looking for a more efficient way of doing this, rather than having a huge file filled with the different if statements, cases or whatever.

Well how else are you going to do it? You need to execute a code to check what has been selected (upload,download,photo,video etc) or else it just won't work, especially if you're having a different action happen for each case.
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
I guess so. Hmph. Is there no way of getting the way I put in my edited post to work?
Like... by getting the function to execute when the corresponding key is called and not when the function is built?
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
I guess so. Hmph. Is there no way of getting the way I put in my edited post to work?
Like... by getting the function to execute when the corresponding key is called and not when the function is built?

From what I know, that isn't possible. You'd most likely have to do something like this.

PHP:
<?php
$reqs = array("upload", "download", "photo", "video");
 
function hi() { echo 'hi'; }
 
foreach ($reqs as $v) {
    switch ($_POST[$v]) {
        case "upload":
            hi();
            break;
       
        case "download":
            //download selected
            break;
           
        /* ..etc*/
       
        default:
            //nothing selected
            break;
    }
}
?>
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Nevermind.

I got it to work, cheers though.

Working code:
PHP:
<?php
    $get = $_GET;
    function hi()
    {
        echo "hi";
    }
    $req = array("login" => "hi");
    foreach($req as $r=>$k)
    {
        if(array_key_exists($r, $get))
        {
            call_user_func($req[$r]);
        }
    }
?>
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
You don't need to use array_key_exists It will exist, if it in there, but check if it is a callable (function). This way you can have anonymous functions in your array.

Note: This is not the proper way of making a router, also consider using $_REQUEST instead.
PHP:
$requests = array(
    'post.login' => function(e) {
        // Do some login here, probably have e return POST fields
    },
 
    'post.upload' => function(e) {
        // Upload something, e could return data
    },
 
    'get.register' => register()
 
);
 
foreach($requests as $request => $callback) {
    $data = null;
    list(strtolower($type), $req) = explode($request, '.');
 
    if($type === 'get') {
        $data = $_GET;
    } else if ($type === 'post') {
        $data = $_POST;
    } else {
        $data = null; // Not a type
    }
    // Here you could also check what kind of request, if it is a GET, POST or other
    if(is_callable($callback)) {
        $callback($data); // You can pass as many arguments you want.
    }
}

!!! However I encourage you (and everyone) to use a real router like or similar.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
You don't need to use array_key_exists It will exist, if it in there, but check if it is a callable (function). This way you can have anonymous functions in your array.

Note: This is not the proper way of making a router, also consider using $_REQUEST instead.
PHP:
$requests = array(
    'post.login' => function(e) {
        // Do some login here, probably have e return POST fields
    },

    'post.upload' => function(e) {
        // Upload something, e could return data
    },

    'get.register' => register()

);

foreach($requests as $request => $callback) {
    $data = null;
    list(strtolower($type), $req) = explode($request, '.');

    if($type === 'get') {
        $data = $_GET;
    } else if ($type === 'post') {
        $data = $_POST;
    } else {
        $data = null; // Not a type
    }
    // Here you could also check what kind of request, if it is a GET, POST or other
    if(is_callable($callback)) {
        $callback($data); // You can pass as many arguments you want.
    }
}

!!! However I encourage you (and everyone) to use a real router like or similar.
Your if statement for $type only needs == not === or it will just fall through and set $data to null.
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
No, because I'm looking for the array key... not the value.
Still, that would require lots of if(in_array()){} statements.
 

Users who are viewing this thread

Top