There are several ways to use the session
1. Cookies based
2. Based on the URL of the SID string
3. stored in the database
4. Stored in the memcache (the most efficient)
Wrote a small example of landing:
Specifically implemented as follows:
comm.php
<?php
/**
* When the browser disables cookies, you can take the delivery SessionID *
*
session_start ();
echo "ID:". SESSION_ID (). "<br>";
To determine if the user is logged in, if the implementation of the jump if
(!$_session["IsLogin"]) {
header ("Location:login.php")
}
? >
conn.inc.php
<?php
$mysqli =new mysqli ("localhost", "root", "root", "phpdb");
? >
control.php
<?php
include "conn.inc.php";
echo "Your authority is as follows:<br>";
$sql = "Select Allow_1, Allow_2, Allow_3, allow_4 from users where id= ' {$_session[' uid"]} ';
$result = $mysqli->query ($sql);
$user = $result->fetch_assoc ();
if ($user ["Allow_1"]) {
echo "111111111111111111111111<br>";
}
if ($user ["allow_2"]) {
echo "2222222222222222<br>";
}
if ($user ["Allow_3"]) {
echo "333333333333333333333<br>";
}
if ($user ["Allow_4"]) {
echo "444444444444444444444444<br>";
}
? >
index.php
<?php
include "comm.php";
echo "User <b>". $_session["username"]. " </b> Hello, this is the homepage of this website. ";
Include "control.php";
>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
login.php
<?php session_start (); echo "ID:". SESSION_ID ().
"<br>";
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);
if ($result->num_rows > 0) {$row = $result->fetch_assoc ();
$_session["username"] = $_post["name"];
$_session["UID" = $row ["id"];
$_session["IsLogin"] = 1;
Jump to index.php echo ' <script> ';
1. The custom parameter name SID, which needs to be session_id ($_get["Sid") when another page opens the session;
echo "Location= ' index.php?sid=". session_id (). "'"; 2. Or use the constant SID to replace the PHPSESSDI=XXXXXXXXXXXX//advantage is: If the cookie is open SID is null, if the cookie does not open, then use SID//3. or modify PHP.ini session.u
Se_trans_sid=1//So only in PHP in the jump need to add SID, other page jumps do not need to add Sid//4. echo "location= ' index.php?"
Phpsessid= ". session_id ()." ' ";
is generally based on a 3-way echo ' </script> ';
}else{ echo "Username password is incorrect.
<br> "; }}?> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > <?php
include "comm.php";
$username = $_session["username"];
Destroysession ();
$_session["username"]= $username;
Echo $username. "Goodbye!";
/**
* Destroy session
/
function destroysession () {
//1. Open session/
/on new page need to open session first
// session_start ();
2. Delete data
//Delete the value method in session One:
unset ($_session["AA"]);
Delete Value method in session two:
$_session=array ();
3. Delete client in COOKIE SessionID
if (Isset ($_cookie[session_name ())) {
// You need to specify the path of the cookie in php.ini session.cookie_path
Setcookie (Session_name (), ', Time () -3600, '/');
4. Complete destruction of Session
Session_destroy ()
;
>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
three.php
<?php
include "comm.php";
echo "User <b>". $_session["username"]. " </b> Hello, this is the website of these three pages. ";
Include "control.php";
>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
two.php
<?php
include "comm.php";
echo "User <b>". $_session["username"]. " </b> Hello, this is the website of these two pages. ";
Include "control.php";
>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >