cookie是设置在客户端的,设置cookie的代码是放在服务器端,php,js到底怎么设置cookie的呢。
一,用纯php来测试cookie
- <?php
- $time = date("Y-m-d H:i:s",time());
- setcookie("test", $time,time()+3600);
- echo "=====1====<br>";
- echo $time;
- echo "<br>=====2====<br>";
- print_r($_COOKIE);
- ?>
第一次运行,请求的结果如下
- =====1====
- 2012-10-19 04:55:11
- =====2====
- Array ( )
为什么这里没有取到cookie呢?第一次运行的时候,将cookie信息(包括过期时间,不要认为cookie的过期时间,根客户端的系统时间有关)写入header,准备传给客户端,而读的cookie,注意这时读的不是刚刚写入header准备 发送给客户端的,而是访问这个程序时从客户端传来的cookie,因为第一次运行,以前没有写cookie呢,所以这时cookie是空的。
我们可以用firebug来看一下。
- 响应头信息原始头信息
- Connection Keep-Alive
- Content-Length 61
- Content-Type text/html
- Date Fri, 19 Oct 2012 04:55:11 GMT
- Keep-Alive timeout=5, max=100
- Server Apache/2.2.6 (Win32) PHP/5.2.5
- Set-Cookie test=2012-10-19+04%3A55%3A11; expires=Fri, 19-Oct-2012 05:55:11 GMT
- X-Powered-By PHP/5.2.5
- 请求头信息原始头信息
- Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Encoding gzip, deflate
- Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
- Cache-Control max-age=0
- Connection keep-alive
- Host localhost
- User-Agent Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0
请求的头信息里面,根本没有cookie的选项,所以这时是取不到cookie的。
第二次请求,请求的结果如下
- =====1====
- 2012-10-19 04:56:36
- =====2====
- Array ( [test] => 2012-10-19 04:55:11 )
在查看一下头信息
- 响应头信息原始头信息
- Connection Keep-Alive
- Content-Length 95
- Content-Type text/html
- Date Fri, 19 Oct 2012 04:56:36 GMT
- Keep-Alive timeout=5, max=100
- Server Apache/2.2.6 (Win32) PHP/5.2.5
- Set-Cookie test=2012-10-19+04%3A56%3A36; expires=Fri, 19-Oct-2012 05:56:36 GMT
- X-Powered-By PHP/5.2.5
- 请求头信息原始头信息
- Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Encoding gzip, deflate
- Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
- Cache-Control max-age=0
- Connection keep-alive
- Cookie test=2012-10-19+04%3A55%3A11 //这个时候就有cookie了,并且是第一次运行的值
- Host localhost
- User-Agent Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0
单单从代码上面看,你可能认为cookie还没有设置成功呢。如果我想设置cookie后,立马要用,php又取不到。js会不会也是这样呢。
二,纯js来测试cookie
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <script type="text/javascript">
- //get cookie
- function getCookie(name) {
- var nameEQ = name + "=";
- var ca = document.cookie.split(';');
- for(var i=0;i < ca.length;i++) {
- var c = ca[i];
- while (c.charAt(0)==' ') {
- c = c.substring(1,c.length);
- }
- if (c.indexOf(nameEQ) == 0) {
- return unescape(c.substring(nameEQ.length,c.length));
- }
- }
- return false;
- }
- //clear cookie
- function clearCookie(name) {
- setCookie(name, "", -1);
- }
- //set cookie
- function setCookie(name, value, seconds) {
- seconds = seconds || 0;
- var expires = "";
- if (seconds != 0 ) {
- var date = new Date();
- date.setTime(date.getTime()+(seconds*1000));
- expires = "; expires="+date.toGMTString();
- }
- document.cookie = name+"="+escape(value)+expires+"; path=/";
- }
- setCookie("test1",new Date().toLocaleString(),3600);
- alert(getCookie('test1'));
- </script>
- </body>
- </html>
为了一防万一,在进行下一次测试时,清除一下浏览器的缓存。
根据上面php存取cookie的方式来说,js也一样,第一次也取不到值才对。因为我只不过把php文件,换成了html文件了而已,但是cookie是可以取到的。这底是为什么呢,难道是二种脚本语言设计的不同?并且用firebug看头信息,请求头信息和响应头信息都没有cookie信息。
js 设置cookie的头信息
三,php结合js来存取cookie
- <?php
- $time = date("Y-m-d H:i:s",time());
- setcookie("test2", $time,time()+3600);
- echo "=====1====<br>";
- echo $time;
- echo "<br>=====2====<br>";
- ?>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <script type="text/javascript">
- //get cookie
- function getCookie(name) {
- var nameEQ = name + "=";
- var ca = document.cookie.split(';');
- for(var i=0;i < ca.length;i++) {
- var c = ca[i];
- while (c.charAt(0)==' ') {
- c = c.substring(1,c.length);
- }
- if (c.indexOf(nameEQ) == 0) {
- return unescape(c.substring(nameEQ.length,c.length));
- }
- }
- return false;
- }
- //clear cookie
- function clearCookie(name) {
- setCookie(name, "", -1);
- }
- //set cookie
- function setCookie(name, value, seconds) {
- seconds = seconds || 0;
- var expires = "";
- if (seconds != 0 ) {
- var date = new Date();
- date.setTime(date.getTime()+(seconds*1000));
- expires = "; expires="+date.toGMTString();
- }
- document.cookie = name+"="+escape(value)+expires+"; path=/";
- }
- alert(getCookie('test2'));
- </script>
- </body>
- </html>
请求一次,js是可以取到cookie的。我查看了一下头信息,根纯php设置访问时候产生的头信息是一样的。我得出的结论php和js存,取cookie的设计思想不一样。可惜我c/c++不行啊,不然就看看源码了。
元芳,你怎么看?
来源: http://blog.51yip.com/php/1468.html