PHP uses session for login verification

王林
Release: 2023-04-08 09:54:01
forward
3764 people have browsed it

PHP uses session for login verification

1. Overview

Back-end language: php (native php is used, no framework is introduced)

Front-end style: Bootstrap

Main functions: Basic administrator functions. Ordinary users need to use administrators for unified management. Ordinary users can view statistical data, and administrator users can only log in to the administrator center to manage users.

(Related learning video tutorial sharing: php video tutorial)

2. Session usage

a. After successful login, you need to create and record session

<?php

    require "../DB/MySQLHelper.php";
    require "../BLL/UserBLL.php";
    $result = -1;
    if(count($_POST)!=2)
    {
        echo $result;
        return;
    }
    $name = $_POST["name"];
    $password = $_POST["password"];

    $helper = new MySQLHelper();
    $helper->InitMySQL();
    $level = SelectUserLevel($helper,$name,$password);
    if($level != -1)
    {
        $result = $level;
        session_start();
        $_SESSION["user"] = true;
        $_SESSION["name"]=$name;
        $_SESSION["pwd"]=$password;
    }
    echo $result;
?>
Copy after login

b. Other functional modules use the specific method of session to write a general php. Other modules first call this php to determine whether to log in before calling.

<?php
    $user = false;
    session_start();
    //  判断是否登陆
    if (isset($_SESSION["user"]) && $_SESSION["user"] === true) {
        echo "true";
    } 
    else {
        $_SESSION["user"] = false;
        echo "false";
    }

?>
Copy after login

Recommended related articles and tutorials: php tutorial

Recommended related articles: The most comprehensive collection of js interview questions in 2020 (up to date)

The above is the detailed content of PHP uses session for login verification. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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