Generally use session (SESSION) to determine whether to log in, as well as login user name and other information. (Recommended learning: PHP video tutorial)
//登录页面 <?php session_start(); if($_POST['user']==$user && $_POST['pwd']=$pwd){ //如果登录成功,生成对应的会话值。 $_SESSION['logined']=1; //判断是否已经登录的依据。 $_SESSION['user']=$user; //记录当前登录用户。 }else{ echo "登录失败,不记录SESSION值"; } ?>
Another page
<?php session_start(); //检测是否登录 if(isset($_SESSION['logined']) && $_SESSION['logined']){ //$_SESSION['logined']有设置,并且值为真,表示已经登录 echo "当前登录用户是: ".$_SESSION['user']; } ?>
SESSION represents the session value, he The survival time is the period during which the browser is opened, which means that once the browser is closed, the session value will disappear. And one characteristic of the session value is that during the session value life cycle, pages with the same domain name can access the session value generated by the domain name. For example, if Baidu knows the session value generated by the page, then in the newly opened Baidu Encyclopedia The page can also be accessed.
The above is the detailed content of How to query the current username in php. For more information, please follow other related articles on the PHP Chinese website!