I have some simple code like this:
<?php
setcookie("user","test", time() + 3600);
echo $_REQUEST['user']."<br>";
echo $_COOKIE['user'];
?>
and this is the result:
Notice: Undefined index: user in D:\interpub\wwwroot\live\cookie.php on line 3
test
I'm running it on IIS 7.5. I've reloaded the page and I'm sure the browser sends the cookie to the php file (because I have it in $_COOKIE). So why doesn't $_REQUEST
contain that cookie?
A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form. The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag.
Accessing Cookies ValuesThe PHP $_COOKIE superglobal variable is used to retrieve a cookie value. It typically an associative array that contains a list of all the cookies values sent by the browser in the current request, keyed by cookie name.
Right-click and click on Inspect Element to open the developer console. Go to the Storage tab. Expand the Cookies menu and select the website to check cookies. On the right side of the console, you will see the cookies that have been set on the website.
$_REQUEST
on newer PHP setups contains only $_GET
and $_POST
.
With the typical PHP 5.3 php.ini $_COOKIE
is excluded from there by request_order=GP
.
See http://php.net/manual/en/ini.core.php#ini.request-order
And http://php.net/manual/en/ini.core.php#ini.variables-order
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With