Php set cookie domain

setcookie ( string $name , string $value = "" , int $expires = 0 , string $path = "" , string $tên miền = "" , bool $secure = false , bool $httponly = false ) : bool

Bạn đang xem: Php set cookie domain

setcookie ( string $name , string $value = "" , array $options = <> ) : bool

setcookie() defines a cookie khổng lồ be sent along with the rest of the HTTPhường headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to lớn this function prior to any output, including và 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.


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


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


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 phối this with the time() function plus the number of seconds before you want it to lớn expire. Or you might use mktime() . time()+60*60*24*30 will set the cookie to expire in 30 days. If phối to lớn 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 lớn the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because does this conversion internally.


The path on the server in which the cookie will be available on. If phối khổng lồ '/', the cookie will be available within the entire domain name. If set lớn '/foo/', the cookie will only be available within the /foo/ directory & all sub-directories such as /foo/bar/ of domain name. The mặc định value is the current directory that the cookie is being mix in.

tên miền

The (sub)tên miền that the cookie is available lớn. Setting this khổng lồ a subdomain name (such as '') will make the cookie available to that subtên miền & all other sub-domains of it (i.e. To make the cookie available lớn the whole domain (including all subdomains of it), simply set the value to lớn the tên miền name ('', in this case).

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


Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set khổng lồ true, the cookie will only be phối if a secure connection exists. On the server-side, it's on the programmer lớn skết thúc this kind of cookie only on secure connection (e.g. with respect lớn $_SERVER<"HTTPS">).


When true the cookie will be made accessible only through the HTTPhường 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 to reduce identity theft through XSS attacks (although it is not supported by all browsers), but that clalặng is often disputed. true or false


An associative array which may have any of the keys expires, path, domain, secure, httponly and samesite. If any other key is present an error of level E_WARNING is generated. The values have sầu 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 default values are the same as the default values of the explicit parameters. If the samesite element is omitted, no SameSite cookie attribute is set.

Xem thêm: Tải Game Wormate.Io

Return Values

If output exists prior to calling this function, setcookie() will fail và return false. If setcookie() successfully runs, it will return true. This does not indicate whether the user accepted the cookie.

$value "something from somewhere";

setcookie("TestCookie"$valuetime()+3600);  /* expire in 1 hour */

lưu ý that the value portion of the cookie will automatically be urlencoded when you skết thúc the cookie, & when it is received, it is automatically decoded và assigned to a variable by the same name as the cookie name. If you don't want this, you can use setrawcookie() instead. To see the contents of our chạy thử cookie in a script, simply use one of the following examples:

// Print an individual cookie
echo $_COOKIE<"TestCookie">;

// Another way to debug/test is to view all cookies