Home >Backend Development >PHP Tutorial >在一个新的页面怎么获取已经登陆的用户名呢?

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

WBOY
WBOYOriginal
2016-06-06 20:18:26943browse

直接这样$_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 "登陆失败";
 }
}
?>




<form action="login.php" method="post">

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

</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 "登陆失败";
 }
}
?>




<form action="login.php" method="post">

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

</code>

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

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

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