PHP登陆Session验证
首先,在MySQL数据库中创建管理员账号表create table users(
username char(8) not null,
passcode char(8) not null,
userflag int,
primary key(username)
);接着对表进行初始化,输入数据。insert into users values('admin','admin123',1);用户登陆页面:<html>
<head>
<title>Login_fangbei</title>
</head>
<body>
<form name="fangbei" method="post" action="check_session_login.php">
<div style="width:353">
<dl>
<dt></dt>
<dd>
<div align="left">
Username:
<input type="text" name="username" />
</div>
</dd>
<dd>
<div align="left">
Psssword:
<input type="password" name="passcode" />
</div>
</dd>
<dd>
<p align="center"> <input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Reset" value="Reset" /> </p>
</dd>
</dl>
</div>
</form>
</body>
</html>这个页面登陆之后,会从表单中转向check_session_login.php页面。代码如下:<?php
@mysql_connect("localhost","root",'root')
or die("数据库连接失败");
@mysql_select_db("mydb")
or die("选择数据库失败");
//获取输入的信息
$username = $_POST['username'];
$passcode = $_POST['passcode'];
//获取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();
//判断权限
if($row['userflag'] == 1 or $row['userflag'] == 0){
$_SESSION['username'] = $row['username'];
$_SESSION['userflag'] = $row['userflag'];
echo "<a href='welcome_session_login.php'>欢迎访问bbs.cnblackhat.com</a>";
}else{
echo "userflag不正确";
}
}else{
echo "username或者usercode";
}
?>验证通过之后,达到欢迎页面welcome_session_login.php。代码如下:<?php
session_start();
if(isset($_SESSION['username']))
{
if($_SESSION['userflag'] == 1)
echo "欢迎管理员".$_SESSION['username']."登陆";
if($_SESSION['userflag'] == 0)
echo "欢迎用户".$_SESSION['username']."登陆";
}
else
{
echo "您没有权限访问此页面";
}
?>最后还有个销毁session页面destroy_session_login.php。代码如下:<?php
unset($_SESSION['username']);
unset($_SESSION['passcode']);
unset($_SESSION['userflag']);
echo "注销成功";
?>
页:
[1]