<?php
error_reporting(0);
session_start();
//数据库连接
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('chenkun', $conn);
mysql_query('SET NAMES UTF-8');
//定义常量
define('ALL_PS', 'PHP');
function user_shell($uid, $shell, $m_id) {
$sql = "select * from admin where uid='$uid'";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
$shell = is_array($row) ? $shell == md5($row['username'] . $row['password'] . ALL_PS) : FALSE;
if ($shell) {
if ($row['m_id'] <= $m_id) {
return $row;
}
echo "你的权限不足";
exit ();
} else {
echo "你无权限访问该页";
exit ();
}
}
function user_mktime($onlinetime) {
$new_time = mktime();
if (($new_time - $onlinetime) > '900') {
session_destroy();
echo "登陆超时";
exit ();
} else {
$_SESSION['times'] = mktime();
}
}
?>
PHP+MYSQL会员系统的登陆即权限判断
包含三个页面,cogfig页面是被包含页面。denglu页面负责提交,session赋值等,denglu_link页面负责权限判断的演示。
本例事先已存在test数据库,user_list表,表中有uid,m_id,username,password四个字段。并且password字段已经经过md5加密,形式是:md5("用户密码".ALL_PS),即用户输入的密码加常量进行加密。
config.php页面:
<?php
//启动session
session_start();
//数据库连接
$conn=mysql_connect('localhost','root','******');
mysql_select_db('test',$conn);
//定义常量
define("ALL_PS","php100");
//判断权限函数
function user_shell($uid,$shell){
$sql="SELECT * FROM `user_list` WHERE `uid` = '$uid'";
$query=mysql_query($sql);
$exist=is_array($row=mysql_fetch_array($query));
$exist2=$exist?$shell==md5($row['username'].$row['password'].ALL_PS):FALSE;
if($exist2){
return $row;
}else{
echo "你无权限访问该页";
exit();
}
}
?>
denglu.php页面:
<?
include("config.php");
if($_POST['submit']){
$username=str_replace(" ","",$_POST['username']); //去除空格
$sql="SELECT * FROM `user_list` WHERE `username` = '$username'";
$query=mysql_query($sql);
$exist=is_array($row=mysql_fetch_array($query)); //判断是否存在这样一个用户
$exist2=$exist?md5($_POST['password'].ALL_PS)==$row['password']:FALSE;//判断密码
if($exist2){
$_SESSION['uid']=$row['uid']; // session赋值
$_SESSION['user_shell']=md5($row['username'].$row['password'].ALL_PS);
echo "登陆成功";
}else{
echo "不正确的用户名";
SESSION_DESTROY();
}
}
?>
<form action="" method="post">
用户名:<input type="text" name="username" /><br>
密码:<input type="password" name="password"/><br>
验证码:<input type="code" name="code" size="10"/>
<img src="imgcode.php"><br><br>
<input type="submit" name="submit" value="登陆"/>
</form>
<a href="http://127.0.0.1/test/denglu_link.php">denglu_link</a>
denglu_link.php页面:
<?
include("config.php");
$arr=user_shell($_SESSION['uid'],$_SESSION['user_shell']);//以上两句即可对权限进行判断
echo $arr['username'];
?>
权限内容