Home  >  Article  >  Backend Development  >  PHP session application example login verification

PHP session application example login verification

黄舟
黄舟Original
2016-12-21 10:03:40840browse

 
 
Login 
 
 
 

 
 
 
 
 
 
 
 
 
 
 



用户名:
密码:
Cookie保存时间:









------------------------------------------------- -----------------------

@mysql_connect("localhost", "root","1981427") //Required before selecting the database Connect to the database server first
or die("Database server connection failed");
@mysql_select_db("test") //Select database mydb
or die("Database does not exist or is unavailable");
//Get user input
$username = $_POST['username'];
$passcode = $_POST['passcode'];
//Execute SQL statement to obtain the value of Session
$query = @mysql_query("select username, userflag from users "
. "where username = '$username' and passcode = '$passcode'")
or die("SQL statement execution failed");
//Determine whether the user exists and whether the password is correct
if($row = mysql_fetch_array($query ))
{
session_start(); //Mark the start of Session
//Determine whether the user's permission information is valid, if it is 1 or 0, it means it is valid
if($row['userflag'] == 1 or $row ['userflag'] == 0)
{
$_SESSION['username'] = $row['username'];
$_SESSION['userflag'] = $row['userflag'];
echo "< a href="http://www.php1.cn/"> }
else //If the permission information is invalid, output an error message
{
echo "User permission information is incorrect";
}
}
else //If If the username and password are incorrect, the output error
{
echo "Username or password is incorrect";
}
?>

------------------------ ----------------------------------------
unset($ _SESSION['username']);
unset($_SESSION['passcode']);
unset($_SESSION['userflag']);
echo "Logout successful";
?>

----- -------------------------------------------------- --------

session_start();
if(isset($_SESSION['username']))
{
@mysql_connect("localhost", "root","1981427" ) //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("test") //Select the database mydb
or die("The database does not exist or is unavailable");
//Get Session
$username = $_SESSION['username'];
//Execute SQL statement to get the value of userflag
$query = @mysql_query("select userflag from users "
."where username = '$username' ")
or die("SQL statement execution failed");
$row = mysql_fetch_array($query);
//Compare the permission information in the current database with the information in the Session, and update the Session information if they are different
if ($row['userflag'] != $_SESSION['userflag'])
{
$_SESSION['userflag'] = $row['userflag'];
}
//Output different welcomes according to the value of Session Information
if($_SESSION['userflag'] == 1)
echo "Welcome administrator".$_SESSION['username']."Log in to the system";
if($_SESSION['userflag'] == 0)
echo "Welcome user".$_SESSION['username']."Log in to the system";
echo " }
else
{
echo " You do not have permission to access this page";
}
?> Beginner to PHP. I also reprinted it from others. I haven't tested it on the computer yet. I will collect it first.

From: http://phpstart.php100.com/apps-htm-q -diary-a-detail-did-7373.html

The above is the content of php session application instance login verification. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!