Home  >  Article  >  Backend Development  >  Understanding and using PHP Cookie related knowledge

Understanding and using PHP Cookie related knowledge

jacklove
jackloveOriginal
2018-05-07 12:00:251446browse

PHP Cookie plays an important role in PHP caching. This article provides some detailed explanations of its related knowledge.

What are Cookies?

Cookies are often used to identify users. A cookie is a small file that a server leaves on a user's computer. Each time the same computer requests a page through the browser, the cookie will be sent to the computer. With PHP, you can create and retrieve cookie values.

How to create a cookie?

The setcookie() function is used to set cookies.

Note: The setcookie() function must be located before the 100db36a723c770d327fc0aef2ce13b1 tag.

Syntax

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

Example 1

In the following example, we will create a cookie named "user" and assign it the value "runoob". We also specify that this cookie expires after one hour:

<?php
setcookie("user", "runoob", time()+3600);?><html>.....

Note: The cookie value is automatically URL-encoded when sending the cookie and automatically decoded when retrieved. (To prevent URL encoding, use setrawcookie() instead.)

Example 2

You can also set the cookie expiration time in another way. This may be simpler than using seconds.

<?php
$expire=time()+60*60*24*30;setcookie("user", "runoob", $expire);?><html>.....

In the above example, the expiration time is set to one month (60 seconds * 60 minutes * 24 hours * 30 days).

How to retrieve the value of Cookie?

PHP’s $_COOKIE variable is used to retrieve the value of the cookie.

In the following example, we retrieve the value of the cookie named "user" and display it on the page:

<?php// 输出 cookie 值echo $_COOKIE["user"];// 查看所有 cookieprint_r($_COOKIE);?>

In the following example, we use isset() function to confirm whether the cookie has been set:

<html><head><meta charset="utf-8"><title>菜鸟教程(runoob.com)</title></head><body><?phpif (isset($_COOKIE["user"]))    echo "欢迎 " . $_COOKIE["user"] . "!<br>";else    echo "普通访客!<br>";?></body></html>

How todelete Cookie?

When deleting a cookie, you should change the expiration date to a point in time in the past.

Deleted example:

8c580e3a110b04970f479d507da46b72

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

If your application needs to deal with browsers that do not support cookies, then you will have to use other methods to pass information between pages in your application. One way is to pass data through a form (forms and user input are covered in the previous chapters of this tutorial).

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

<html><head><meta charset="utf-8"><title>菜鸟教程(runoob.com)</title></head><body><form action="welcome.php" method="post">名字: <input type="text" name="name">年龄: <input type="text" name="age"><input type="submit"></form></body></html>

Retrieve the value in the "welcome.php" file, as follows Shown:

<html><head><meta charset="utf-8"><title>菜鸟教程(runoob.com)</title></head><body>欢迎 <?php echo $_POST["name"]; ?>.<br>你 <?php echo $_POST["age"]; ?> 岁了。</body></html>

This article provides a detailed understanding of Cookie-related knowledge. For more learning materials, please pay attention to the php Chinese website to view.

Related recommendations:

PHP Session’s understanding and application of cache-related knowledge

About PHP array sorting Knowledge application

#Related knowledge and application of PHP file upload

The above is the detailed content of Understanding and using PHP Cookie related knowledge. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn