Php cookie settings Delete logon details

Source: Internet
Author: User
Tags set cookie

Cookie is one of the things in all program development. Next I will introduce how to delete ($ _ cookie) the COOKIE settings (SetCookie.

1. Set Cookie

PHP uses the SetCookie function to set the Cookie. Note that Cookie is a part of the HTTP header used to transmit information between the browser and the server. Therefore, the Cookie function must be called before any HTML file is output.

The SetCookie function defines a Cookie and attaches it to the end of the HTTP header. The prototype of the SetCookie function is as follows:

Int SetCookie (string name, string value, int expire, string path, string domain, int secure );
All parameters except name are optional. The value, path, and domain parameters can be replaced by an empty string, indicating that they are not set. The expire and secure parameters are numeric and can be expressed as 0. The expire parameter is a standard Unix time mark, which can be obtained using the time () or mktime () function, in seconds. The secure parameter indicates whether the Cookie is transmitted over the network through the encrypted HTTPS protocol.

The Cookie currently set does not take effect immediately, but will not be visible until the next page. this is because the Cookie is transmitted from the server to the client's browser on the configured page, and the Cookie can be retrieved from the client's machine and sent back to the server on the next page.

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:

How to Create a cookie?
The setcookie () function is used to set the cookie.

Note: The setcookie () function must be located before the

Syntax
Setcookie (name, value, expire, path, domain );

Simple:

The Code is as follows: Copy code

SetCookie ("MyCookie", "Value of MyCookie ");

With expiration time:

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

Everything:

SetCookie ("FullCookie", "Full cookie value", time () + 3600, "/forum", ".phpuser.com", 1 );

Here is another note. For example, if your site has several different directories, if you only use cookies without paths, the Cookie set on the page under a 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. To solve this problem, add the path and domain name when calling SetCookie. The domain name format can be "www.phpuser.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 ″);

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

Echo $ MyCookie;

Echo $ CookieArray [0];

Echo count ($ CookieArray

How can I retrieve the Cookie value?
The $ _ COOKIE variable of PHP is used to retrieve the cookie value.

In the following example, we retrieve the cookie value named "user" and display it on the page:

The Code is as follows: Copy code

<? Php
// Print a cookie
Echo $ _ COOKIE ["user"];

// A way to view all cookies
Print_r ($ _ COOKIE );
?>

In the following example, we use the isset () function to check whether the cookie has been set:

The Code is as follows: Copy code

<Html>
<Body>

<? Php
If (isset ($ _ COOKIE ["user"])
Echo "Welcome". $ _ COOKIE ["user"]. "! <Br/> ";
Else
Echo "Welcome guest! <Br/> ";
?>

</Body>
</Html>

How to delete a cookie?
When deleting a cookie, you should change the expiration date to the past time point.

Example of deletion:

The Code is as follows: Copy code

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

Example

Php cookie to set the user login time and expiration time code.

The Code is as follows: Copy code

Function loginCookie ($ uid, $ name, $ group, $ ip, $ time)
{
Global $ site_domain, $ login_key;
 
$ Domain = (substr ($ site_domain, 0, 4) = "www .")? Substr ($ site_domain, 3): ".". $ site_domain;

$ Secure = Xxtea: encrypt ($ uid. "|". $ name. "|". $ group. "|". $ ip, $ login_key );

Setcookie ("userId", $ uid, $ time + 86400, "/", $ domain );

Setcookie ("userName", $ name, $ time + 86400, "/", $ domain );

Setcookie ("userGroup", $ group, $ time + 86400, "/", $ domain );

Setcookie ("userSecure", $ secure, $ time + 86400, "/", $ domain );
}

For more details, see: http://www.bKjia. c0m/phper/18/1 b5df18d38cfea1a63282c417f6cdf63.htm

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.