Home > Backend Development > PHP Problem > How to display name after php login

How to display name after php login

Release: 2023-02-27 16:18:01
Original
7756 people have browsed it

How to display name after php login

PHP session variables are used to store information about the user session (session), or to change the settings of the user session (session). Session variables store information for a single user and are available to all pages in the application.

We can use PHP to output the user name information stored in the session after logging in to display the name after php login.

Recommended: php server

How to display name after php login:

Login form:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
    <form action="dologin.php" method="post">
 username:<input type="text" name="username" /><br/>
 password:<input type="password" name="password" />
        <input type="submit" value="提交"/>
    </form>
</body>
</html>
Copy after login

php login code:

dologin.php

<?php
    session_start();
    $username = $_POST[&#39;username&#39;]?$_POST[&#39;username&#39;]:&#39;&#39;;
    $password = $_POST[&#39;password&#39;]?$_POST[&#39;password&#39;]:&#39;&#39;;
    $link = mysqli_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;root&#39;, &#39;test&#39;);
    mysqli_set_charset($link,&#39;utf8&#39;);
    $sql = "select password from user_dologin where username=&#39;".$username."&#39;";
    $res = mysqli_query($link,$sql);
    $result = mysqli_fetch_assoc($res);
 
    mysqli_close($link);
    if($result[&#39;password&#39;] == $password){
        $_SESSION[&#39;username&#39;] = $username;
        $_SESSION[&#39;password&#39;] = $password;//一般情况下session中不保存密码信息
        header("Location: index.php");
    }else{
        header("Location: login.html");//密码错误跳回登陆网页
    }
Copy after login

Name code displayed after login:

<?php
session_start();
echo "username:".$_SESSION[&#39;username&#39;]."<br/>";
echo "password:".$_SESSION[&#39;password&#39;];
Copy after login

Browser display effect:

username:zhangsan
password:123456
Copy after login

The above is the detailed content of How to display name after php login. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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