0

Is there a way to get a reference to the global arrays that contain HTTP request data like get, post, cookie, etc. ? For example:

$varName = getRequestReference()['varName'];

where getRequestReference() would return &$_GET, &$_POST...depending on the type of request that the user used to submit data in the script.

0

2 Answers 2

3

Sounds like you're after $_REQUEST variable?

Request variable docs

Sign up to request clarification or add additional context in comments.

Comments

0

You can use $_REQUEST to either grab the $_POST, $_GET or $_COOKIE values

See the documentation for more details.

You could also create a custom function that either grabs $_POST or $_GET, depending which has the requested key:

function getRequest($key)
{
    if(!empty($_GET[$key]))
    {
        return $_GET[$name];
    }
    if(!empty($_POST[$key]))
    {
        return $_POST[$name];
    }

    return null;
}

$varValue = getRequest('varName');

This function will return the updated values (when you modify one of these global variables at run-time)

3 Comments

I know about $_REQUEST, however I wanted a reference because if the contents of the get or post (or whatever the request was) are modified at runtime the $_REQUEST will not reflect that change as opposed to a reference.
I have updated my answer, with a solution to keep using $_GET and $_POST, so it will provide updated values (which you seem planning to do). Personally, I would not update these global variables, as it might provide confusion later (eg when you forgot you updated them and expected a different value)
Thanks for that..though it would be useful if php could offer a reference to the global array depending on the type of request.

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.