search
Homephp教程php手册PHP与JavaScript下的Cookie操作

下面的例子列出几种情形交互场景,列出JS和php交互的方法。总结下,以免日后再为cookie问题困扰。 setcookie.php View Code getcookie.php View Code 总结: php用自身函数读取php 的cookie,没有任何障碍,无需解码处理。 js采用cookie.js方法读取js 的cooki

下面的例子列出几种情形交互场景,列出JS和php交互的方法。总结下,以免日后再为cookie问题困扰。

setcookie.php

 

PHP与JavaScript下的Cookie操作View Code

getcookie.php

PHP与JavaScript下的Cookie操作View Code

总结:

  1. php用自身函数读取php 的cookie,没有任何障碍,无需解码处理。
  2. js采用cookie.js方法读取js 的cookie,没有任何障碍,无需解码处理。
  3. js读取php的中文cookie,需要做 "decodeURIComponent (escape(php_cn_ck)) "函数处理
  4. php读取js的中文cookie 需要做 "unescape()" 函数处理

cookie.js

	var Cookies = {}; 
	/** 
	* 设置Cookies 
	*/ 
	Cookies.set = function(name, value){ 
		var argv = arguments; 
		var argc = arguments.length; 
		var expires = (argc > 2) ? argv[2] : null; 
		if(expires != null){ 
        	var exp   = new Date(); 
	 		exp.setTime(exp.getTime() + 8*3600 + expires); 
		} 
		alert(exp.toGMTString()); 
		var path = (argc > 3) ? argv[3] : '/'; 
		var domain = (argc > 4) ? argv[4] : null; 
      	var secure = (argc > 5) ? argv[5] : false; 
		document.cookie = name + "=" + escape (value) + 
		((expires == null) ? "" : ("; expires=" + exp.toGMTString())) + 
		((path == null) ? "" : ("; path=" + path)) + 
		((domain == null) ? "" : ("; domain=" + domain)) + 
		((secure == true) ? "; secure" : ""); 
	}; 
	/** 
	* 读取Cookies 
	*/ 
	Cookies.get = function(name){ 
		var arg = name + "="; 
		var alen = arg.length; 
		var clen = document.cookie.length; 
		var i = 0; 
		var j = 0; 
		while(i 

<p>4 function.php</p>
<p><img  src="/static/imghwm/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnblogs.com%2FOutliningIndicators%2FContractedBlock.gif&refer=http%3A%2F%2Fwww.cnblogs.com%2Fheheisme%2Fp%2F3384827.html" class="lazy" alt="PHP与JavaScript下的Cookie操作" ><span>View Code</span></p>
<p> </p>
<p>  </p>
<p> </p>
<p>http://www.nowamagic.net/webdesign/webdesign_CookieInPhpJavascript.phpPHP与JavaScript下的Cookie操作</p>


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.