为何在PHP中的cookie值读取失败?
2015-07-11 08:34
567 查看
在同一网站下,使用 setcookie("userLogin","TEST",time()+60*60*24); 语句设置了cookie值,在本页面能用echo $_COOKIE["userLogin"]; 读取,但转到同一网站的其他页面用echo $_COOKIE["userLogin"]; 就读取不了,是空值。请问是哪里出问题了?
答案:
如果你在子目录的文件中 setcookie 的话,那么只能在该子目录的程序中看到设置的 cookie 变量
鉴于网站文件数较多,不大可能都放在根目录中,所以应令 setcookie 的第 4 个参数为 /,即使cookie 在整个网站中都有效。
参考:
The path on the server in which the cookie will be available on. If set to
'/', the cookie will be available within the entire
'/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as
/foo/bar/ of
使用setcookie另需注意的是:在设置 setcookie 之前如果存在任何输出的,将会令setcookie失效,此种情况,可以在输出前面使用 ob_start()函数。
参考:If output exists prior to calling this function, setcookie() will fail and return
答案:
如果你在子目录的文件中 setcookie 的话,那么只能在该子目录的程序中看到设置的 cookie 变量
鉴于网站文件数较多,不大可能都放在根目录中,所以应令 setcookie 的第 4 个参数为 /,即使cookie 在整个网站中都有效。
参考:
path
The path on the server in which the cookie will be available on. 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.
使用setcookie另需注意的是:在设置 setcookie 之前如果存在任何输出的,将会令setcookie失效,此种情况,可以在输出前面使用 ob_start()函数。
参考: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.
相关文章推荐
- Windows 7或者 server 小技巧 添加 IIS和FTP 组件功能
- Windows 7或者 server 小技巧 添加 IIS和FTP 组件功能
- PB Post到PHP接口时无法从_POST数组取值
- 【只需3步】Linux php的安装与配置[源码安装]
- PHP数组操作类实例
- php实现源代码加密的方法
- php实现模拟post请求用法实例
- PHP中文字符串截取类
- 《PHP扩展学习系列》系列技术文章整理收藏
- 设置IIS下PHP环境的DOCUMENT_ROOT
- 使用OutPutStream写数据的问题及其它
- 安装YII
- Atitit.执行cmd 命令行 php
- Atitit.执行cmd 命令行 php
- Atitit. 脚本语言的断点单步调试的设计与实现 attialx 总结 php 参照java
- Atitit. 脚本语言的断点单步调试的设计与实现 attialx 总结 php 参照java
- atitit.php中的dwr 设计模式
- atitit.php中的dwr 设计模式
- PHP安全编程:register_globals的安全性 全局变量注册
- php中static静态关键字的使用