PHP COOKIE support details

  • 2020-03-31 21:16:25
  • OfStack

One: set cookies
Cookies must be set before using them.
Function prototype :int setcookie(string name,string value,int expire,string path,string domain,int secure)
Where, all parameters except name are optional, and an empty string can be used to represent unset.
Property value: to specify a value.
Property path: specifies which directory path the cookie is sent to on the server.
Property domain: the ability to restrict the sending of cookies on the browser side.
Expire parameter: specifies the valid time of the cookie, which is a standard Unix time token.
You can do it in seconds using either time() or mktime().
Secure parameter: indicates whether the cookie is transmitted over the network over the encrypted HTTPS protocol.

Two: matters needing attention when setting cookie
If you want to delete a cookie and then write a cookie, you must write a write statement before you write a delete statement.

Example of setcookie
Simple: setcookie("mycookie","value_of_mycookie");
Setcookie ("withExpire","Expire_in_1_hour",time()+3600);
Setcookie ("FullCookie","Full_cookie_value",time+3600,"/forum","www.123.com",1);

Four: some features of cookie
Cookies are path-oriented. The WEB server page automatically passes the current path to the browser with the default path property. Specifying a path forces the server to use the set path.
Cookies set in one directory page are not visible in another directory page.

Five: receive and process cookies
PHP handles cookies automatically, just as it does FORM variables, but you can also use the PHP global variable, the $HTTP_COOKIE_VARS array.
Example: echo $mycookie;
Echo $cookie Array [0];
Echo the count ($cookie Array);
Echo $HTTP_COOKIE_VARS (" mycookie ");

Delete cookies
(1) call setcookie() with name parameter only;
(2) make the invalidation time time() or time-1;

Restrictions on the use of cookies
(1) must be set before the content output of the HTML file;
(2) different browsers handle cookies inconsistently, so it must be considered when using them;
(3) restrictions on the client side, such as the user setting the forbidden cookie, the cookie cannot be established;

Eight: a specific example, I hope you have a deeper understanding of cookies
 
<? 
//cookie.php 
if(!isset($flag)) 
{ 
setcookie("mycookie","this my cookie!"); 
header("location:cookie.php?flag=1"); 
exit; 
} 
?> 
<html> 
<body> 
<? 
echo "cookie There are :".$mycookie; 
?> 
</body> 
</html> 

Related articles: