Home >Backend Development >PHP Tutorial >PHP uses session for login verification

PHP uses session for login verification

王林
王林forward
2020-01-23 19:32:273799browse

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;
?>

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";
    }

?>

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete