PHP setcookie (name, value, expires, path, domain, secure) Parameters

Source: Internet
Author: User
Tags set cookie

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, including the <HTML> and 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 to '/' , the cookie is valid for the entire domain .

if it is set to '/Foo/' , the cookie is valid only in the /Foo/ directory and Its subdirectories under domain , for example, /Foo/BAR/.

the default value is the current directory of the cookie.

domain valid Domain Name of the cookie.

to make the cookie valid for all subdomains under the domain name example.com, set this parameter to '.example.com' .

although . is not mandatory, but it is compatible with more browsers.

if this parameter is set to www.example.com , it is valid only in the WWW subdomain.

For details, see tail matching in cookie standards.

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
CopyCodeThe 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:Copy codeThe Code is as follows: <? PHP
// 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
Copy codeThe 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 ()
Copy codeThe Code is as follows: <? PHP
// 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 <br/> \ n ";
}
}
?>

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

Summary: it is not difficult to use cookies.ArticleThe record focuses on path setting and domain name setting.

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.