Home > php教程 > php手册 > body text

关于php存储cookie时path存放路径放的详细介绍

WBOY
Release: 2016-06-06 19:47:10
Original
1571 people have browsed it

以前在存储cookie时,没有用到path这个参数,最近开发中在存储cookie时,要用到path参数来决定存储的路径,结果一开始的时候一直取不到,到最后详细看了一下path参数的介绍后,才顿悟。分享几个小例子,对这一块不是太明白的可以在自己的机子上测试一下。 se

以前在存储cookie时,没有用到path这个参数,最近开发中在存储cookie时,要用到path参数来决定存储的路径,结果一开始的时候一直取不到值,到最后详细看了一下path参数的介绍后,才顿悟。分享几个小例子,对这一块不是太明白的可以在自己的机子上测试一下。

setcookie.php(路径:localhost/test):

(1):不带path参数时:

setcookie('testcookie', 'cookievalue', time() + 86400);

print_r($_COOKIE['testcookie']);

这种情况在setcookie.php本页面就可以取到值

(2):带path参数时:

setcookie('xiaokang', 'cookievalue', time() + 86400, '/test/mycookie');

print_r($_COOKIE['xiaokang']);

这种情况在setcookie.php本页面就不能取到值.这是为什么呢?

解释:虽然设置cookie的这个页面url不一定是以/test/mycookie开头,但是一定得在请求以/test/mycookie开头的页面时才会发回cookie.

getcookie.php:(其路径为:localhost/test/mycookie/getcookie.php)

print_r($_COOKIE);// 这里就能取到值了。

注释:如果在存储cookie的时候加上了localhost,则注销的时候也一定要加上localhsot,如:setcookie('xiaokang', '', time() - 3600, '/test//mycookie',localhost);

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!