w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP Cookies
A cookie is often used to identify a user.
Cookieͨ³£ÓÃÀ´ÑéÖ¤»ò±æ±ðÒ»¸öÓû§¡£
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests for a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
Cookieͨ³£ÓÃÀ´ÑéÖ¤»ò±æ±ðÒ»¸öÓû§¡£CookieÊÇͨ¹ý·þÎñÆ÷·¢Ë͵½Óû§¼ÆËã»úÖеÄÒ»¸öСÎļþ¡£Ã¿´Î£¬µ±ÏàͬµÄ¼ÆËã»úͨ¹ýä¯ÀÀÆ÷ÇëÇóÒ»¸öÒ³ÃæÊ±£¬ÔÏÈ´æ´¢µÄcookieÒ²»á·¢Ë͵½·þÎñÆ÷¡£Äã¿ÉÒÔʹÓÃPHPÀ´´´½¨ºÍ»ñÈ¡cookieµÄÖµ¡£
The setcookie() function is used to set a cookie.
Setcookie()º¯ÊýÊÇÓÃÀ´ÉèÖÃÒ»¸öcookieµÄ¡£
Note: The setcookie() function must appear BEFORE the <html> tag.
×¢Ò⣺setcookie()º¯Êý±ØÐëÔÚ<html>±êǩ֮ǰ¡£
setcookie(name, value, expire, path, domain); |
In the example below, we will create a cookie named "user" and assign the value "Alex Porter" to it. We also specify that the cookie should expire after one hour:
ÔÚÏÂÃæÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«´´½¨Ò»¸öÃûΪ“user”µÄcookie²¢°Ñ“Alex Porter”Õâ¸öÖµ¸³¸øËü£»Í¬Ê±£¬ÎÒÃÇ»¹¹æ¶¨cookie½«ÔÚһСʱºó¹ýÆÚ£º
<?php
setcookie("user", "Alex Porter", time()+3600);
?>
<html> <body> </body> </html> |
Note: The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).
×¢Ò⣺µ±·¢ËÍcookieʱ£¬cookieÖµÊÇ×Ô¶¯½øÐÐURL±àÂë[URLencoded]µÄ£»µ±Êܵ½cookieʱ£¬×Ô¶¯½øÐÐURL½âÂë¡££¨Èç¹ûÄ㲻ϣÍû½øÐÐURL±àÂ룬¿ÉÒÔʹÓÃsetrawcookie()º¯ÊýÌæ´ú£©
The PHP $_COOKIE variable is used to retrieve a cookie value.
PHP $_COOKIE±äÁ¿ÊÇÓÃÀ´»ñȡһ¸öcookieÖµµÄ
In the example below, we retrieve the value of the cookie named "user" and display it on a page:
ÔÚÏÂÃæÕâ¸öÀý×ÓÖУ¬ÎÒÃÇ»ñÈ¡ÁËÃûΪ“user”µÄÖµ²¢½«ËüÏÔʾÔÚÒ³ÃæÉÏ£º
<?php // A way to view all cookies print_r($_COOKIE); ?> |
In the following example we use the isset() function to find out if a cookie has been set:
ÎÒÃÇʹÓÃisset()º¯ÊýÀ´¼ì²écookieÊÇ·ñ±»ÉèÖãº
<html> <body> <?php if (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br />"; else echo "Welcome guest!<br />"; ?> </body> </html> |
When deleting a cookie you should assure that the expiration date is in the past.
µ±ÄãҪɾ³ýÒ»¸öcookieʱ£¬Äã±ØÐëÈ·±£cookieÒѾ¹ýÆÚ¡£
Delete example:
°¸Àý£º
<?php // set the expiration date to one hour ago |
If your application deals with browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application. One method is to pass the data through forms (forms and user input are described earlier in this tutorial).
Èç¹ûÄãµÄä¯ÀÀÆ÷²»Ö§³Öcookie£¬Äã±ØÐëʹÓÃÆäËû·½·¨½«Ò»¸öÒ³ÃæµÄÐÅÏ¢´«Êäµ½ÁíÒ»Ò³ÃæÖС£ÆäÖÐÒ»¸ö·½·¨¾ÍÊÇʹÓÃ±íµ¥£¨ÎÒÃÇÒѾÔÚÏÈǰµÄ½Ì³ÌÖÐÌáµ½¹ý£©¡£
The form below passes the user input to "welcome.php" when the user clicks on the "Submit" button:
ÏÂÃæÕâ¸ö°¸Àý£ºµ±Óû§µã»÷“Ìá½»submit”°´Å¥µÄʱºò£¬Óû§ÊäÈëµÄÐÅÏ¢±»´«Êäµ½“welcome.php”£º
<html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> |
Retrieve the values in the "welcome.php" file like this:
ÏÂÃæµÄ°¸ÀýÖ¸Ã÷ÁËÈçºÎ»ñÈ¡“welcome.php”ÎļþÖеÄÖµ£º
<html> <body> Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old. </body> </html> |
ÆÀÂÛ (0)
All