How do I create a cookie?
The Setcookie () function is used to set cookies.
Note: the Setcookie () function must be located in theLabel before.
Create your first PHP cookie
When you create a cookie that uses a function Setcookie, you must specify three parameters. These parameters are Setcookie (name, value, expiration):
Name: The names of your cookies. You will use this name to retrieve your cookie later, so don't forget it!
Value: The value stored in your cookie. Common values are the user name (string) and the last Access time (date).
Expiry: The date when the cookie expires and is deleted. If you do not set this expiration date, then 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 access frequency to measure people's visit to our web page. We want people to ignore more than two months to take back to the site, so we will set the cookie expiration date to two months in the future!
Copy CodeThe code is as follows:
Calculate 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 the three important parameters: name, value and expiration date.
Retrieve your Fresh Cookies
If your cookie is not yet expired, let us use the appropriate associative array name from the user's personal computer in _cookie. The name of your stored cookie is key and will allow you to retrieve the value of your stored cookie!
Copy CodeThe code is as follows:
if (Isset ($_cookie[' lastvisit '))
$visit = $_cookie[' lastvisit ');
Else
echo "You ' ve got some stale cookies!";
echo "Your last visit was-". $visit;
?>
Cookies are often used to identify users. A cookie is a small file that the server leaves on the user's computer. Whenever the same computer requests a page through a browser, it also sends a cookie. 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 a different approach to passing information from one page to another in your application. One way is to pass data from a form
http://www.bkjia.com/PHPjc/321801.html www.bkjia.com true http://www.bkjia.com/PHPjc/321801.html techarticle How do I create a cookie? The Setcookie () function is used to set cookies. Note: the Setcookie () function must precede the HTML tag. Create your first PHP cookie when you create a cookie, ...