Home > Backend Development > PHP Tutorial > PHP session application example login verification_PHP tutorial

PHP session application example login verification_PHP tutorial

WBOY
Release: 2016-07-21 15:47:38
Original
811 people have browsed it

复制代码 代码如下:



Login


















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









复制代码 代码如下:

@mysql_connect("localhost", "root","1981427") //选择数据库之前需要先连接数据库服务器
or die("数据库服务器连接失败");
@mysql_select_db("test") //选择数据库mydb
or die("数据库不存在或不可用");
//获取用户输入
$username = $_POST['username'];
$passcode = $_POST['passcode'];
//执行SQL语句获得Session的值
$query = @mysql_query("select username, userflag from users "
."where username = '$username' and passcode = '$passcode'")
or die("SQL语句执行失败");
//判断用户是否存在,密码是否正确
if($row = mysql_fetch_array($query))
{
session_start(); //标志Session的开始
//判断用户的权限信息是否有效,如果为1或0则说明有效
if($row['userflag'] == 1 or $row['userflag'] == 0)
{
$_SESSION['username'] = $row['username'];
$_SESSION['userflag'] = $row['userflag'];
echo "欢迎登录,点击此处进入欢迎界面";
}
else //如果权限信息无效输出错误信息
{
echo "用户权限信息不正确";
}
}
else //如果用户名和密码不正确,则输出错误
{
echo "用户名或密码错误";
}
?>

复制代码 代码如下:

unset($_SESSION['username']);
unset($_SESSION['passcode']);
unset($_SESSION['userflag']);
echo "注销成功";
?>

复制代码 代码如下:

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( "Database does not exist or is unavailable");
//Get Session
$username = $_SESSION['username'];
//Execute SQL statement to obtain 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 welcome messages according to the value of Session
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 "Logout";
}
else
{
echo "You do not have permission to access this page";
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319892.htmlTechArticleCopy the code as follows: html head titleLogin/title meta http-equiv="Content-Type" content="text /html; charset=gb2312" /head body form name="form1" method="post" action="login.ph...
source:php.cn
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