Home  >  Article  >  Backend Development  >  请问这段JS 代码如何用 PHP 来写?谢谢

请问这段JS 代码如何用 PHP 来写?谢谢

WBOY
WBOYOriginal
2016-06-20 12:45:05718browse

var cookieString = new String(document.cookie) 
var cookieHeader = 'test='
var beginPosition = cookieString.indexOf(cookieHeader) 
if (beginPosition document.write("我不知道");
var Then = new Date()     
Then.setTime(Then.getTime() + 60*1000 ) 
document.cookie = 'test=yes;expires='+ Then.toGMTString()


回复讨论(解决方案)

$cookie = $_COOKIE['name'];//接收指定cookie
$header = 'test=';
//没有找到指定的内容
if( strpos($cookie,$header) === false){
echo '我不知道';
//重新设置过期时间
$_COOKIE('name',值,过期时间);
}

$cookie = $_COOKIE['name'];//接收指定cookie
$header = 'test=';
//没有找到指定的内容
if( strpos($cookie,$header) === false){
echo '我不知道';
//重新设置过期时间
$_COOKIE('name',值,过期时间);
}



这里的cookie不指定怎么做?上面JS的cookie就是打开浏览器有cookie而已 不需要值

凡事都有个度,php 不能代替 js 完成的工作

你的那段 js 代码完成的是这样的工作:如果名为 test 的 cookie 变量不存在,则创建他
等价的 php 代码是 if(! isset($_COOKIE['test'])) setcookie('test', 'yes', time()+60);

但是 js 是运行于 2 次 php 之间的,于是效果也是不一样的

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