PHP设置cookie跳转页面后cookie值丢失
本帖最后由 opopen 于 2015-08-12 22:54:00 编辑 问题就是,我输入正确的用户名和密码,还是照样让我重新登陆
在index.php页面获取不到cookie里面的值
主页是判断是否设置用户名,没有设置就返回设置,大牛们帮忙看下什么情况!
我贴一下代码:
//Login.php
//清空cookies
function clearCookies(){
setCookie('username','',time()-3600); //删除cookies中标识符为username的变量的值
setCookie('isLogin','',time()-3600); //删除cookies中标识符为isLogin的变量的值
setCookie('userpass','',time()-3600); //删除cookies中标识符为userpass的变量的值
}
//判断用户是否登录
//if($_GET['action']=='login')
// {
clearCookies();
$inputname = $_GET['loginname'];
$inputpass = $_GET['loginpass'];
echo '获取到用户名后立即
输出'.$inputname.'';
echo '获取到密码后立即输出'.$inputpass.'';
//插入数据库
$host = "localhost";
$user = "root";
$password = "";
$dbname = "test";
$connection = mysql_connect($host,$user,$password);
//echo $connection;
if($connection)
{
//$password = md5($password);
echo '此处是密码'.$inputpass.'';
echo '此处是用户名'.$inputname.'';
mysql_select_db($dbname,$connection);
echo "数据库链接成功了".'';
$sql = " SELECT * FROM `user_info` where `uname`='$inputname' and `upass` = '$inputpass' ";
echo $sql;
$result = mysql_query($sql);
if($result)
{
$row = mysql_fetch_array($result);
if($row)
{
var_dump($row);
setcookie("username",$row['uname'],time()+3600*24,'/');
setcookie("userpass",$row['upass'],time()+3600*24,'/');
setCookie('isLogin','login',time()+3600*24,'/');
//echo $_COOKIE["isLogin"];
//echo $checklogin;
//echo $checkloginvalue;
//$home_url = 'index.php';
mysql_close($connection);
//echo "";
header("location:index.php");
}else
{
die("登录失败");
}
}
}
else{
echo "数据库链接失败";
}
//}
?>
接下来是主页:index.php
$username = $_COOKIE["username"];
var_dump($username);
echo '此处
输出Cookie里面的用户名'.$username;
if($username=="" || $username==null)
{
echo"我进来了";
//header("location:login.html");
exit;
}
?>
------解决思路----------------------我试了下,可以登陆的,cookie也写入成功了,截图给你看