Yes, it is allowed and may be useful for a number of reasons.
- Debugging If for any reason you want to "force" a specific request parameter, you can set the value in
$_REQUEST , $_GET or $_POST arrays. This will override any value sent by the requesting page, which may be desirable. - Since you are going to do something with the whole array - if you want, for example,
json_encode all the key-value pairs $_REQUEST , as well as some additional values, it might be faster to just “add” the values to $_REQUEST this way and then pass $_REQUEST to json_encode() .
Regarding your question about $_COOKIE , you cannot change the cookie value in this way, only get access to it.
Author's note: The following example was added as a recommended and approved edit to my original answer. Although this may work, there are better ways to protect your site from injection attacks (for example, prepared statements ). IMHO, a reasonable programmer should seriously consider these approaches before relying on the code below.
Consider preventing SQL injection attacks on your site. This simple code will stop them for all $_REQUEST variables (mysqli example):
function injectionwall($dbinterface) { foreach($_REQUEST as $key => $data) { $_REQUEST[$key]=$dbinterface->real_escape_string($data); } }
All $_REQUEST variables $_REQUEST now safe to use :)
source share