


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="/static/imghw/default1.png" data-src="images/header.png" class="lazy" 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 id="这里是主页">这里是主页</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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
