COOKIE support in PHP4
To create a commercial site or a personal site with relatively complete functions, you often need to record the visitor's information. In PHP, two convenient methods are provided: session and cookie functions. cookie is the most convenient method to keep user information permanently. here I will explain in detail the functions and usage of cookies.
I. Set cookie
You must set the cookie before using the cookie.
Function prototype: int setcookie (string name, string value, int expire, string path, string domain, int secure)
All parameters except name are optional and can be empty strings.
Attribute value: used to specify the value.
Attribute path: Specifies the directory path in which the cookie is sent to the server.
Attribute domain: limits the sending of cookies on the browser side.
Expire parameter: used to specify the cookie validity period, which is a standard Unix time mark.
It can be obtained using the time () or mktime () function, in seconds.
Secure parameter: indicates whether the cookie is transmitted over the network through the encrypted HTTPS protocol.
Ii. considerations when setting cookies
Setting cookies on the same page is actually performed in the order from the back to the back. if you want to delete a cookie and then write a cookie, you must first write the statement and then write the delete statement. otherwise, an error occurs.
Iii. Example of setcookie
Simple: setcookie ("mycookie", "value_of_mycookie ");
Setcookie ("withExpire", "Expire_in_1_hour", time () + 3600 );
ALL: setcookie ("FullCookie", "Full_cookie_value", time + 3600, "/forum", "www.123.com", 1 );
Iv. Cookies
Cookie is path-oriented. When the default path attribute is used, the WEB Server Page will automatically pass the current path to the browser. specifying a path will force the server to use the set path.
The cookie set in one directory page is invisible on the page of another directory.
5. Receive and process cookies
PHP automatically processes cookies, just like the FORM variable processing principle. 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"];
6. Delete cookies
(1) Call setcookie () with only the name parameter ();
(2) set the expiration time to time () or time-1;
7. cookie usage restrictions
(1) it must be set before HTML file content output;
(2) Different browsers may process different cookies, so you must consider them when using them;
(3) client restrictions. For example, if the user sets to disable cookies, the cookie cannot be created;
8. A specific example. I hope you will 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 "among the cookies:". $ mycookie;
?>
</Body>
</Html>
Through the above introduction, do you have a detailed understanding of the COOKIE function in PHP. use your intelligence to make your site more powerful. if you have any questions, please contact fbigov@sina.com. let's make progress together.