Question

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);
Was it helpful?

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?
}

OTHER TIPS

Just make a function that does this logic for you:

function fromCookielessRequest($key) {
    if (isset($_GET[$key])) {
        return $_GET[$key];
    }
    if (isset($_POST[$key])) {
        return $_POST[$key];
    }
    return null;
}

$name     = fromCookielessRequest('name');
$password = fromCookielessRequest('password');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top