cookie的path和domain参数实例解析

来源:互联网 发布:java 占位符替换 编辑:程序博客网 时间:2024/05/22 18:26

一句话概括两个参数含义各为:

path表示cookie所在的目录

domain表示的是cookie所在的域,默认为请求的地址


首先修改我们的 hosts 文件 我本机内网ip 192.168.1.162


一.

我们在web根目录下创建文件夹 cookietest 创建文件 index.php

<?phpsetcookie('t1','t1',time()+3600,'/','simael.php.com');setcookie('t2','t2',time()+3600,'/','php.com');setcookie('t3','t3',time()+3600,'/','m0sh1.php.com');setcookie('t4','t4',time()+3600,'/cookietest','simael.php.com');setcookie('t5','t5',time()+3600,'/cookietest','php.com');setcookie('t6','t6',time()+3600,'/cookietest','m0sh1.php.com');echo __FILE__;  //  E:\wamp\www\cookietest\index.phpecho '<br>';?>
访问 http://simael.php.com/cookietest/
结果:

没有 t3 t6 意味着 在域名 simael.php.com 下获取不到为 m0sh1.php.com 设置cookie 的

二.

访问 http://simael.php.com/ 结果:


没有 t4  t5 是因为设置 cookie时候设置了path的原因

三.

访问 http://simael.php.com/cookietest/index2.php 结果:


结果同(二)

四.

访问 http://simael.php.com/cookietest/test1/index.php  结果:


结果同(二)

五.

修改 /cookietest/index.php 代码

<?php//setcookie('t1','t1',time()+3600,'/','simael.php.com');//setcookie('t2','t2',time()+3600,'/','php.com');//setcookie('t3','t3',time()+3600,'/','m0sh1.php.com');//setcookie('t4','t4',time()+3600,'/cookietest','simael.php.com');//setcookie('t5','t5',time()+3600,'/cookietest','php.com');//setcookie('t6','t6',time()+3600,'/cookietest','m0sh1.php.com');setcookie('t7','t7',time()+3600,'/cookietest/test1','simael.php.com');setcookie('t8','t8',time()+3600,'/cookietest2/test1','simael.php.com');echo __FILE__;echo '<br>';?>
访问 http://simael.php.com/cookietest/index.php 结果:



没有 t7 t8 证明上一级目录 不能获取到下一级目录设置的cookie
但是有个没弄明白的地方:
上面截图是 chorme 浏览器的效果
再看下 firefox

cookies 中有t7 报错是我打印 $_COOKIE['t7'] 的结果 即获取不到t7 再看看控制台

可见也没有 t7 只有在 cookies 选项卡下可以看到 t7 但是是获取不到的(这个内容只是顺便提一下 - -!)

六.
访问 http://simael.php.com/cookietest/test1/index.php 结果:


这时候就看到了 t7 说明在上一级的index.php 中cookie设置生效了只不过只有在相应的path下才能获取到

七.
访问 http://m0sh1.php.com/cookietest/index2.php

只有 t2 t5 说明在 simeal.php.com 中为 域名 m0sh1.php.com 设置cookie 是不生效的,同时因为 cookie 设置的domain
使得在 这个域名下是获取不到 simeal域名下的cookie
但是能获取到 .php.com 域下的cookie 信息 即设置 cookie的 domain 为 php.com
可以在任意  *.php.com 域下获取到cookie信息





1 0
原创粉丝点击