How do I create cookies?
The Setcookie () function is used to set cookies.
Note: the Setcookie () function must precede the
Create your first PHP cookie
When you create a cookie, use the function Setcookie, you must specify three parameters. These parameters are Setcookie (name, value, expiration):
Name: Names of your cookies. You will use this name to retrieve your cookies later, so don't forget it!
Value: The value stored in your cookie. Common values are user names (strings) and the last Access time (date).
Expires: The cookie expires and is deleted when the date is expired. If you do not set this expiration date, it will be treated as if a session cookie is deleted and the browser is restarted.
In this example, we will create a cookie that stores the user's last visit frequency to measure people's visit to our web pages. We want people to ignore more than two months to take a return to the site, so we will set the cookie's expiration date to two in the coming month!
Copy Code code as follows:
<?php
Calculate in the future
seconds * minutes * hours * days + current time
$inTwoMonths = * * * + time ();
Setcookie (' lastvisit ', date ("G:i-m/d/y"), $inTwoMonths);
?>
If you can't walk in this example there is a date calculation involved. It is important that you know how to set up a cookie by specifying three key parameters: name, value and expiration date.
Retrieve your fresh cookies.
If your cookie has not expired, let us from that user's personal computer be using the appropriate associative array named $ _cookie. The name of your stored cookie is critical and will let you retrieve the value of your stored cookie!
Copy Code code as follows:
<?php
if (Isset ($_cookie[' lastvisit '))
$visit = $_cookie[' lastvisit '];
Else
echo "You ' ve got some stale cookies!";
echo "Your last visit is-". $visit;
?>
Cookies are often used to identify users. Cookies are small files that the server leaves on the user's computer. Whenever the same computer requests a page through a browser, it sends cookies as well. With PHP, you can create and retrieve the value of a cookie.
What if the browser does not support cookies?
If your application involves a browser that does not support cookies, you will have to take other steps to pass information from one page to another in your application. One way is to pass data from a form