Home  >  Article  >  Daily Programming  >  How does PHP use session to record user login information? (Pictures + Videos)

How does PHP use session to record user login information? (Pictures + Videos)

藏色散人
藏色散人Original
2018-10-15 14:08:387890browse

This article mainly introduces you to the specific method of recording user login information using session in PHP.

The issue of session implementation in PHP recording user login information is also one of the more common test points in PHP interview questions and is a knowledge point that PHP learners must master.

It may be difficult for newbies who are just getting started with PHP. Then in the previous article [How to store and delete variables in session in PHP], I also introduced the basic meaning of session in PHP. Friends who need it can choose to refer to it.

Below we will use specific code examples to introduce in detail the specific method of recording user login information in session in PHP.

1. Simple login interface code example:

login.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>登录</title>
    <style type="text/css">
        body {
            background: url(images/bg.png);
        }

        .clear {
            clear: both;
        }

        .login {
            width: 370px;
            margin: 100px auto 0px;
            text-align: center;
        }

        input[type="text"] {
            width: 360px;
            height: 50px;
            border: none;
            background: #fff;
            border-radius: 10px;
            margin: 5px auto;
            padding-left: 10px;
            color: #745A74;
            font-size: 15px;
        }

        input[type="checkbox"] {
            float: left;
            margin: 5px 0px 0px;
        }

        span {
            float: left;
        }

        .botton {
            width: 130px;
            height: 40px;
            background: #745A74;
            border-radius: 10px;
            text-align: center;
            color: #fff;
            margin-top: 30px;
            line-height: 40px;
        }
    </style>
</head>
<body>
<div class="login">
    <form action="check.php" method="post">
        <img  src="images/header.png" alt="How does PHP use session to record user login information? (Pictures + Videos)" ><br>
        <input type="text" name="username" placeholder="请输入用户名!" value=""><br>
        <input type="text" name="password" placeholder="请输入密码!" value=""><br>
        <input type="submit" class="botton" value="login">
    </form>
    <div class="clear"></div>
</div>
</body>

</html>

2. Simple PHP file code example for connecting to the database:

db.php

<?php

$dbName = &#39;demo&#39;;
$host = &#39;127.0.0.1&#39;;
$user = &#39;root&#39;;
$password = &#39;root&#39;;

$dsn = "mysql:host=$host;dbname=$dbName";
$pdo = new PDO($dsn, $user, $password);

function sql($table, $field = &#39;*&#39;, $where = &#39;&#39;)
{
    global $pdo;
    $sql = &#39;select&#39; . &#39; &#39; . $field . &#39; &#39; . &#39;from&#39; . &#39; &#39; . $table . &#39; where &#39; . $where;
    $data = $pdo->query($sql)->fetch();
    return $data;
}

Here we define a sql method to query the database table field and return data .

So if there are novices who don’t know how to connect PHP to the database, you can refer to this article[How to connect PHP to the Mysql database].

3. Code example to check user login information:

check.php

<?php
session_start();
include "db.php";
@$name = $_POST[&#39;username&#39;];
@$pas = $_POST[&#39;password&#39;];

$row = sql(&#39;user&#39;, &#39;*&#39;, "username = &#39;$name&#39;");
if (!$row) {
    return "用户名不存在!请检查用户名~~";
}

if ($row[&#39;password&#39;] == $pas) {
    $_SESSION[&#39;username&#39;] = "$name";
    echo "<script>
    alert(&#39;登录成功!正在跳转...&#39;)
</script>";
    echo "<a href=&#39;index.php&#39;>如果跳转失败请点击跳转~~</a>";
    header("Refresh:1;url=index.php");
}

Here we need to open the session and use include to introduce the database, and then use the if statement to judge the submitted data and submit the user name to the session for recording, that is, to judge whether the user name and password exist and are equal.

4. Example of page code that jumps after successful login:

index.php

<?php
echo "<h1>这里是主页</h1>";
session_start();
$name = $_SESSION[&#39;username&#39;];
if ($name) {
    echo "<script>
     alert(\"尊敬的$name ,欢迎回来!!\");
</script>";
}else{
    echo "<script>
    alert(&#39;您还尚未登录!请返回登录~~&#39;)
</script>";
    echo "<a href=&#39;index.php&#39;>如果跳转失败请点击跳转~~</a>";
    header("Refresh:1;url=login.html");
}

The above codes login.html, db.php, check.php and index.php are a simple program that uses session to record user login information.

We can test through browser access. First, we can enter the username and password on the login interface. The effect is as follows:

How does PHP use session to record user login information? (Pictures + Videos)

Click login to log in. , jump to check.php.

How does PHP use session to record user login information? (Pictures + Videos)

Click OK

How does PHP use session to record user login information? (Pictures + Videos)

If the jump fails, click the link in the above picture, if the jump is successful , it will jump directly to the index.php main page, as shown below:

How does PHP use session to record user login information? (Pictures + Videos)

How does PHP use session to record user login information? (Pictures + Videos)

##This article is about

Introduction to the specific method of recording user login information using sessions in #PHP. It has certain reference value. I hope it will be helpful to friends in need! If you want to learn more about PHP, you can follow the PHP Chinese website

PHP Video Tutorial

, everyone is welcome to refer to and learn!

The above is the detailed content of How does PHP use session to record user login information? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

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