How to create, access and delete cookies in php setcookie() function

setcookie() defines a cookie to lớn be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any đầu ra from your script (this is a protocol restriction). This requires that you place calls to lớn this function prior to any output, including & tags as well as any whitespace.

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE array. Cookie values may also exist in $_REQUEST.


Bạn đang xem: How to create, access and delete cookies in php setcookie() function

Parameters

» RFC 6265 provides the normative reference on how each setcookie() parameter is interpreted.

name

The name of the cookie.

value

The value of the cookie. This value is stored on the clients computer; bởi vì not store sensitive information. Assuming the name is "cookiename", this value is retrieved through $_COOKIE<"cookiename">

expires

The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you"ll most likely mix this with the time() function plus the number of seconds before you want it khổng lồ expire. Or you might use mktime(). Time()+60*60*24*30 will set the cookie to expire in 30 days. If set khổng lồ 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

Note:

You may notice the expires parameter takes on a Unix timestamp, as opposed khổng lồ the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.

path

The path on the vps in which the cookie will be available on. If set lớn "/", the cookie will be available within the entire domain. If set to lớn "/foo/", the cookie will only be available within the /foo/ directory và all sub-directories such as /foo/bar/ of domain. The mặc định value is the current directory that the cookie is being phối in.

tên miền

The (sub)domain that the cookie is available to. Setting this khổng lồ a subdomain (such as "www.example.com") will make the cookie available to lớn that subdomain and all other sub-domains of it (i.e. W2.www.example.com). Lớn make the cookie available khổng lồ the whole domain name (including all subdomains of it), simply phối the value to the domain name ("example.com", in this case).

Older browsers still implementing the deprecated » RFC 2109 may require a leading . Lớn match all subdomains.

secure

Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to TRUE, the cookie will only be mix if a secure connection exists. On the server-side, it"s on the programmer lớn send this kind of cookie only on secure connection (e.g. With respect khổng lồ $_SERVER<"HTTPS">).

httponly

When TRUE the cookie will be made accessible only through the HTTP protocol. This means that the cookie won"t be accessible by scripting languages, such as JavaScript. It has been suggested that this setting can effectively help khổng lồ reduce identity theft through XSS attacks (although it is not supported by all browsers), but that claim is often disputed. Added in PHP 5.2.0. TRUE or FALSE

options

An associative array which may have any of the keys expires, path, domain, secure, httponly & samesite. If any other key is present an error of màn chơi E_WARNING is generated. The values have the same meaning as described for the parameters with the same name. The value of the samesite element should be either None, Lax or Strict. If any of the allowed options are not given, their mặc định values are the same as the default values of the explicit parameters. If the samesite element is omitted, no SameSite cookie attribute is set.


Return Values

If output exists prior lớn calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.


Xem thêm: Hướng Dẫn Cách Sửa Lỗi System Recovery Options Là Gì, Sửa Lỗi Khởi Động Lặp Lại Trên Windows 7

$value = "something from somewhere";setcookie("TestCookie", $value);setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */setcookie("TestCookie", $value, time()+3600, "/~rasmus/", "example.com", 1);?>

chú ý that the value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned lớn a variable by the same name as the cookie name. If you don"t want this, you can use setrawcookie() instead if you are using PHP 5. To see the contents of our test cookie in a script, simply use one of the following examples:


// Print an individual cookieecho $_COOKIE<"TestCookie">;// Another way to debug/test is to view all cookiesprint_r($_COOKIE);?>