【PHP】简单的登陆验证和权限管理

一、登录页面

</pre><pre name="code" class="html"><form method=”post” action=”check.php”>
<input type=”text” name=”username” />
<input type=”password” name=”password” />
<input type=”submit” name=”submit” value=”提交” />
</from>


二、登陆验证

check.php

<?php
include “include.php”//后面有代码
$username=$_POST[‘username’];
$password=md5($_POST[‘password’]);
$sql=”SELECT * FROM user WHERE username=’$username’ and password=’$password’”;
$res=feachOne($sql);
if($res){
session_start();
$_SESSION[‘username’]=$username;
$_SESSION[‘power’]=$res[‘power’];
echo "<script>location.href='index.php';</script>";}
?>

include.php

<?php
require_once 'Config/config.php';//数据库配置,见下文
require_once 'mysql.php';//数据库操作,见下文
$link=connect();//连接数据库
?>

mysql.php

<?php
 
/* 
 * 连接数据库
 *  
 *  */
require_once 'Config/config.php';
function connect(){
    //连接MySQL
    $link=@mysql_connect(DB_HOST,DB_USER,DB_PWD) or die ('数据库连接失败<br/>ERROR'.mysql_errno().':'.mysql_error());
    //字符集
    //mysql_set_charset(DB_CHARSET,$link);
    mysql_query("SET NAMES 'utf8'");
    //打开数据库
    mysql_select_db(DB_DBNAME) or die ('指定的实据库打开失败');
    return $link;
}
/*
 * 查询一条记录
 *
 *  */
 
function feachOne($sql,$result_type=MYSQL_ASSOC){
    $result=mysql_query($sql);
    if($result && mysql_num_rows($result)>0){
        return mysql_fetch_array($result,$result_type);
    }else{
        return false;
    }
}
 
?>

config.php

<?php
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PWD', 'root');
define('DB_CHARSET', 'utf-8');
define('DB_DBNAME', 'database');
 ?>


三、session权限验证(index.php)

power.php

在后面需要登录才能查看的页面和需要权限的页面include此文件

<?php
session_start();
if(!isset($_SESSION['username'])){
    echo "<script>location.href='login.php';</script>";
}elseif($_SESSION[‘power’]<=’10’){
 echo “您没有权限查看此页!”;
}


index.php(后缀名为php才可以直接使用<?php ?>)

<?php
include “power.php”
?>
?>
<html>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值