Home  >  Article  >  Backend Development  >  PHP5中Cookie小结(一)

PHP5中Cookie小结(一)

WBOY
WBOYOriginal
2016-06-13 10:39:221526browse

PHP5中Cookie总结(一)

一. cookie的配置与应用


设置cookie

bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] );

?

删除cookie

bool setcookie(name, "");

?

参数说明举例
name Cookie 的名字。 使用 $_COOKIE['cookiename'] 调用名为 cookiename 的 cookie。
value Cookie 的值。此值保存在客户端,不要用来保存敏感数据。 假定 name 是 'cookiename',可以通过 $_COOKIE['cookiename'] 取得其值。
expire Cookie 过期的时间。这是个 Unix 时间戳,即从 Unix 纪元开始的秒数。换而言之,通常用 time() 函数再加上秒数来设定 cookie 的失效期。或者用 mktime()来实现。 time()+60*60*24*30 将设定 cookie 30 天后失效。如果未设定,cookie 将会在会话结束后(一般是浏览器关闭)失效。
path Cookie 在服务器端的有效路径。 如果该参数设为 '/' 的话,cookie 就在整个 domain 内有效,如果设为 '/foo/',cookie 就只在 domain 下的 /foo/ 目录及其子目录内有效,例如 /foo/bar/。默认值为设定 cookie 的当前目录。
domain 该 cookie 有效的域名。 要使 cookie 能在如 example.com 域名下的所有子域都有效的话,该参数应该设为 '.example.com'。虽然 . 并不必须的,但加上它会兼容更多的浏览器。如果该参数设为 www.example.com 的话,就只在 www 子域内有效。细节见 Cookie 规范中的 tail matching。
secure 指明 cookie 是否仅通过安全的 HTTPS 连接传送。当设成 TRUE 时,cookie 仅在安全的连接中被设置。默认值为 FALSE 01

?

和其它标头一样,cookie 必须在脚本的任何其它输出之前发送(这是协议限制)。这需要将本函数的调用放到任何输出之前,包括 标签以及任何空格。如果在调用 setcookie() 之前有任何输出,本函数将失败并返回 FALSE。如果 setcookie() 函数成功运行,将返回 TRUE。这并不说明用户是否接受了 cookie。

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