Home >Backend Development >PHP Tutorial >32 PHP Cookies
Cookies are often used to identify users.
What are Cookies?
Cookies are often used to identify users. Cookies are small files that a server leaves on a user's computer. Whenever the same computer requests a page through the browser, it also sends the cookie. With PHP, you can create and retrieve cookie values.
How to create cookies?
The setcookie() function is used to set cookies.
Note: The setcookie() function must be placed before the < html> tag.
Grammar
<code>setcookie(name, <span>value</span>, expire, path, domain);</code>
Examples
In the following example, we will create a cookie named "user" and assign it the value "Alex Porter". We also specify that this cookie expires after one hour:
<code><span><span><?php</span>setcookie(<span>"user"</span>, <span>"Alex Porter"</span>, time()+<span>3600</span>); <span>?></span></span><span><<span>html</span>></span><span><<span>body</span>></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
Note: When sending a cookie, the cookie value will be automatically URL-encoded and automatically decoded when retrieved (to prevent URL encoding, please use setrawcookie() instead )
How to retrieve the value of Cookie?
PHP's $_COOKIE variable is used to retrieve the value of the cookie.
In the example below, we retrieve the value of the cookie named "user" and display it on the page:
<code><span><span><?php</span><span>// Print a cookie</span><span>echo</span><span>$_COOKIE</span>[<span>"user"</span>]; <span>// A way to view all cookies</span> print_r(<span>$_COOKIE</span>); <span>?></span></span></code>
In the example below, we use the isset() function to confirm whether it has been set Cookies:
<code><span><span><<span>html</span>></span><span><<span>body</span>></span><span><span><?php</span><span>if</span> (<span>isset</span>(<span>$_COOKIE</span>[<span>"user"</span>])) <span>echo</span><span>"Welcome "</span> . <span>$_COOKIE</span>[<span>"user"</span>] . <span>"!<br />"</span>; <span>else</span><span>echo</span><span>"Welcome guest!<br />"</span>; <span>?></span></span><span></<span>body</span>></span><span></<span>html</span>></span></span></code>
How to delete cookies?
When deleting a cookie, you should change the expiry date to a point in time in the past.
Example of deletion:
<code><span><span><?php</span><span>// set the expiration date to one hour ago</span> setcookie(<span>"user"</span>, <span>""</span>, time()-<span>3600</span>); <span>?></span></span></code>
What should I do if my browser does not support cookies?
If your application involves browsers that don't support cookies, you'll have to use other methods to pass information from one page to another within your application. One way is to pass data from a form (we've covered forms and user input earlier in this tutorial).
The form below submits user input to "welcome.php" when the user clicks the submit button:
<code><span><<span>html</span>></span><span><<span>body</span>></span><span><<span>form</span><span>action</span>=<span>"welcome.php"</span><span>method</span>=<span>"post"</span>></span> Name: <span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"name"</span> /></span> Age: <span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"age"</span> /></span><span><<span>input</span><span>type</span>=<span>"submit"</span> /></span><span></<span>form</span>></span><span></<span>body</span>></span><span></<span>html</span>></span> 取回 "welcome.php" 中的值,就像这样: <span><<span>html</span>></span><span><<span>body</span>></span>Welcome <span><span><?php</span><span>echo</span><span>$_POST</span>[<span>"name"</span>]; <span>?></span></span>.<span><<span>br</span> /></span> You are <span><span><?php</span><span>echo</span><span>$_POST</span>[<span>"age"</span>]; <span>?></span></span> years old. <span></<span>body</span>></span><span></<span>html</span>></span></code>
The above has introduced 32 PHP Cookies, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.