Home  >  Article  >  Backend Development  >  Detailed explanation of COOKIE support in PHP4 transferred from Oso_PHP Tutorial

Detailed explanation of COOKIE support in PHP4 transferred from Oso_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:23:18768browse

PHP4's COOKIE support is explained in detail. When establishing a commercial site or a personal site with relatively complete functions, it is often necessary to record visitor information. PHP provides two convenient means: session and cookie functions. In order to permanently maintain user information, cookies are The most convenient method. Here I will explain the functions and usage of cookies in detail. 1: Set cookies. Cookies must be set before using cookies. Function prototype: int setcookie(string name, string value, int expire, string path, string domain, int secure) Among them, except name, all parameters are optional, and an empty string can be used to indicate not set. Attribute value: used to specify the value. Attribute path: used to specify the cookie to be sent to the server Which directory path. Attribute domain: can limit the sending of cookies on the browser side. Expire parameter: used to specify the validity time of the cookie, which is a standard Unix time mark. You can use time() or mktime( ) function, in seconds. Secure parameter: Indicates whether this cookie is transmitted over the network through the encrypted HTTPS protocol. 2: Things to note when setting cookies When setting cookies on the same page, you actually start from back to front In order. If you want to delete a cookie first and then write a cookie, you must write the write statement first and then the delete statement. Otherwise, an error will occur. Three: Setcookie example is simple: setcookie("mycookie"," value_of_mycookie"); with expiration time: setcookie("withExpire","Expire_in_1_hour",time()+3600); everything: setcookie("FullCookie","Full_cookie_value",time+3600,"/forum", "www.123.com",1); Four: Some characteristics of cookies Cookies are path-oriented. When the path attribute is defaulted, the WEB server page will automatically pass the current path to the browser. Specifying the path will force the server to use the set path. . Cookies set in one directory page cannot be seen in another directory page. Five: Receiving and processing cookies PHP processes cookies fully automatically, and the principles are the same as processing FORM variables. Of course, you can also use PHP global variables, $HTTP_COOKIE_VARS array. Example: echo $mycookie; echo $cookie Array[0]; echo count($cookie Array); echo $HTTP_COOKIE_VARS["mycookie"]; Six: Delete cookies (1) Call only with setcookie() of the name parameter; (2) Set the expiration time to time() or time-1; Seven: Restrictions on using cookies (1) Must be set before the content of the HTML file is output; (2) Different browsers have different views on cookies The processing is inconsistent and must be considered when using it; (3) Client restrictions, such as user settings that prohibit cookies, the cookie cannot be created; 8: A specific example, I hope everyone has a deeper understanding of cookies //cookie.php if(!isset($flag)) { setcookie("mycookie","this my cookie!"); header("location:cookie.php?flag=1"); exit; } ?>

echo "cookie中有:".$mycookie; ?> How about, through the above introduction, do you have a detailed understanding of the COOKIE function in PHP. Use your ingenuity to make your site more powerful. If you have any questions, please contact fbigov@sina.com. Let us make progress together.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532234.htmlTechArticleDetailed explanation of COOKIE support for PHP4 When establishing a commercial site or a personal site with relatively complete functions, it is often necessary to record visitor information. PHP provides two convenient means: session and coo...
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