Php Setcookie Not Working

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 setcookie not working

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

setcookie() defines a cookie to lớn 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 khổng lồ any output, including & tags as well as any whitespace.

Once the cookies have sầu been mix, 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 sầu 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; bởi 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 khổng lồ expire. Or you might use mktime() . time()+60*60*24*30 will set the cookie khổng lồ expire in 30 days. If phối to 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 does this conversion internally.


The path on the server in which the cookie will be available on. If mix to lớn '/', the cookie will be available within the entire domain name. If mix to '/foo/', the cookie will only be available within the /foo/ directory & all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.

domain name

The (sub)domain name that the cookie is available lớn. Setting this to lớn a subdomain name (such as '') will make the cookie available to that subtên miền and all other sub-domains of it (i.e. To make the cookie available khổng lồ the whole tên miền (including all subdomains of it), simply set the value to lớn the domain name 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 to lớn true, the cookie will only be mix if a secure connection exists. On the server-side, it's on the programmer to send this kind of cookie only on secure connection (e.g. with respect khổng lồ $_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 lớn reduce identity theft through XSS attacks (although it is not supported by all browsers), but that clayên is often disputed. true or false


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 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 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 phối.

Xem thêm: Spirits Là Rượu Gì ? Những Điều Có Thể Bạn Chưa Biết Về Rượu?

Return Values

If output exists prior to 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.

$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, và when it is received, it is automatically decoded & assigned to lớn 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 test 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