One: Set cookies
Cookies must be set before using cookies.
function prototypes: int Setcookie (string name,string value,int expire,string path,string Secure)
where, except for name, all parameters are optional and can be represented by an empty string that is not set.
Property value: Used to specify a value.
Property path: Use to specify which directory path the cookie is sent to the server.
Property domain: The ability to limit the sending of cookies on the browser side.
Expire parameter: The valid time to specify the cookie, which is a standard Unix time token.
Can be obtained with the time () or the Mktime () function, in seconds.
Secure parameter: Indicates whether this cookie is transmitted over the network via an encrypted HTTPS protocol.
II: Considerations when setting Cookies
Cookies are set up in the same page, in fact, in sequential order. If you want to delete a cookie and then write a cookie, you must write the statement first, and then write the deletion statement. Otherwise, there will be an error.
Three: Setcookie examples
Simple: Setcookie ("MyCookie", "Value_of_mycookie");
With Expiration: Setcookie ("Withexpire", "Expire_in_1_hour", Time () +3600);
Everything: Setcookie ("Fullcookie", "Full_cookie_value", time+3600, "/forum", "www.123.com", 1);
Four: Some features of cookies
Cookies are path-oriented. When the default Path property is used, the Web server page automatically passes the current path to the browser. The specified path forces the server to use the path of the setting.
Cookies in one directory page are not visible on the page of another directory.
V: Receive and process cookies
PHP's processing of cookies is automatic, as is the principle of dealing with form variables. You can also use PHP global variables to $HTTP _cookie_vars arrays.
Example: Echo $mycookie;
echo $cookie array[0];
echo Count ($cookie Array);
echo $HTTP _cookie_vars["MyCookie"];
VI: Delete Cookies
(1) Invoke the Setcookie () with the name parameter only;
(2) to make the time of expiration () or time-1;
Seven: Restrictions on using cookies
(1) must be set before the content output of the HTML file;
(2) different browsers on the processing of cookies inconsistent, use must be considered;
(3) Client restrictions, such as user settings prohibit cookies, the cookie can not be established;
Eight: A concrete example, I hope you have a more profound understanding of cookies
Copy Code code as follows:
?
cookie.php
if (!isset ($flag))
{
Setcookie ("MyCookie", "This My cookie!");
Header ("location:cookie.php?flag=1");
Exit
}
?>
<body>
?
The echo "Cookie has:". $mycookie;
?>
</body>