一、登录页面
</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>