请教这段JS 代码怎么用 PHP 来写?多谢

WBOY
Release: 2016-06-13 12:26:56
Original
762 people have browsed it

请问这段JS 代码如何用 PHP 来写?谢谢
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',值,过期时间);
}

------解决思路----------------------
凡事都有个度,php 不能代替 js 完成的工作

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!