Home >Daily Programming >PHP Knowledge >How does PHP use session to record user login information? (Pictures + Videos)
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 = 'demo'; $host = '127.0.0.1'; $user = 'root'; $password = 'root'; $dsn = "mysql:host=$host;dbname=$dbName"; $pdo = new PDO($dsn, $user, $password); function sql($table, $field = '*', $where = '') { global $pdo; $sql = 'select' . ' ' . $field . ' ' . 'from' . ' ' . $table . ' where ' . $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['username']; @$pas = $_POST['password']; $row = sql('user', '*', "username = '$name'"); if (!$row) { return "用户名不存在!请检查用户名~~"; } if ($row['password'] == $pas) { $_SESSION['username'] = "$name"; echo "<script> alert('登录成功!正在跳转...') </script>"; echo "<a href='index.php'>如果跳转失败请点击跳转~~</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['username']; if ($name) { echo "<script> alert(\"尊敬的$name ,欢迎回来!!\"); </script>"; }else{ echo "<script> alert('您还尚未登录!请返回登录~~') </script>"; echo "<a href='index.php'>如果跳转失败请点击跳转~~</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:
Click login to log in. , jump to check.php.
Click OK
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:
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!