cookie usage

Create Cookie

Create a cookie in PHP through the setcookie() function. The syntax format is as follows:

bool setcookie(string name[,string value[,int expire[, string path[,string domain[,int secure]]]]])

Read Cookie

In PHP, you can read the cookie value on the browser side directly through the super global array $_COOKIE[].

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"
  />
 <title>PHP语言基础</title>
 
</head>
<body>
<?php
date_default_timezone_set("PRC");
header("Content-Type:text/html; charset=gb2312");
if (!isset($_COOKIE["visittime"])) {
 setcookie("visittime", date("y-m-d H:i:s"));
 echo "这是第一次保存Cookie"."<br>";
} else {
 setcookie("visittime", date("y-m-d H:i:s"),   time() + 60);
 echo "上次访问时间为:" . $_COOKIE["visittime"];
 echo "<br>";
}
echo "本次访问时间:" . date("y-m-d H:i:s");
?>
</body>
</html>


First run result:
This is the first time to save Cookie
Time of this visit: 16-07- 16 08:26:25

Second running result:
Last visit time: 16-07-16 08:26:25
This time visit time: 16-07-16 08:27:25

The above code first detects whether the Cookie file exists through the isset() function. If it does not exist, create a Cookie file through the setcookie() function. If it exists, set the cookie expiration time to 60 seconds.

Delete Cookie

When a cookie is created, if its expiration time is not set, the cookie file will be automatically deleted when the browser is closed. If you want to delete the Cookie file before closing the browser, there are two methods: one is to delete it using the setcookie() function, and the other is to manually delete the Cookie in the browser. They are introduced separately below.

1. Use the setcookie() function to delete cookies

Deleting cookies is basically similar to creating cookies. The setcookie() function is also used to delete cookies. To delete cookies, you only need to set the second parameter in the setcookie() function to a null value, and set the expiration time of the third parameter Cookie to be less than the current time of the system.

For example, to set the cookie expiration time to the current time minus 1 second, the code is as follows:

setcookie("name", "", time()-1);

In the above code, the time() function returns the current timestamp expressed in seconds. Subtracting 1 second from the expiration time will get the past time, thereby deleting the cookie.

2. Manually delete cookies in the browser

When using cookies, the cookie automatically generates a text file and stores it in the Cookies temporary folder of the IE browser. Deleting cookie files in your browser is a very convenient method.

Cookie life cycle

If the cookie does not set a time, it means that its life cycle is during the browser session. As long as the IE browser is closed, the cookie will automatically disappear. This kind of cookie is called a session cookie and is generally not saved on the hard disk but in memory.

If the expiration time is set, the browser will save the cookie to the hard disk, and it will still be valid when you open the IE browser again until its validity period expires.

Although cookies can be stored in the client browser for a long time, they are not static. Because the browser is allowed to store up to 300 cookie files, and each cookie file supports a maximum capacity of 4KB; each domain name supports a maximum of 20 cookies. If the limit is reached, the browser will automatically delete cookies at random.

What should I do if my browser does not support cookies?

If your application involves 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 way is to pass data from a form (we covered forms and user input earlier in this tutorial).

The following form submits user input to "welcome.php" when the user clicks the submit button:

<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 value in "welcome.php" like this:

<html>
<body>
 
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.
 
</body>
</html>


Continuing Learning
||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>PHP语言基础</title> </head> <body> <?php date_default_timezone_set("PRC"); header("Content-Type:text/html; charset=gb2312"); if (!isset($_COOKIE["visittime"])) { setcookie("visittime", date("y-m-d H:i:s")); echo "这是第一次保存Cookie"."<br>"; } else { setcookie("visittime", date("y-m-d H:i:s"), time() + 60); echo "上次访问时间为:" . $_COOKIE["visittime"]; echo "<br>"; } echo "本次访问时间:" . date("y-m-d H:i:s"); ?> </body> </html>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!