Home  >  Article  >  Backend Development  >  How to set cookies in php header()

How to set cookies in php header()

青灯夜游
青灯夜游Original
2021-02-25 17:48:383615browse

In PHP, the header() function is used to send the original HTTP header to the client. Cookies can be set in the form of "header("Set-Cookie:xxxxxxxxxxxxxxxx",false);".

How to set cookies in php header()

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The header() function sends the original header to the client HTTP headers.

Header output cookie

set cookie function, if you want it to take effect, it also takes effect by setting the browser side through the header. So outputting through header is also feasible. So about writing several equivalent header operations, let’s compare and learn:

header("Set-Cookie:cookie_name1_cp=" . urlencode("浏览器关闭失效"));
setcookie("cookie_name1", "浏览器关闭失效");

How to set cookies in php header()

For more programming-related knowledge, please visit: Programming Video ! !

Note:

We know that both setcookie and header of php can set cookies. But when using headers, there is another thing to note: that is the order between header and setcookie, or even the order between header and header.

Header("Set-Cookie:") will clear: all the effects of header("Set-Cookie:") and setcookie(setrawcookie) before this statement is called. See the example below, where four cookies are set. But only one actually takes effect. Because the other three were washed away by the last one.

header("Set-Cookie:cookie_name1_cp=" . urlencode("浏览器关闭失效"));
setcookie("cookie_name1", "浏览器关闭失效");
setcookie("cookie_name3", "设置有效域名/https/httponly", time() + 3600*24, "/", $_SERVER['SERVER_NAME'], isset($_SERVER["HTTPS"]),true);
header("Set-Cookie:cookie_name3_cp=" . urlencode("设置有效域名/https/httponly") . "; expires=" . gmstrftime("%a, %d-%b-%Y %H:%M:%S GMT", time() + 3600*24) . "; Max-Age=3600; path=/; domain= ".$_SERVER['SERVER_NAME']."; httponly");

How to set cookies in php header()

How to set cookies in php header()

Solution:

Add a second parameter to the header, false. That is to say:

header("Set-Cookie:xxxxxxxxxxxxxxxx",false);

For example:

header("Set-Cookie:cookie_name3_cp=" . urlencode("设置有效域名/https/httponly") . "; expires=" . gmstrftime("%a, %d-%b-%Y %H:%M:%S GMT", time() + 3600*24) . "; Max-Age=3600; path=/; domain= ".$_SERVER['SERVER_NAME']."; httponly;");
header("Set-Cookie:cookie_name3_cp2=" . urlencode("设置有效域名/https/httponly") . "; expires=" . gmstrftime("%a, %d-%b-%Y %H:%M:%S GMT", time() + 3600*24) . "; Max-Age=3600; path=/; domain= ".$_SERVER['SERVER_NAME']."; httponly;",FALSE);

In this case, the header using the false parameter will not conflict with the original one. For details, please refer to the PHP function description of header:

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

Our false setting is the parameter $replace, which means not to replace the existing header of the same type. Note that it is "same type".

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of How to set cookies in php header(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn