ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP Cookies

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

PHP
WINDOWSϰ²×°MyS..
PHP ÖÆ×÷ ÍøÕ¾/·þ..
ÓÃPHPºÍCSSÖÆ×÷»î..
PHP µ¥¼þģʽ
PHP MVCģʽ£¬Àà·..
PHP ÖÐʹÓÃÕýÔò±í..
PHP ·ÀÖ¹ SQL ×¢È..
PHP ¿çÕ¾µã½Å±¾¹¥..
PHP ·ÀÖ¹Óû§²Ù×Ý..
PHP ·ÀÖ¹Ô¶³Ì±íµ¥..

PHP Cookies


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:6862 :: ::

A cookie is often used to identify a user.
Cookieͨ³£ÓÃÀ´ÑéÖ¤»ò±æ±ðÒ»¸öÓû§¡£


What is a Cookie?
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µÄÖµ¡£


How to Create a Cookie?
ÔõÑù´´½¨Ò»¸öcookieÄØ£¿

The setcookie() function is used to set a cookie.
Setcookie()º¯ÊýÊÇÓÃÀ´ÉèÖÃÒ»¸öcookieµÄ¡£

Note: The setcookie() function must appear BEFORE the <html> tag.
 ×¢Ò⣺setcookie()º¯Êý±ØÐëÔÚ<html>±êǩ֮ǰ¡£

Syntax
Óï·¨

setcookie(name, value, expire, path, domain);

Example
°¸Àý

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()º¯ÊýÌæ´ú£©


How to Retrieve a Cookie Value?
ÈçºÎ»ñÈ¡cookieÖµ£¿

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
// Print a cookie
echo $_COOKIE["user"];
// 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>


How to Delete a Cookie?
ÈçºÎɾ³ýÒ»¸öcookie£¿

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
setcookie("user", "", time()-3600); ?>


What if a Browser Does NOT Support Cookies?
Èç¹ûä¯ÀÀÆ÷²»Ö§³ÖCookieÔõô°ì£¿

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