COOKIE support in PHP4 _ PHP Tutorial

Source: Internet
Author: User
COOKIE support for PHP4. Cookies of PHP4 support the establishment of commercial sites or well-functional personal sites in detail. visitors often need to be recorded. in PHP, two convenient methods are provided: COOKIE support for session and cook 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;
}
?>


Echo "among the cookies:". $ mycookie;
?>



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.

Ghost often needs to record visitor information when establishing commercial sites or personal sites with relatively complete functions. in PHP, it provides two convenient methods: session and cook...

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.