创建cookie:
1.如果不指明cookie路径php将创建的cookie保存在当前上下文的路径中,比如我在/a/test.php中创建一个cookie,那么Php将在/a目录下生成一个cookie文件,而只有在/a目录下的文件才能访问此cookie
2.一般做法是将路径设为‘/’,这样创建的cookie将在整个域名下都有效。
引用手册上一段:
If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.
3.如果不指明cookie失效时间的话,cookie将在关闭浏览器后失效
If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
4.cookie生成后是伴随在http包中发布出来,所以在生成cookie后不能立马获得cookie值,只能通过下次请求获得!因为那时cookie已经夹在http包中。
销毁cookie:
很多情况下,我们会将cookie和session结合使用。所以必定会牵涉到cookie和session的销毁问题。这就牵涉到先销毁谁的问题,访问网站的来客会被分配一个唯一的标识符-sessionid关联用户创建的cookie,所以得先销毁cookie再销毁session,
session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie.
In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.
<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
// Finally, destroy the session.
session_destroy();
?>
分享到:
相关推荐
php自定义session,cookie,不依赖客户端及服务器设置,非常实用,自编,0分贡献,有什么意见建议可留言回复我,本人菜鸟,勿喷
1、Cookie和Session简介与区别Session信息是存放在server端,但session id是存放在client cookie的,当然php的session存放方法是多样化的,这样就算禁用cookie一样可以跟踪Cookie是完全保持在客户端的如:IE firefox...
php中session与cookie的区别以及配置使用
//访问 cookie变量 和变通变量一样 setcookie(’user_id’,0,time()-1);//删除cookie变量 // codie end // 该代码不可运行,只是将所有使用方法在这里列出,实际应该不同功能在不同页面使用,将在下面的例子中演示
基于PHP两种会话机制,Session和Cookie的相关实例,帮助大家更好理解PHP会话机制。如有错误,请提出
如果在Cookie中设置了"HttpOnly"属性,那么通过程序(JS脚本、Applet等)将无法读取到Cookie信息,这样能有效的防止XSS攻击。 对于以上两个属性, 首先,secure属性是防止信息在传递的过程中被监听捕获后信息泄漏,...
php_session与cookie会员登录验证实例
php.itcast.cn PHP课程-session和cookie的简单原理
本文较为详细的比较了php中session与cookie区别。分享给大家供大家参考。具体分析如下: 1、存放的位置 cookie保存在客户端,session保存在服务器端的文件系统/数据库/memcache等。 2、安全性 session因为保存有...
PHP会话控制SESSION与COOKIE
网上商城购物车实现原理,PHP+Mysql实现,包含使用Cookie和Session两种实现方式的源代码。使用前请先导入数据表备份到你的mysql数据库中,然后修改config.php文件中的数据库配置。供PHPer学习交流,有任何意见或建议...
在PHP中,cookie和session的使用
1. PHP的COOKIE cookie 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制。PHP在http协议的头信息里发送cookie, 因此setcookie() 函数必须在其它信息被输出到浏览器前调用,这和对header() 函数的限制类似...
1、Cookie和Session简介与区别 Session信息是存放在server端,但session id是存放在client cookie的,当然php 的session存放方法是多样化的,这样就算禁用cookie一样可以跟踪 Cookie是完全保持在客户端的如:IE fire...
php session登录验证实例与 cookie登录验证实例
韩顺平老师的讲解记录的笔记。比老师的详细很多
一次搞明白 Session、Cookie、Token,面试问题全搞定.pdf 面试中的问题一站式全搞定 在也不用头疼面试官的问题了。
cookie和session是PHP程序设计中非常重要的技巧。深入理解并掌握cookie和session的应用是进行PHP程序设计的基础。本文就以实例形式来分析一下二者之间的区别。具体分析如下: 1.Cookie cookie 是一种在远程浏览器端...
在PHP中浅谈Cookie与Session.pdf
软件介绍 1、Cookie和Session简介与区别 Session信息是存放在server端,但session id是存放在client cookie的,当然php 的session存放方法是多样化的,这样就算禁用cookie一样可以跟踪 Cookie是完全保持在客户端...