首页 >后端开发 >php教程 >在一个新的页面怎么获取已经登陆的用户名呢?

在一个新的页面怎么获取已经登陆的用户名呢?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原创
2016-06-06 20:18:26959浏览

直接这样$_COOKIE['username'] 不能打印出来登陆的用户名啊 我想用这个用户名怎么办呢?

登录页

<code><?php
$pdo=new PDO("mysql:host=localhost;dbname=t1;","root","");
if(isset($_POST['submit'])){

 $stmt=$pdo->prepare("select id,username,password from user where username=? and password=?");
 $stmt->execute(array($_POST['username'],($_POST['password'])));
 if($stmt->rowCount()>0){
 list($id,$username)=$stmt->fetch(PDO::FETCH_NUM);
   $time=time()+24*60*60;
   setCookie("uid",$id,$time,"/");
   setCookie("username",$username,$time,"/");
   setCookie("isLogin",1,$time,"/");
   echo '<script>location="blog.php"</script>';
 }
 else{
     echo "登陆失败";
 }
}
?>
<html>
<head>
</head>
<body>
<form action="login.php" method="post">

用户名:<input type="text" name="username">
密码:<input type="text" name="password">
<button type="submit" name="submit">登录</button>
</form>
</body>
</html></code>

回复内容:

直接这样$_COOKIE['username'] 不能打印出来登陆的用户名啊 我想用这个用户名怎么办呢?

登录页

<code><?php
$pdo=new PDO("mysql:host=localhost;dbname=t1;","root","");
if(isset($_POST['submit'])){

 $stmt=$pdo->prepare("select id,username,password from user where username=? and password=?");
 $stmt->execute(array($_POST['username'],($_POST['password'])));
 if($stmt->rowCount()>0){
 list($id,$username)=$stmt->fetch(PDO::FETCH_NUM);
   $time=time()+24*60*60;
   setCookie("uid",$id,$time,"/");
   setCookie("username",$username,$time,"/");
   setCookie("isLogin",1,$time,"/");
   echo '<script>location="blog.php"</script>';
 }
 else{
     echo "登陆失败";
 }
}
?>
<html>
<head>
</head>
<body>
<form action="login.php" method="post">

用户名:<input type="text" name="username">
密码:<input type="text" name="password">
<button type="submit" name="submit">登录</button>
</form>
</body>
</html></code>

你得先在登录成功的时候写一下COOKIE,写完了再访问新的页面,你的php代码才能读到它

没人啊。。。。。。。。。。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn