Search code examples
phpsuperglobals

get $_GET and $_POST values, but not $_COOKIE values


I would like to use $_REQUEST, but I don't want the value if it came from a cookie. What is the best way to do this? This is how I currently do it. Thanks

$value=isset($_GET['name'])?$_GET['name']:(isset($_POST['name'])?$_POST['name']:NULL);

Solution

  • I would like to use $_REQUEST, but I don't want the value if it came from a cookie.

    Those are two mutually-exclusive statements, I'm afraid. According to the PHP docs, $_REQUEST is:

    An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.

    If you only want to use $_GET and $_POST and explicitly don't want $_COOKIE then you'll have to use them individually.

    In general it's a good idea to abstract infrastructure dependencies anyway, so you can make a function (or, better yet, an object) which gets the values you're looking for. Maybe something as simple as this:

    function getRequestValue($name) {
        if (isset($_GET[$name])) {
            return $_GET[$name];
        }
        if (isset($_POST[$name])) {
            return $_POST[$name];
        }
        // throw an error?  return a default value or null?
    }