Home > php教程 > php手册 > PHP+MYSQL会员系统的登陆即权限判断实现代码

PHP+MYSQL会员系统的登陆即权限判断实现代码

WBOY
Release: 2016-06-13 12:04:59
Original
865 people have browsed it

复制代码 代码如下:


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'] 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页面:

复制代码 代码如下:


//启动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();
}
}
?>


用户名:

密码:

验证码:
PHP+MYSQL会员系统的登陆即权限判断实现代码




denglu_link

denglu_link.php页面:

复制代码 代码如下:



include("config.php");
$arr=user_shell($_SESSION['uid'],$_SESSION['user_shell']);//以上两句即可对权限进行判断
echo $arr['username'];
?>
权限内容


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template