PHP Learning Cookie Session Control

Source: Internet
Author: User

The use of cookies to record user data on the client without the need to call from the server every time, this can improve the efficiency of the Web page, reduce the pressure on the server

The following example simulates the operation of a login interface, using the invocation of a cookie

Interface display (not beautified)



Create the required files under the root directory


index.ph In p , the output of the tabular form is first written out


Create a database and create a file that is called by the database because you want to connect to the database and call the database's data

Create a table of users and insert data, where data is inserted into information including user name, password, and permissions


Create a conn.inc.php in the root directory to hold the database connection


conn.inc.php

<?php    $mysqli =new mysqli ("localhost", "root", "XXXXX", "sqldb");

connection database included in login.php, using the Mysqli method, the password is encrypted using the MD5 () method

<?php    if (isset ($_post["sub")) {    include "conn.inc.php";    $sql = "SELECT id from users where name= ' {$_post[" name "]} ' and password= '". MD5 ($_post["password"]). "'";    $result = $mysqli->query ($sql);    Save Data    if ($result->num_rows > 0) {    $row = $result->fetch_assoc ();    $time =time () *1800;    Setcookie ("username", $_post["name"], $time);    Setcookie ("UID", $row ["id"], $time);    Setcookie ("IsLogin", 1);    Header ("Location:index.php");  Jump interface    }    echo "username password wrong";    }? >

Note that the cookie needs to indicate the length of time the data is saved, "username", "uid", "IsLogin" in the saved data

Index.php involves page jumps, creating a common jump page for the class comm.php

comm.php: If the return is "IsLogin" to jump

<?php    //judgment: If not logged in automatically jump to the login page if    (!$_cookie["IsLogin"]) {        header ("Location:login.php");    }

Log in successfully after the jump to the index.php page, login is unsuccessful, return to log in again

index.php contains conn.inc.php and comm.php

<?php    include "comm.php";  Determine if login success    include "conn.inc.php";    echo "User <b>". $_cookie["username"]. " </b> Hello! This is the homepage of the website ";    echo "Your permission is as follows:<br>";    $sql = "Select Allow_1,allow_2,allow_3,allow_4 from Users where id= ' {$_cookie[" UID "]} '";    $result = $mysqli->query ($sql);    $user = $result->fetch_assoc ();    if ($user ["Allow_1"]) {        echo "111111111111<br>";    }    if ($user ["allow_2"]) {        echo "222222222222<br>";    }    if ($user ["Allow_3"]) {        echo "333333333333<br>";    }    if ($user ["Allow_4"]) {        echo "444444444444<br>";    }    ? >    <a href= "test.php" > second page </a><br>    <a href= "test2.php" > third page </a><br >    <a href= "logout.php" > Exit </a><br>
The last second page and the third page are the same as index.php.

Logout logout.php need to log out user information

<?php    include "comm.php";  Determine if the login is successful        $username =$_cookie["username"];  Remove username    //logout    Setcookie ("username");    Setcookie ("UID");    Setcookie ("IsLogin");    echo $username. " Goodbye ";? ><br><a href= "login.php" > re-login </a>




Full code:

login.php:

<?php if (Isset ($_post["sub")) {include "conn.inc.php";    $sql = "SELECT id from users where name= ' {$_post[" name "]} ' and password= '". MD5 ($_post["password"]). "'";    $result = $mysqli->query ($sql);    Save data if ($result->num_rows > 0) {$row = $result->fetch_assoc ();    $time =time () *1800;    Setcookie ("username", $_post["name"], $time);    Setcookie ("UID", $row ["id"], $time);    Setcookie ("IsLogin", 1);  Header ("Location:index.php");    Jump interface} echo "User name password is incorrect"; }?>

















PHP Learning Cookie Session Control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.