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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Freigeben: 2016-06-06 20:18:26
Original
973 Leute haben es durchsucht

直接这样$_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>
Nach dem Login kopieren
Nach dem Login kopieren

回复内容:

直接这样$_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>
Nach dem Login kopieren
Nach dem Login kopieren

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

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

Verwandte Etiketten:
php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Aktuelle Ausgaben
PHP-Datenerfassung?
Aus 1970-01-01 08:00:00
0
0
0
PHP-Erweiterung intl
Aus 1970-01-01 08:00:00
0
0
0
Wie man PHP gut lernt
Aus 1970-01-01 08:00:00
0
0
0
Mehrere PHP-Versionen
Aus 1970-01-01 08:00:00
0
0
0
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage