The establishment of a commercial site or a more functional personal site, often need to record the visitor's information, in PHP provides two convenient means: Session and cookie function. To keep the user information permanently, Cookies are the most convenient means. Here I will give you a detailed explanation of the function of cookies and how to use them.
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.
Four: some characteristics 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;
Vii. limitations of 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
?
cookie.php
if (!isset ($flag))
{
Setcookie ("MyCookie", "This My cookie!");
Header ("location:cookie.php?flag=1");
Exit
}
?>
<body>
?
The echo "Cookie has:". $mycookie;
?>
</body>
How, through the above introduction, you are not a PHP in the cookie function has a detailed understanding. Use your ingenuity to make your site more powerful. If you have any questions, please contact fbigov@sina.com. Let's make progress together.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.