How to Set, use, and delete cookies in PHP-PHP source code

Source: Internet
Author: User
Tags send cookies
Cookie is a common function in php. Next I will introduce you to the Cookie settings, usage, and deletion methods in PHP. If you need to know how to use cookies, please refer. Cookie is a common function in php. Next I will introduce you to the Cookie settings, usage, and deletion methods in PHP. If you need to know how to use cookies, please refer.

Script ec (2); script

Cookie syntax

Int SetCookie (string name, string value, int expire, string path, string domain, int secure );


Setting a Cookie on the same page actually goes from the back to the back. If you want to delete a Cookie before inserting it, you must first write the insert statement and then write the delete statement, otherwise, unexpected results may appear. Here are several examples:


Simple:

The Code is as follows:
SetCookie ("Myookie", "Value of MyCookie ");

Example 1

Set and send cookies:

The Code is as follows:

$ Value = "my cookie value ";

// Send a simple cookie
Setcookie ("TestCookie", $ value );
?>


...
...

The Code is as follows:

$ Value = "my cookie value ";

// Send a cookie that expires in 24 hours
Setcookie ("TestCookie", $ value, time () + 3600*24 );
?>


...

...


Example 2
Different Methods for retrieving cookie values:

The Code is as follows:


// Output individual cookies
Echo $ _ COOKIE ["TestCookie"];
Echo"
";
Echo $ HTTP_COOKIE_VARS ["TestCookie"];
Echo"
";

// Output all cookies
Print_r ($ _ COOKIE );
?>


Output:

My cookie value
My cookie value
Array ([TestCookie] => my cookie value)

Example 3
Delete a cookie by setting the expiration date to the past date/time:

The Code is as follows:

// Set the expiration date to one hour ago
Setcookie ("TestCookie", "", time ()-3600 );
?>


...
... Example 4
Create an array cookie:

The Code is as follows:

Setcookie ("cookie [three]", "cookiethree ");
Setcookie ("cookie [two]", "cookietwo ");
Setcookie ("cookie [one]", "cookieone ");

// Output cookie (After reloading the page)
If (isset ($ _ COOKIE ["cookie"])
{
Foreach ($ _ COOKIE ["cookie"] as $ name => $ value)
{
Echo "$ name: $ value
";
}
}
?>


...
... Output:

Three: cookiethree
Two: cookietwo
One: cookieone


For example, if your site has several different directories, if you only use cookies without paths, the Cookie set on the page under one directory is invisible on the page of another directory, that is, the Cookie is path-oriented. In fact, even if no path is specified, the WEB server will automatically pass the current path to the browser, and the specified path will force the server to use the set path. The solution to this problem is to add the path and domain name when calling SetCookie. The domain name format can be "www.smalluv.com" or ".phpuser.com ". The SetCookie function indicates the value part, which is automatically encoded during transmission. That is to say, if the value of "test value" is changed to "test % 20value" during transmission ", same as the URL method. Of course, this is transparent to programs, because PHP will automatically decode the Cookie value when receiving it.

To set multiple cookies with the same name, use an array:

The Code is as follows:

SetCookie ("CookieArray []", "Value 1 ″);

SetCookie ("CookieArray []", "Value 2 ″);

Or

SetCookie ("CookieArray [0]", "Value 1 ″);

SetCookie ("CookieArray [1]", "Value 2 ″);

2. Receive and process cookies

PHP supports Cookie receiving and processing very well and is completely automatic. It is as simple as the FORM variable principle. For example, if you set a Cookie named MyCookier, PHP will automatically analyze it from the HTTP header received by the WEB server and form a variable named $ myCookie, which is the same as a common variable, the value of this variable is the Cookie value. Arrays also apply.

Another method is to reference the global variable $ HTTP_COOKIE_VARS array of PHP.

Examples are as follows: (assuming these are all set in the previous page and still valid)

The Code is as follows:


Echo $ MyCookie;
Echo $ CookieArray [0];
Echo count ($ CookieArray );
Echo $ HTTP_COOKIE_VARS ["MyCookie"];

That's simple.


3. delete a Cookie

There are two ways to delete an existing Cookie:

One is to call the SetCookie with only the name parameter, the Cookie named this name will be deleted from the relationship host;

Another method is to set the Cookie's expiration time to time () or time ()-1. Then, the Cookie is deleted after the page is viewed (in fact, it is invalid ). Note that when a Cookie is deleted, its value is still valid on the current page.

The Code is as follows:

Setcookie ($ cookiename ,'');

Or

Setcookie ($ cookiename, NULL );

Will delete the cookie, of course, these manuals do not.

With expiration time:

The Code is as follows:

SetCookie ("WithExpire", "Expire in 1 hour", time () + 3600); // 3600 seconds = 1 hour

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.