Home  >  Article  >  Backend Development  >  How cookies work_PHP tutorial

How cookies work_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:34847browse

  • When a customer visits a website based on PHP technology, the setcookie() function can be used in PHP to generate a cookie. After processing, the system sends the cookie to the client and saves it in the C:Documents andSettings username Cookies directory. .
  • Cookies are part of the HTTP headers, so the setcookie() function must be called before any content of the HTML itself is sent to the browser. This restriction is the same as the header() function (if you need to understand the head() function, please check it yourself).
  • When the customer visits the website again, the browser will automatically send the cookie corresponding to the website in the C:DocumentsandSettings username Cookies directory to the server, and the server will automatically convert the cookie passed from the client into a PHP variables. In PHP5, cookies sent by the client will be converted into global variables. You can read it through $_COOKIE[‘xxx’].

Define a cookie

  • Set cookies:
  • Syntax: boolsetcookie(stringname,[stringvalue,[int expire,[stringpath,[stringdomain,[int secure]]]]]);

This cookie function can have 6 attributes, and the commonly used ones have 3 parameters.

$value="the best way is by yourself";

setcookie("cookiename",$value,time()+60*60*24*7);

PHP has good support for cookies. Just like form forms, PHP will automatically receive the HTTP header from the web server and process it when received. When receiving, use $_COOKIE["cookiename"] or $HTTP_COOKIE_VARS["cookiename"] (not recommended)

If the website has several different file directories and uses a cookie without a path, the cookie can only be accessed in the path of the file that sets the cookie. If a path is specified, the path when setting will be used as the specified path to access the cookie.

Create cookie array:

setcookie("CookieArray[0]","Value1");

setcookie("CookieArray[1]","Value2");

setcookie("CookieArray['one']","Value1");

setcookie("CookieArray['two']","Value2");

setcookie("cookie[three]","cookiethree");

setcookie("cookie[two]","cookietwo");

setcookie("cookie[one]","cookieone");

//After refreshing the page, it will be displayed

if(isset($_COOKIE['cookie'])){

foreach($_COOKIE['cookie']as$name=>$value){

echo "$name:$value
n";

}

}

?>

Delete cookies

1. Call setcookie with only the name parameter, then the cookie with this name will be deleted from the client;

setcookie("MyCookie");//Delete MyCookie

2. The expiration time is enough, then the cookie will be deleted (actually invalid) after browsing this page.

For example:

setcookie("MyCookie","Value",time()-1);

//Delete MyCookie.

Note: When a cookie is deleted, its value is still valid on the current page. If you want to set the cookie to expire after the browser is closed. Then you can directly set expiretime to 0, or do not set this value.

For example: setcookie("name","value",0).

Cookie Notes

  • 1. There cannot be any html output before setcookie(), even spaces and blank lines will not work. It must be set before the content of the html file is output
  • 2. After setcookie(), there will be no output when you call echo$_COOKIE["name"] on the current page. You must refresh or go to the next page to see the cookie value.
  • 3. There is no need for browsers to handle cookies differently. The client can disable cookies, and the browser will also limit the number of cookies. The maximum number of cookies that can be created by a browser is 300, and each cookie cannot exceed 4KB. The total number of cookies that can be set by each WEB site cannot exceed 20.
  • 4. Cookies are stored on the client side. If the user disables cookies, your cookies will naturally have no effect! Therefore, avoid over-reliance on cookies and think about solutions if cookies are disabled, just in case.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/738550.htmlTechArticleWhen a customer visits a website based on PHP technology, the setcookie() function can be used in PHP to generate a cookie , the system sends this cookie to the client after processing and saves it in C:Documen...
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