Phpsetcookie (name, value, expires, path, domain, secure) parameter _ PHP Tutorial

Source: Internet
Author: User
Tags set cookie
Phpsetcookie (name, value, expires, path, domain, secure) parameters. Setcookie () defines a cookie sent together with other HTTP headers. Like other headers, cookies must be sent before any other output of the script (this is a protocol restriction ). This setcookie () defines a cookie sent together with other HTTP headers. Like other headers, cookies must be sent before any other output of the script (this is a protocol restriction ). This requires that the call of this function be placed before any output, includingAndLabel and any space. If any output exists before setcookie () is called, this function fails and returns FALSE. If the setcookie () function runs successfully, TRUE is returned. This does not indicate whether the user has accepted the cookie.
Function definition:
Bool setcookie (string name [, string value [, int expire [, string path [, string domain [, bool secure])
Setcookie () parameters
Parameters Description Example
Name Cookie name Use$ _ COOKIE ['cookiename']Call the cookie named cookiename.
Value Cookie value, which is stored on the client. do not store sensitive data. Assume thatNameIs 'cookiename', you can use$ _ COOKIE ['cookiename']Obtain the value.
Expire

The time when the Cookie expires. This is a Unix timestamp, that is, the number of seconds from the Unix epoch.

In other words, it is usually usedTime ()The function plus the number of seconds to set the cookie expiration time.

Or useMktime ().

Time () + 60*60*24*30The cookie is set to expire after 30 days.

If not set, the cookie will expire after the session ends (usually closed by the browser.

Path Valid Cookie path on the server.

If this parameter is set'/'Then, the cookie isDomainValid,

If'/Foo /'The cookie isDomainUnder/Foo/Directory and its subdirectories are valid, such/Foo/bar/.

The default value is the current directory of the cookie.

Domain The valid domain name of the cookie.

This parameter should be set'.Example.com'.

Although.Not required, but it will be compatible with more browsers.

If this parameter is setWww.example.comInWwwValid in the subdomain.

For details, see tail matching in the Cookie specification.

Secure

Indicates whether the cookie is transmitted only through a secure HTTPS connection.

When setTRUEThe cookie is only set in secure connections. The default value isFALSE.

0Or1

Example 1. setcookie () sending example

The code is as follows:


$ Value = 'something from somewhere ';
Setcookie ("TestCookie", $ value );
Setcookie ("TestCookie", $ value, time () + 3600);/* expire in 1 hour */
Setcookie ("TestCookie", $ value, time () + 3600 ,"/~ Rasmus/",". utoronto. ca ", 1 );


Note that the portion of the value in the cookie will be automatically encoded with urlencode upon sending and automatically decoded upon receipt, and the value will be assigned to the cookie variable with the same name. If you do not want to do this and use PHP 5, you can use setrawcookie () instead. In the following simple example, we can get the cookie value just set:

The code is as follows:


// Output a separate cookie
Echo $ _ COOKIE ["TestCookie"];
Echo $ HTTP_COOKIE_VARS ["TestCookie"];
// Another debugging method is to output all cookies.
Print_r ($ _ COOKIE );
?>


To delete a cookie, you must ensure that its expiration time is in the past to trigger the deletion mechanism of the browser. The following example shows how to delete the cookie you just set:
Example 2. setcookie () deletion example

The code is as follows:


// Set the expiration time to one hour ago
Setcookie ("TestCookie", "", time ()-3600 );
Setcookie ("TestCookie", "", time ()-3600 ,"/~ Rasmus/",". utoronto. ca ", 1 );


You can also set an array cookie by using array symbols in the cookie name. you can set multiple cookies as array units. when the script extracts cookies, all values are placed in an array:
Example 3. example of using arrays in setcookie ()

The code is as follows:


// Set cookie
Setcookie ("cookie [three]", "cookiethree ");
Setcookie ("cookie [two]", "cookietwo ");
Setcookie ("cookie [one]", "cookieone ");
// Refresh the page and display it
If (isset ($ _ COOKIE ['cookies']) {
Foreach ($ _ COOKIE ['cooker'] as $ name => $ value ){
Echo "$ name: $ value
\ N ";
}
}
?>


The above example will output:
Three: cookiethree
Two: cookietwo
One: cookieone

Summary: It is not difficult to use cookies. the focus of this article is to master path settings and domain settings.


The http://www.bkjia.com/PHPjc/327934.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327934.htmlTechArticlesetcookie () defines a cookie that is sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any other output of the script (this is a protocol restriction ). This...

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.