以jquery与php来读写cookie

Google到一篇文章,写得实在很简洁易懂,转来备忘

原文:MicroTut: Getting And Setting Cookies With jQuery & PHP

Cookies and PHP

Setting cookies

To create a cookie in PHP, you need to use the setcookie function. It takes a number of parameters (all except for the first are optional and can be omitted):

setcookie(
	'pageVisits',			// Name of the cookie, required
	$visited,				// The value of the cookie
	time()+7*24*60*60,		// Expiration time, set for a week in the future
	'/',					// Folder path the cookie will be available for
	'demo.tutorialzine.com'	// Domain to which the cookie will be bound
);

If you pass 0 as an expiration time (which is the default behavior) the cookie will be lost on browse restart. The “/” parameter indicates that it will be available for all directories of the domain (you can optionally bind a cookie to a single directory with something like /admin/ as a parameter).

There are two additional parameters that you could pass to the function, which are not given here. They are specified with a boolean value. The first one indicates that the cookie would be transferred only on a secure HTTPS connection, and the second that the cookie will not be accessible though JavaScript (introduced in PHP 5.2)

For most practical purposes, you would only need the first four parameters, omitting the rest.

Reading cookies

Reading a cookie with PHP is a lot simpler. All the cookies that were passed to the script are available in the $_COOKIE superglobal array. In our example, to read the cookie we would write the following code:

$visits = (int)$_COOKIE['pageVisits']+1;
echo "You visited this site: ".$visits." times";

It is a good place to note, that cookies set with setcookie are available in the $_COOKIE array on the next page load, which is something you should be aware of.

Deleting cookies

To delete cookies, just use setcookie and give it a time in the past as an expiration date.

setcookie(
	'pageVisits',
	$visited,
	time()-7*24*60*60,		// One week in the past. The cookie will be deleted
	'/',
	'demo.tutorialzine.com'
);

Cookies and jQuery

To use cookies with jQuery, you will need the special Cookie plugin.
Setting cookies

Setting cookies with the Cookie plug-in is quite intuitive:

$(document).ready(function(){

	// Setting a kittens cookie, it will be lost on browser restart:
	$.cookie("kittens","Seven Kittens");

	// Setting demoCookie (as seen in the demonstration):
	$.cookie("demoCookie",text,{expires: 7, path: '/', domain: 'demo.tutorialzine.com'});

	// "text" is a variable holding the string to be saved
});

Reading cookies

Reading a cookie is even simpler. Just call the $.cookie() function with a single cookie-name parameter, and the value of the cookie will be returned:

$(document).ready(function(){

	// Getting the kittens cookie:
	var str = $.cookie("kittens");

	// str now contains "Seven Kittens"
});

Deleting cookies

To delete a cookie, again use the $.cookie() function, but pass null as its second parameter.

$(document).ready(function(){

	// Deleting the kittens cookie:
	var str = $.cookie("kittens",null);

	// No more kittens
});

————————————分割线—————————————–

以下为原创部分,比上面的要糟糕得多……
目的是让服务器端得到客户端的时间,也就是必须用javascript语言获取的时间传给php语言。
我利用了cookie来实现,代码如下

<script type="text/javascript">
$(document).ready(function(){
	var date = new Date();
	var Offset = date.getTimezoneOffset()/60*(-1);
	$.cookie("TimeCookie",Offset,{expires: 7});
	$("#timer").html( date.toLocaleString() );
	});
</script>
<?php
	$j=time();
	$Offset= (int)$_COOKIE['TimeCookie'];
	echo "<span id=\"timer\"></span><br /><br />";
	$st=$j+$Offset*3600;
?>

以上代码的问题就在于php的执行必须在javascript之前,
以至于访问者第一次访问页面时php读取不到cookie里的时区信息,只能按照GMT标准时间计算(时区00:00)。
第二次访问页面时因为javascript已经把访问者的时区写入cookie,php可以正确读取时区,方可按照客户时区计算。
这问题目前解决不了,以后再研究吧……