含三个页面,cogfig页面是被包含页面,denglu页面负责提交,session赋值等,denglu_link页面负责权限判断的演示,本例事先已存在test数据库,user_list表,表中有uid,m_id,username,password四个字段,并且password字段已经经过md5加密,形式是:md5(\"用户密码\".ALL_PS),即用户输入的密码加常量进行判断.
先来看配置文件,代码如下:
- <?php
-
- 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();
- }
- }
- ?>
PHP登录页面,代码如下:
- <?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['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>
PHP权限判断页面,代码如下:
- <?php
- include("config.php");
- $arr=user_shell($_SESSION['uid'],$_SESSION['user_shell']);
- echo $arr['username'];
- ?>
|