初来乍到,PHP setcookie怎么能正确使用?我写的代码只能设置两个中的一个,怎么办?

WBOY
Release: 2016-06-23 13:09:11
Original
1116 people have browsed it

 <?php   session_start();   $flag="True";   setcookie("flags",$flag,time()+3600);   $name=$_SESSION['name'];   setcookie("uname",$name,time()+3600*24*30);  ?>
Copy after login


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>十六进制之火种</title><script type="text/javascript" language="javascript">flag="false";function getCookie(name){      var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));      if(arr != null){       return unescape(arr[2]);       }else{       return null;      }  }   if(!getCookie("flags")){  alert('失败');  }else{ alert(getCookie("flags"));}			if(!getCookie("uname")){  alert('失败');  }else{ alert(getCookie("uname"));}    dell=getCookie("uname");	flag=getCookie("flags");function change(){	if(flag == "True"){        document.getElementById("benph").innerHTML=dell;		var hp='<a href="logout.php">退出</a>';		document.getElementById("lan").innerHTML=hp;		}else{		var huawei='<a href="denglu.html">登录1</a>';	var htc='<a href="register.php">注册1</a>';	document.getElementById("benph").innerHTML=huawei;	document.getElementById("lan").innerHTML=htc;				}		}	var iID =setInterval("change()",1000);</script></head><body><p id="benph"><a href="denglu.html">登录</a></p><p id="lan"><a href="register.php">注册</a></p></body></html>
Copy after login


回复讨论(解决方案)

   function getCookie(cookie_name)
{
    var allcookies = document.cookie;
    var cookie_pos = allcookies.indexOf(cookie_name);   //索引的长度
 
    // 如果找到了索引,就代表cookie存在,
    // 反之,就说明不存在。
    if (cookie_pos != -1)
    {
        // 把cookie_pos放在值的开始,只要给值加1即可。
        cookie_pos += cookie_name.length + 1;      //这里容易出问题,所以请大家参考的时候自己好好研究一下
        var cookie_end = allcookies.indexOf(";", cookie_pos);
 
        if (cookie_end == -1)
        {
            cookie_end = allcookies.length;
        }
 
        var value = unescape(allcookies.substring(cookie_pos, cookie_end));         //这里就可以得到你想要的cookie的值了。。。
    }
    return value;
}

你获取cookie的方法有问题,用这个吧

谢谢我试试!

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!