How to query the current username in php

(*-*)浩
Release: 2023-02-26 17:38:01
Original
3683 people have browsed it

How to query the current username in php

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[&#39;user&#39;]==$user && $_POST[&#39;pwd&#39;]=$pwd){
    //如果登录成功,生成对应的会话值。
    $_SESSION[&#39;logined&#39;]=1;   //判断是否已经登录的依据。
    $_SESSION[&#39;user&#39;]=$user;  //记录当前登录用户。
}else{
    echo "登录失败,不记录SESSION值";
}
?>
Copy after login

Another page

<?php
session_start();
//检测是否登录
if(isset($_SESSION[&#39;logined&#39;]) && $_SESSION[&#39;logined&#39;]){
   //$_SESSION[&#39;logined&#39;]有设置,并且值为真,表示已经登录
   echo "当前登录用户是: ".$_SESSION[&#39;user&#39;];
}
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template