Home >Backend Development >PHP Tutorial >PHP设置cookie跳转页面后cookie值丢失解决办法

PHP设置cookie跳转页面后cookie值丢失解决办法

WBOY
WBOYOriginal
2016-06-13 12:25:292244browse

PHP设置cookie跳转页面后cookie值丢失

本帖最后由 opopen 于 2015-08-12 22:54:00 编辑 问题就是,我输入正确的用户名和密码,还是照样让我重新登陆
在index.php页面获取不到cookie里面的值
主页是判断是否设置用户名,没有设置就返回设置,大牛们帮忙看下什么情况!

我贴一下代码:
<br /><br /><?php<br />//Login.php<br />//清空cookies<br />function clearCookies(){<br />		setCookie('username','',time()-3600);  //删除cookies中标识符为username的变量的值<br />		setCookie('isLogin','',time()-3600);      //删除cookies中标识符为isLogin的变量的值 <br />		setCookie('userpass','',time()-3600);      //删除cookies中标识符为userpass的变量的值 <br />}<br />//判断用户是否登录<br />	//if($_GET['action']=='login')<br />//	{<br />		clearCookies();<br />		$inputname = $_GET['loginname'];<br />		$inputpass = $_GET['loginpass'];<br />		echo '获取到用户名后立即输出'.$inputname.'</br>';<br />		echo '获取到密码后立即输出'.$inputpass.'</br>';<br />		//插入数据库<br />		$host = "localhost";<br />		$user = "root";<br />		$password = "";<br />		$dbname = "test";<br />		$connection = mysql_connect($host,$user,$password);<br />		//echo $connection;<br />		if($connection)<br />		{<br />			//$password = md5($password);<br />			echo '此处是密码'.$inputpass.'</br>';<br />			echo '此处是用户名'.$inputname.'</br>';<br />			<br />			mysql_select_db($dbname,$connection);<br />			echo "数据库链接成功了".'</br>';<br />			$sql = "   SELECT * FROM  `user_info`  where `uname`='$inputname' and `upass` = '$inputpass' ";<br />			echo $sql;<br />			$result = mysql_query($sql);<br />				if($result)<br />				{<br />					$row = mysql_fetch_array($result);<br />					if($row)<br />					{<br />						var_dump($row);<br />						setcookie("username",$row['uname'],time()+3600*24,'/');<br />						setcookie("userpass",$row['upass'],time()+3600*24,'/');<br />						setCookie('isLogin','login',time()+3600*24,'/'); <br />						//echo $_COOKIE["isLogin"];<br />						//echo $checklogin;<br />						//echo $checkloginvalue;<br />						//$home_url = 'index.php';<br />						mysql_close($connection);<br />						//echo "<script language='javascript'> alert(\"欢迎您,${row}['uname']\");window.location='../index.html'</script>";<br />						header("location:index.php");<br />						<br />					}else<br />					{<br />						die("登录失败");<br />					}<br />				}<br />		}<br />		else{<br />				echo "数据库链接失败";<br />		}<br />		<br />	//}<br />	<br />?><br />

接下来是主页:index.php
<br /><?php<br /><br />	$username = $_COOKIE["username"]; <br />	var_dump($username);<br />	echo '此处输出Cookie里面的用户名'.$username;<br />	if($username=="" || $username==null)<br />	{<br />					echo"我进来了";<br />					//header("location:login.html");<br />					<br />					exit;<br />	}<br />?><br />

------解决思路----------------------
我试了下,可以登陆的,cookie也写入成功了,截图给你看
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