Home >Backend Development >PHP Tutorial >PHP cookie settings delete login detailed explanation_PHP tutorial
Cookies are something that are present in all program development. Let me introduce cookie settings (SetCookie) and delete ($_COOKIE) login usage.
1. Set Cookie
PHP uses the SetCookie function to set cookies. One thing that must be noted is that cookies are part of the HTTP protocol header and are used to transfer information between the browser and the server, so the Cookie function must be called before any content belonging to the HTML file itself is output.
The SetCookie function defines a Cookie and appends it to the end of the HTTP header. The prototype of the SetCookie function is as follows:
int SetCookie(string name, string value, int expire, string path, string domain, int secure);
All parameters except name are optional. The three parameters value, path, and domain can be replaced with empty strings, indicating that they are not set; the expire and secure parameters are numerical and can be represented by 0. The expire parameter is a standard Unix time stamp, which can be obtained using the time() or mktime() function, in seconds. The secure parameter indicates whether this cookie is transmitted over the network through the encrypted HTTPS protocol.
The currently set cookie does not take effect immediately, but will not be visible until the next page. This is because the cookie is passed from the server to the client's browser on the set page, and the browser can only save the cookie on the next page. The reason is retrieved from the client's machine and returned to the server.
Setting cookies on the same page is actually from back to front, so if you want to delete a cookie before inserting a new one, you must first write the insertion statement, and then write the deletion statement, otherwise there may be inaccuracies. desired results.
Let’s look at a few examples:
How to create cookies?
The setcookie() function is used to set cookies.
Note: The setcookie() function must be placed before the tag.
Grammar
setcookie(name, value, expire, path, domain);
Simple:
The code is as follows | Copy code | ||||||||
With expiration time: SetCookie(“WithExpire”, “Expire in 1 hour”, time()+3600);//3600 seconds=1 hour Everything is available:
|
The code is as follows | Copy code |
SetCookie(“CookieArray[]“, “Value 1″); SetCookie(“CookieArray[]“, “Value 2″); or SetCookie(“CookieArray[0]“, “Value 1″); SetCookie(“CookieArray[1]“, “Value 2″); |
2. Receive and process Cookies
PHP has very good support for receiving and processing cookies. It is completely automatic and has the same principle as FORM variables. It is very simple.
For example, if you set a cookie named MyCookier, PHP will automatically analyze it from the HTTP header received by the WEB server and form a variable like an ordinary variable named $myCookie. The value of this variable is the cookie. value. The same applies to arrays. Another way is to reference PHP's global variable $HTTP_COOKIE_VARS array.
Examples are as follows: (assuming these have been set in previous pages and are still valid)
The code is as follows | Copy code | ||||
echo count($CookieArray |
代码如下 | 复制代码 |
// Print a cookie // A way to view all cookies |
PHP's $_COOKIE variable is used to retrieve the value of the cookie.
代码如下 | 复制代码 |
if (isset($_COOKIE["user"]))
|
The code is as follows | Copy code | ||||||||||||||||||||
// Print a cookie echo $_COOKIE["user"];
print_r($_COOKIE); ?>In the following example, we use the isset() function to confirm whether the cookie has been set:
For more details, please see: http://www.bKjia.c0m/phper/18/1b5df18d38cfea1a63282c367f6cdf63.htm 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 Previous article:PHP singleton mode study notes_PHP tutorialNext article:PHP singleton mode study notes_PHP tutorial Related articlesSee more |