How to set, use, and delete cookies in PHP

Source: Internet
Author: User
Tags send cookies setcookie valid

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: Copy code
SetCookie ("Myookie", "Value of MyCookie ");

Example 1

Set and send cookies:

The code is as follows: Copy code

<? Php
$ Value = "my cookie value ";

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

<Html>
<Body>

...
...

The code is as follows: Copy code

<? Php
$ Value = "my cookie value ";

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

<Html>
<Body>

...

...


Example 2
Different methods for retrieving cookie values:

The code is as follows: Copy code

<Html>
<Body>

<? Php

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

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

</Body>
</Html> 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: Copy code

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

<Html>
<Body>

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

The code is as follows: Copy code

<? Php
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 <br/> ";
    }
  }
?>

<Html>
<Body>

...
... 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: Copy code

SetCookie ("CookieArray []", "Value 1 & Prime ;);

SetCookie ("CookieArray []", "Value 2 & Prime ;);
 
Or
 

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

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

 

 

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: Copy code

 
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: Copy code

Setcookie ($ cookiename ,'');

Or

Setcookie ($ cookiename, NULL );

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

With expiration time:

The code is as follows: Copy code

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.