• PHP 8 MySQL 教程:构建登录和用户身份验证系统


    PHP 8 MySQL Tutorial: Build Login and User Auth SystemHow to create a secure PHP user authentication and login system with MySQL database using procedural programming approach.https://www.positronx.io/build-php-mysql-login-and-user-authentication-system/

    在本教程中,我们将学习如何使用过程编程方法使用 MySQL 数据库创建安全的 PHP 8 用户身份验证和登录系统。在下面的演示中,您可以查看这个 PHP 8 登录系统教程的最终输出。

    用户认证是一种标准的安全机制,它允许被识别的用户访问任何数字应用程序或网站。它严格防止身份不明的用户访问网络或移动应用程序。

    我们在日常生活中使用 Facebook、Twitter、Gmail 等数字产品,我们几乎熟悉用户注册和登录机制。您可以在登录这些应用程序之前创建一个帐户,这样您就可以使用它们的功能。

    我们将学到什么:

    在本文中,我们将重点介绍使用 PHP 和 MySQL 构建完整的登录和注册系统所需的一些核心功能。

    • 使用 Bootstrap 4 创建登录和注册表单
    • 与 PHP 项目建立 MySQL 数据库连接
    • 在会话中管理用户数据
    • PHP服务器端验证
    • 处理错误消息
    • 使用 SwiftMailer 插件发送用户验证邮件
    • 使用密码哈希机制保护密码
    • 密码验证
    • 基于用户登录状态的 URL 重定向
    • 使用 PHP 会话显示登录用户的数据
    • 注销并销毁会话

    PHP 8 文件和文件夹结构

    打开MAMPXAMPP并启动您的 Web 服务器,然后转到htdocs文件夹并创建以下文件夹和文件,这些文件夹和文件对于开发我们的用户身份验证系统至关重要。

    1. \-- php-user-authentication
    2. |-- config
    3. |--- db.php
    4. |-- controllers
    5. |--- login.php
    6. |--- register.php
    7. |--- user_activation.php
    8. |-- css
    9. |--- style.css
    10. |-- lib
    11. |--- 3rd party plugins
    12. |-- dashboard.php
    13. |-- header.php
    14. |-- index.php
    15. |-- logout.php
    16. |-- signup.php
    17. |-- user_verification.php
    CSS
    复制

    在 MySQL 中创建数据库和表

    我们的本地 Web 服务器已启动并正在运行,请转到PHPMyAdmin

    首先创建数据库`your_database_name`

    `table_name`在 MySQL 数据库中创建表。

    您可以手动创建列,甚至可以从SQL选项卡执行以下 sql 脚本来创建具有以下值的列。

    1. CREATE TABLE `users` (
    2. `id` int(11) NOT NULL,
    3. `firstname` varchar(100) NOT NULL,
    4. `lastname` varchar(100) NOT NULL,
    5. `email` varchar(50) NOT NULL,
    6. `mobilenumber` varchar(50) NOT NULL,
    7. `password` varchar(255) NOT NULL,
    8. `token` varchar(255) NOT NULL,
    9. `is_active` enum('0','1') NOT NULL,
    10. `date_time` date NOT NULL
    11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    PL/SQL
    复制

    连接数据库

    在config/db.php文件中添加以下代码。

    1. // Enable us to use Headers
    2. ob_start();
    3. // Set sessions
    4. if(!isset($_SESSION)) {
    5. session_start();
    6. }
    7. $hostname = "localhost";
    8. $username = "phpdemo";
    9. $password = "4Mu99BhzK8dr4vF1";
    10. $dbname = "positronx_db";
    11. $connection = mysqli_connect($hostname, $username, $password, $dbname) or die("Database connection not established.")
    12. ?>
    PHP
    复制

    ob_start()方法密切关注输出缓冲并允许我们使用 Header。

    $_SESSION允许我们保存可以在 PHP 应用程序中使用的数据,只要浏览器窗口打开,会话就处于活动状态。

    使用 Bootstrap 4 设计用户注册和登录表单 UI

    要设计用户注册和登录表单 UI,我们使用 Bootstrap 4,在 HTML 布局的头部添加 Bootstrap CSS、JavaScript 和 jQuery 链接。

    在signup.php中添加以下代码。

    1. "en">
    2. "utf-8">
    3. "viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    4. "stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    5. "stylesheet" href="./css/style.css">
    6. PHP User Registration System Example
    7. class="App">
    8. <div class="vertical-center">
    9. <div class="inner-block">
    10. <form action="" method="post">
    11. <h3>Registerh3>
    12. <div class="form-group">
    13. <label>First namelabel>
    14. <input type="text" class="form-control" name="firstname" id="firstName" />
    15. div>
    16. <div class="form-group">
    17. <label>Last namelabel>
    18. <input type="text" class="form-control" name="lastname" id="lastName" />
    19. div>
    20. <div class="form-group">
    21. <label>Emaillabel>
    22. <input type="email" class="form-control" name="email" id="email" />
    23. div>
    24. <div class="form-group">
    25. <label>Mobilelabel>
    26. <input type="text" class="form-control" name="mobilenumber" id="mobilenumber" />
    27. div>
    28. <div class="form-group">
    29. <label>Passwordlabel>
    30. <input type="password" class="form-control" name="password" id="password" />
    31. div>
    32. <button type="submit" name="submit" id="submit" class="btn btn-outline-primary btn-lg btn-block">
    33. Sign up
    34. button>
    35. form>
    36. div>
    37. div>
    38. div>
    39. body>
    40. html>
    标记
    复制

    在index.php中添加以下代码以创建登录表单布局。

    1. "en">
    2. "utf-8">
    3. "viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    4. "stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    5. "stylesheet" href="css/style.css">
    6. PHP Login System
    7. class="App">
    8. <div class="vertical-center">
    9. <div class="inner-block">
    10. <form action="" method="post">
    11. <h3>Loginh3>
    12. <div class="form-group">
    13. <label>Emaillabel>
    14. <input type="email" class="form-control" name="email_signin" id="email_signin" />
    15. div>
    16. <div class="form-group">
    17. <label>Passwordlabel>
    18. <input type="password" class="form-control" name="password_signin" id="password_signin" />
    19. div>
    20. <button type="submit" name="login" id="sign_in"
    21. class="btn btn-outline-primary btn-lg btn-block">Sign
    22. inbutton>
    23. form>
    24. div>
    25. div>
    26. div>
    27. body>
    28. html>
    标记
    复制

    要在 PHP 用户身份验证应用程序中添加样式,请转到css/style.css并添加以下代码。

    1. * {
    2. box-sizing: border-box;
    3. }
    4. body {
    5. font-weight: 400;
    6. background-color: #EEEFF4;
    7. }
    8. body,
    9. html,
    10. .App,
    11. .vertical-center {
    12. width: 100%;
    13. height: 100%;
    14. }
    15. .navbar {
    16. background: #1833FF !important;
    17. width: 100%;
    18. }
    19. .btn-outline-primary {
    20. border-color: #1833FF;
    21. color: #1833FF;
    22. }
    23. .btn-outline-primary:hover {
    24. background-color: #1833FF;
    25. color: #ffffff;
    26. }
    27. .vertical-center {
    28. display: flex;
    29. text-align: left;
    30. justify-content: center;
    31. flex-direction: column;
    32. }
    33. .inner-block {
    34. width: 450px;
    35. margin: auto;
    36. background: #ffffff;
    37. box-shadow: 0px 14px 80px rgba(34, 35, 58, 0.2);
    38. padding: 40px 55px 45px 55px;
    39. transition: all .3s;
    40. border-radius: 20px;
    41. }
    42. .vertical-center .form-control:focus {
    43. border-color: #2554FF;
    44. box-shadow: none;
    45. }
    46. .vertical-center h3 {
    47. text-align: center;
    48. margin: 0;
    49. line-height: 1;
    50. padding-bottom: 20px;
    51. }
    52. label {
    53. font-weight: 500;
    54. }
    CSS
    复制

     

    建立用户注册系统

    要创建一个安全的用户注册系统,我们需要进入controllers/register.php文件并将以下代码放入其中。

    1. // Database connection
    2. include('config/db.php');
    3. // Swiftmailer lib
    4. require_once './lib/vendor/autoload.php';
    5. // Error & success messages
    6. global $success_msg, $email_exist, $f_NameErr, $l_NameErr, $_emailErr, $_mobileErr, $_passwordErr;
    7. global $fNameEmptyErr, $lNameEmptyErr, $emailEmptyErr, $mobileEmptyErr, $passwordEmptyErr, $email_verify_err, $email_verify_success;
    8. // Set empty form vars for validation mapping
    9. $_first_name = $_last_name = $_email = $_mobile_number = $_password = "";
    10. if(isset($_POST["submit"])) {
    11. $firstname = $_POST["firstname"];
    12. $lastname = $_POST["lastname"];
    13. $email = $_POST["email"];
    14. $mobilenumber = $_POST["mobilenumber"];
    15. $password = $_POST["password"];
    16. // check if email already exist
    17. $email_check_query = mysqli_query($connection, "SELECT * FROM users WHERE email = '{$email}' ");
    18. $rowCount = mysqli_num_rows($email_check_query);
    19. // PHP validation
    20. // Verify if form values are not empty
    21. if(!empty($firstname) && !empty($lastname) && !empty($email) && !empty($mobilenumber) && !empty($password)){
    22. // check if user email already exist
    23. if($rowCount > 0) {
    24. $email_exist = '
    25. User with email already exist!
  • ';
  • } else {
  • // clean the form data before sending to database
  • $_first_name = mysqli_real_escape_string($connection, $firstname);
  • $_last_name = mysqli_real_escape_string($connection, $lastname);
  • $_email = mysqli_real_escape_string($connection, $email);
  • $_mobile_number = mysqli_real_escape_string($connection, $mobilenumber);
  • $_password = mysqli_real_escape_string($connection, $password);
  • // perform validation
  • if(!preg_match("/^[a-zA-Z ]*$/", $_first_name)) {
  • $f_NameErr = '
  • Only letters and white space allowed.
  • ';
  • }
  • if(!preg_match("/^[a-zA-Z ]*$/", $_last_name)) {
  • $l_NameErr = '
  • Only letters and white space allowed.
  • ';
  • }
  • if(!filter_var($_email, FILTER_VALIDATE_EMAIL)) {
  • $_emailErr = '
  • Email format is invalid.
  • ';
  • }
  • if(!preg_match("/^[0-9]{10}+$/", $_mobile_number)) {
  • $_mobileErr = '
  • Only 10-digit mobile numbers allowed.
  • ';
  • }
  • if(!preg_match("/^(?=.*\d)(?=.*[@#\-_$%^&+=§!\?])(?=.*[a-z])(?=.*[A-Z])[0-9A-Za-z@#\-_$%^&+=§!\?]{6,20}$/", $_password)) {
  • $_passwordErr = '
  • Password should be between 6 to 20 charcters long, contains atleast one special chacter, lowercase, uppercase and a digit.
  • ';
  • }
  • // Store the data in db, if all the preg_match condition met
  • if((preg_match("/^[a-zA-Z ]*$/", $_first_name)) && (preg_match("/^[a-zA-Z ]*$/", $_last_name)) &&
  • (filter_var($_email, FILTER_VALIDATE_EMAIL)) && (preg_match("/^[0-9]{10}+$/", $_mobile_number)) &&
  • (preg_match("/^(?=.*\d)(?=.*[@#\-_$%^&+=§!\?])(?=.*[a-z])(?=.*[A-Z])[0-9A-Za-z@#\-_$%^&+=§!\?]{8,20}$/", $_password))){
  • // Generate random activation token
  • $token = md5(rand().time());
  • // Password hash
  • $password_hash = password_hash($password, PASSWORD_BCRYPT);
  • // Query
  • $sql = "INSERT INTO users (firstname, lastname, email, mobilenumber, password, token, is_active,
  • date_time) VALUES ('{$firstname}', '{$lastname}', '{$email}', '{$mobilenumber}', '{$password_hash}',
  • '{$token}', '0', now())";
  • // Create mysql query
  • $sqlQuery = mysqli_query($connection, $sql);
  • if(!$sqlQuery){
  • die("MySQL query failed!" . mysqli_error($connection));
  • }
  • // Send verification email
  • if($sqlQuery) {
  • $msg = 'Click on the activation link to verify your email.

  • ';
  • // Create the Transport
  • $transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
  • ->setUsername('your_email@gmail.com')
  • ->setPassword('your_email_password');
  • // Create the Mailer using your created Transport
  • $mailer = new Swift_Mailer($transport);
  • // Create a message
  • $message = (new Swift_Message('Please Verify Email Address!'))
  • ->setFrom([$email => $firstname . ' ' . $lastname])
  • ->setTo($email)
  • ->addPart($msg, "text/html")
  • ->setBody('Hello! User');
  • // Send the message
  • $result = $mailer->send($message);
  • if(!$result){
  • $email_verify_err = '
  • Verification email coud not be sent!
  • ';
  • } else {
  • $email_verify_success = '
  • Verification email has been sent!
  • ';
  • }
  • }
  • }
  • }
  • } else {
  • if(empty($firstname)){
  • $fNameEmptyErr = '
  • First name can not be blank.
  • ';
  • }
  • if(empty($lastname)){
  • $lNameEmptyErr = '
  • Last name can not be blank.
  • ';
  • }
  • if(empty($email)){
  • $emailEmptyErr = '
  • Email can not be blank.
  • ';
  • }
  • if(empty($mobilenumber)){
  • $mobileEmptyErr = '
  • Mobile number can not be blank.
  • ';
  • }
  • if(empty($password)){
  • $passwordEmptyErr = '
  • Password can not be blank.
  • ';
  • }
  • }
  • }
  • ?>
  • PHP
    复制

    包括处理用户数据的数据库。isset()方法在用户单击提交按钮时检查表单数据,该按钮与我们在注册表单中传递的相同名称属性。

    使用 HTTP $_POST[”]方法提取用户数据,例如名字、姓氏、电子邮件、手机号码密码。

    使用 SQL 脚本通过mysqli_query()方法验证用户提供的电子邮件是否已存在于数据库中。

    检查表单值是否不为空。

    验证用户电子邮件是否已存在,然后使用 Bootstrap 警报消息组件显示错误。我们将错误消息设置为全局变量,以便我们可以将其显示给用户。

    mysqli_real_escape_string ()方法在发送到数据库之前清理数据。

    preg_match()方法对名称、手机名称和密码进行 PHP 验证。为了验证电子邮件值,我们使用了filter_var()方法。我们包装了错误并将其设置为全局。

    我们需要使用md5(rand().time())方法生成随机令牌,以向用户电子邮件 ID 发送验证电子邮件。

    为了安全地散列密码,我们使用了password_hash()方法。password_hash() 使用安全的单向散列算法创建一个新的密码散列。

    我们需要安装 SwiftMailer php 插件来向用户发送验证邮件,我使用以下 composer 命令来安装 SwiftMailer 库。确保你的开发机器上安装了 composer。

    composer require "swiftmailer/swiftmailer:^6.0"
    重击
    复制

    我们需要导入SwiftMailer库并添加 SwiftMailer 脚本,同时定义将发送给用户的电子邮件模板。

    现在,我们需要在signup.php文件中实现用户认证逻辑。

    1. include('./controllers/register.php'); ?>
    2. "en">
    3. "utf-8">
    4. "viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    5. "stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    6. "stylesheet" href="./css/style.css">
    7. PHP User Registration System Example
    8. include('./header.php'); ?>
    9. class="App">
    10. <div class="vertical-center">
    11. <div class="inner-block">
    12. <form action="" method="post">
    13. <h3>Registerh3>
    14. php echo $success_msg; ?>
    15. php echo $email_exist; ?>
    16. php echo $email_verify_err; ?>
    17. php echo $email_verify_success; ?>
    18. <div class="form-group">
    19. <label>First namelabel>
    20. <input type="text" class="form-control" name="firstname" id="firstName" />
    21. php echo $fNameEmptyErr; ?>
    22. php echo $f_NameErr; ?>
    23. div>
    24. <div class="form-group">
    25. <label>Last namelabel>
    26. <input type="text" class="form-control" name="lastname" id="lastName" />
    27. php echo $l_NameErr; ?>
    28. php echo $lNameEmptyErr; ?>
    29. div>
    30. <div class="form-group">
    31. <label>Emaillabel>
    32. <input type="email" class="form-control" name="email" id="email" />
    33. php echo $_emailErr; ?>
    34. php echo $emailEmptyErr; ?>
    35. div>
    36. <div class="form-group">
    37. <label>Mobilelabel>
    38. <input type="text" class="form-control" name="mobilenumber" id="mobilenumber" />
    39. php echo $_mobileErr; ?>
    40. php echo $mobileEmptyErr; ?>
    41. div>
    42. <div class="form-group">
    43. <label>Passwordlabel>
    44. <input type="password" class="form-control" name="password" id="password" />
    45. php echo $_passwordErr; ?>
    46. php echo $passwordEmptyErr; ?>
    47. div>
    48. <button type="submit" name="submit" id="submit" class="btn btn-outline-primary btn-lg btn-block">Sign up
    49. button>
    50. form>
    51. div>
    52. div>
    53. div>
    54. body>
    55. html>
    PHP
    复制

    PHP 8 中的用户电子邮件验证脚本

    我们在register.php文件中定义了 SwiftMailer 配置,现在实现用户验证脚本发送验证邮件。

    在controllers/user_activation.php文件中添加以下代码。

    1. // Database connection
    2. include('./config/db.php');
    3. global $email_verified, $email_already_verified, $activation_error;
    4. // GET the token = ?token
    5. if(!empty($_GET['token'])){
    6. $token = $_GET['token'];
    7. } else {
    8. $token = "";
    9. }
    10. if($token != "") {
    11. $sqlQuery = mysqli_query($connection, "SELECT * FROM users WHERE token = '$token' ");
    12. $countRow = mysqli_num_rows($sqlQuery);
    13. if($countRow == 1){
    14. while($rowData = mysqli_fetch_array($sqlQuery)){
    15. $is_active = $rowData['is_active'];
    16. if($is_active == 0) {
    17. $update = mysqli_query($connection, "UPDATE users SET is_active = '1' WHERE token = '$token' ");
    18. if($update){
    19. $email_verified = '
    20. User email successfully verified!
  • ';
  • }
  • } else {
  • $email_already_verified = '
  • User email already verified!
  • ';
  • }
  • }
  • } else {
  • $activation_error = '
  • Activation error!
  • ';
  • }
  • }
  • ?>
  • PHP
    复制

    在user_verification.php文件中添加以下代码。

    1. include('./controllers/user_activation.php'); ?>
    2. "en">
    3. "utf-8">
    4. "viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    5. "stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    6. "stylesheet" href="./css/style.css">
    7. User Verification
    8. class="container">
    9. <div class="jumbotron text-center">
    10. <h1 class="display-4">User Email Verification Demoh1>
    11. <div class="col-12 mb-5 text-center">
    12. php echo $email_already_verified; ?>
    13. php echo $email_verified; ?>
    14. php echo $activation_error; ?>
    15. div>
    16. <p class="lead">If user account is verified then click on the following button to login.p>
    17. <a class="btn btn-lg btn-success" href="http://localhost:8888/php-user-authentication/index.php"
    18. >Click to Login
    19. a>
    20. div>
    21. div>
    22. body>
    23. html>
    PHP
    复制

     

    使用 MySQL 构建 PHP 8 登录系统

    以下代码仅允许访问已验证其电子邮件地址的用户。未经验证的用户无法在应用程序中访问,我们还将登录用户的数据存储到 PHP Session 中,并借助header(“Location: page_url.php”)方法将登录用户重定向到dashboard.php 页面。

    要创建 PHP MySQL 登录系统,请在controllers/login.php文件中添加以下代码。

    1. // Database connection
    2. include('config/db.php');
    3. global $wrongPwdErr, $accountNotExistErr, $emailPwdErr, $verificationRequiredErr, $email_empty_err, $pass_empty_err;
    4. if(isset($_POST['login'])) {
    5. $email_signin = $_POST['email_signin'];
    6. $password_signin = $_POST['password_signin'];
    7. // clean data
    8. $user_email = filter_var($email_signin, FILTER_SANITIZE_EMAIL);
    9. $pswd = mysqli_real_escape_string($connection, $password_signin);
    10. // Query if email exists in db
    11. $sql = "SELECT * From users WHERE email = '{$email_signin}' ";
    12. $query = mysqli_query($connection, $sql);
    13. $rowCount = mysqli_num_rows($query);
    14. // If query fails, show the reason
    15. if(!$query){
    16. die("SQL query failed: " . mysqli_error($connection));
    17. }
    18. if(!empty($email_signin) && !empty($password_signin)){
    19. if(!preg_match("/^(?=.*\d)(?=.*[@#\-_$%^&+=§!\?])(?=.*[a-z])(?=.*[A-Z])[0-9A-Za-z@#\-_$%^&+=§!\?]{6,20}$/", $pswd)) {
    20. $wrongPwdErr = '
    21. Password should be between 6 to 20 charcters long, contains atleast one special chacter, lowercase, uppercase and a digit.
    22. ';
  • }
  • // Check if email exist
  • if($rowCount <= 0) {
  • $accountNotExistErr = '
  • User account does not exist.
  • ';
  • } else {
  • // Fetch user data and store in php session
  • while($row = mysqli_fetch_array($query)) {
  • $id = $row['id'];
  • $firstname = $row['firstname'];
  • $lastname = $row['lastname'];
  • $email = $row['email'];
  • $mobilenumber = $row['mobilenumber'];
  • $pass_word = $row['password'];
  • $token = $row['token'];
  • $is_active = $row['is_active'];
  • }
  • // Verify password
  • $password = password_verify($password_signin, $pass_word);
  • // Allow only verified user
  • if($is_active == '1') {
  • if($email_signin == $email && $password_signin == $password) {
  • header("Location: ./dashboard.php");
  • $_SESSION['id'] = $id;
  • $_SESSION['firstname'] = $firstname;
  • $_SESSION['lastname'] = $lastname;
  • $_SESSION['email'] = $email;
  • $_SESSION['mobilenumber'] = $mobilenumber;
  • $_SESSION['token'] = $token;
  • } else {
  • $emailPwdErr = '
  • Either email or password is incorrect.
  • ';
  • }
  • } else {
  • $verificationRequiredErr = '
  • Account verification is required for login.
  • ';
  • }
  • }
  • } else {
  • if(empty($email_signin)){
  • $email_empty_err = "
  • Email not provided.
  • ";
  • }
  • if(empty($password_signin)){
  • $pass_empty_err = "
  • Password not provided.
  • ";
  • }
  • }
  • }
  • ?>
  • PHP
    复制

    要在登录页面中实现登录逻辑,请在controllers/index.php文件中添加以下代码。

    1. "en">
    2. "utf-8">
    3. "viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    4. "stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    5. "stylesheet" href="css/style.css">
    6. PHP User Registration & Login System Demo
    7. include('../php-user-authentication/header.php'); ?>
    8. include('./controllers/login.php'); ?>
    9. class="App">
    10. <div class="vertical-center">
    11. <div class="inner-block">
    12. <form action="" method="post">
    13. <h3>Loginh3>
    14. php echo $accountNotExistErr; ?>
    15. php echo $emailPwdErr; ?>
    16. php echo $verificationRequiredErr; ?>
    17. php echo $email_empty_err; ?>
    18. php echo $pass_empty_err; ?>
    19. <div class="form-group">
    20. <label>Emaillabel>
    21. <input type="email" class="form-control" name="email_signin" id="email_signin" />
    22. div>
    23. <div class="form-group">
    24. <label>Passwordlabel>
    25. <input type="password" class="form-control" name="password_signin"
    26. id="password_signin" />
    27. div>
    28. <button type="submit" name="login" id="sign_in" class="btn btn-outline-primary btn-lg btn-block">Sign
    29. inbutton>
    30. form>
    31. div>
    32. div>
    33. div>
    34. body>
    35. html>
    PHP
    复制

    显示登录用户的数据和注销脚本

    在dashboard.php中添加以下代码,仅向登录用户显示用户数据。

    1. include('config/db.php'); ?>
    2. "en">
    3. "utf-8">
    4. "viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    5. "stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    6. "stylesheet" href="./css/style.css">
    7. PHP User Registration System Example
    8. class="container mt-5">
    9. <div class="d-flex justify-content-center">
    10. <div class="card" style="width: 25rem">
    11. <div class="card-body">
    12. <h5 class="card-title text-center mb-4">User Profileh5>
    13. <h6 class="card-subtitle mb-2 text-muted">php echo $_SESSION['firstname']; ?>
    14. php echo $_SESSION['lastname']; ?>h6>
    15. <p class="card-text">Email address: php echo $_SESSION['email']; ?>p>
    16. <p class="card-text">Mobile number: php echo $_SESSION['mobilenumber']; ?>p>
    17. <a class="btn btn-danger btn-block" href="logout.php">Log outa>
    18. div>
    19. div>
    20. div>
    21. div>
    22. body>
    23. html>
    PHP
    复制

    现在,我们需要销毁会话以从用户身份验证系统中注销用户。我们已经将 logout.php 链接传递给仪表板文件中的注销按钮。

    打开logout.php并将以下代码放入其中。

    1. session_start();
    2. session_destroy();
    3. header("Location: http://localhost:8888/php-user-authentication/index.php")
    4. ;?>
    PHP
    复制

    结论

    因此,这是 PHP 8 用户身份验证和登录应用程序。我希望您对我们如何使用 PHP 和 MySQL 进行登录和身份验证系统有基本的了解。我们介绍了一些基本主题,例如向用户发送验证电子邮件、安全地散列密码。

    本教程的完整代码可以在GitHub上找到。

  • 相关阅读:
    小程序分包及分包预下载
    Redis客户端通信RESP协议
    【技术】Spring Boot 将 Word 转换为 PDF 2.0 版本
    使用telnet和ssh登录linux
    移动通信网络规划:勘测输出
    MangoPapa 的实用小脚本(目录篇)
    CVPR 2022 | 美团技术团队精选论文解读
    xml schema
    聊聊分布式架构01——http通信基础
    苹果MacBook电脑应用优化利器CleanMyMac X
  • 原文地址:https://blog.csdn.net/allway2/article/details/126423859