Details of php setcookie of name value expires path domain secure parameters

  • 2020-06-23 00:04:37
  • OfStack

setcookie() defines the cookie sent from 1 and the remaining HTTP header 1. Like the other header 1, cookie must be sent before any other output from the script (this is a protocol limitation). This requires putting the call to this function before any output, including < html > and < head > Labels and any Spaces. If there is any output before calling setcookie(), this function will fail and return FALSE. If the setcookie() function runs successfully, TRUE is returned. This does not indicate whether users have accepted cookie or not.
Function definition:
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )
Details of setcookie() parameters
parameter instructions For example, name The name of the cookie Call cookie named cookiename with $_COOKIE['cookiename']. value cookie value, stored in the client, do not store sensitive data Assuming that name is 'cookiename', the value can be obtained by $_COOKIE['cookiename']. expire

Cookie expiration date. This is an Unix timestamp, the number of seconds since the Unix epoch.

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

Or mktime().

time()+60*60*24*30 sets cookie to expire after 30 days.

If not set, cookie will expire at the end of the session (usually when the browser is closed).

path Cookie valid path on the server side.

If this parameter is set to '/', cookie is valid throughout domain,

If set to '/foo/', cookie is only valid in the /foo/ directory under domain and its subdirectories, such as /foo/bar/.

The default value is set to the current directory of cookie.

domain The cookie valid domain name.

For cookie to be valid for all subdomains such as example.com, this parameter should be set to '.example.com '.

Although. Is not necessary, plus it will work with more browsers.

If this parameter is set to ES82en.example.com, it is valid only within the www subdomain.

See tail matching in the Cookie specification for details.

secure

Indicates whether cookie is transmitted only over a secure HTTPS connection.

When set to TRUE, cookie is set only on secure connections. The default value is FALSE.

0 or 1
Example 1. setcookie() sends an example

$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 cookie value that is sent is automatically encoded in urlencode and is automatically decoded on receipt and assigned to the cookie variable with the same name. If you do not want this and are using PHP 5, use setrawcookie() instead. Here's a simple example to get the value of cookie you just set:

<?php
//  Output individual  cookie
echo $_COOKIE["TestCookie"];
echo $HTTP_COOKIE_VARS["TestCookie"];
//  On the other 1 One way to debug is to output all of them  cookie
print_r($_COOKIE);
?>

To remove cookie, you need to ensure that its expiration date is in the past to trigger the browser's deletion mechanism. The following example shows how to delete cookie you just set:
Example 2. setcookie() removes the example

//  Set the expiration time to 1 Hours before 
setcookie("TestCookie", "", time() - 3600);
setcookie("TestCookie", "", time() - 3600, "/~rasmus/", ".utoronto.ca", 1);  

You can also set the array cookie by using the array symbol in the cookie name. You can set multiple cookie as array units, and when the script extracts cookie, all values are placed in one array type:
Example 3.setcookie () using an array

<?php
//  set  cookie
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
//  After refreshing the page, it is displayed 
if (isset($_COOKIE['cookie'])) {
    foreach ($_COOKIE['cookie'] as $name => $value) {
        echo "$name : $value <br />\n";
    }
}
?>

The above example will output:
three : cookiethree
two : cookietwo
one : cookieone

Summary: the basic use of cookie is not difficult. The main focus of this article is to master the path setting of path and the domain setting of domain.



Related articles: