This article describes how to set, use, and delete cookies in PHP, as well as some restrictions on cookies. For more information, see. 1,Set CookieSet the cookie in php and use the SetCookie function. Note: Cookie is part of the HTTP header and is used to transmit information between the browser and the server. Therefore, you must call the Cookie function before outputting any HTML file content. The SetCookie function defines a Cookie and attaches 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 value, path, and domain parameters can be replaced by an empty string, indicating that they are not set. The expire and secure parameters are numeric and can be expressed as 0. The expire parameter is a standard Unix time mark, which can be obtained using the time () or mktime () function, in seconds. The secure parameter indicates whether the Cookie is transmitted over the network through the encrypted HTTPS protocol. The Cookie currently set does not take effect immediately, but will not be visible until the next page. this is because the Cookie is transmitted from the server to the client's browser on the configured page, and the Cookie can be retrieved from the client's machine and sent back to the server on the next page. Setting a Cookie on the same page actually goes from the back to the back. if you want to delete a Cookie before inserting it, you must first write the insert statement and then write the delete statement, otherwise, unexpected results may appear. Let's take a look at several examples of setting a Cookie using the SetCookie () function. -- Simple: SetCookie ("MyCookie", "Value of MyCookie "); -- SetCookie ("WithExpire", "Expire in 1 hour", time () + 3600); // 3600 seconds = 1 hour -- Comprehensive: SetCookie ("FullCookie", "Full cookie value", time () + 3600, "/forum", ".jbxue.com", 1 ); If the site has several different directories and only uses cookies without paths, the Cookie set on the page under one directory cannot be seen on the page of another directory. Cookie is path-oriented. Even if no path is specified, the WEB server will automatically pass the current path to the browser, and the specified path will force the server to use the set path. Solution: add the path and domain name when calling SetCookie. the domain name format can be "bbs.it-home.org", but also ".jbxue.com ". The value part in the SetCookie function is automatically encoded when it is passed. If the value of value is "test value", it is changed to "test % 20value" when passed, which is the same as the URL method. This is transparent to the program. PHP will automatically decode the Cookie value when receiving it. To set multiple cookies with the same name, use an array: 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 supports Cookie receiving and processing very well and is completely automatic. it is as simple as the FORM variable principle. 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 named $ myCookie, which is the same as a common variable, the value of this variable is the Cookie value. Arrays also apply. Another method is to reference the global variable $ HTTP_COOKIE_VARS array of PHP. Examples are as follows: (assuming these are all set in the previous page and still valid) Echo $ MyCookie; echo $ CookieArray [0]; echo count ($ CookieArray); echo $ HTTP_COOKIE_VARS ["MyCookie"];That's simple. 3,Delete CookieThere are two ways to delete an existing Cookie: one is to call a SetCookie with only the name parameter, and the Cookie named this name will be deleted from the relationship host; another method is to set the Cookie's expiration time to time () or time ()-1. then, the Cookie is deleted after the page is viewed (in fact, it is invalid ). Note: When a Cookie is deleted, its value is still valid on the current page. 4,Cookie usage restrictionsFirst, it must be set before HTML file content output; Second, different browsers may make different speeches on Cookie processing, and sometimes error results may occur. For example, ms ie + service pack 1 cannot correctly process cookies with domain names and paths. Netscape Communicator 4.05 and ms ie 3.0 cannot correctly process cookies without path and time. Ms ie 5 does not seem to be able to process cookies with domain names, paths, and time. This is what I found when designing the website page. The third limitation is on the client. A browser can create a maximum of 30 cookies, each of which cannot exceed 4 kB. each WEB site can set a maximum of 20 cookies. The above is all the content about php cookie operations described in this section. we have introduced in detail how to set, use, and delete cookies in php and some precautions. Hope to help you. |