PHP会员管理系统原理及实现
注意事项:
验证码的输出问题?出现乱码
config.php
<?php
session_start();
$link=mysql_connect("localhost","root","liunian");
mysql_select_db("member",$link);
define("GLOBAL_PWD","okliunian");
/**
* 检查用户权限
* @param $uid 用户登录成功时,保存在session中的用户uid
* @param $shell 用户登录成功时,保存在session中的用户user_shell
* @param $m_id
*/
function user_shell($uid,$shell,$m_id){
$sql="select * from user_list where uid='$uid'";
$query=mysql_query($sql);
if (is_array($row=mysql_fetch_array($query))) {
if ($shell==md5($row['username'].$row['password'].GLOBAL_PWD)) {
if ($row['mid']<=$m_id) {
return $row;
}
}
}else{
echo "你无权限!";
exit();
}
}
/**
* 检查用户是否登录超时
* @param $login_time 用户登录成功时,保存在session中的用户mktime()
*/
function user_mktime($login_time){
if (mktime()-$login_time>'10') {
session_destroy();
echo "<script type='text/javascript'>alert('登陆超时!');</script>";
// echo "登陆超时!";
}else{
$_SESSION['login_time']=mktime();
}
}
?>
user.php
<?php
require_once './config.php';
if ($_POST['submit']) {
$username=str_replace(" ","",$_POST['username']);
$sql="select * from user_list where username = '$username'";
$query=mysql_query($sql);
if (is_array($row=mysql_fetch_array($query))) {
if (md5($_POST['password'].GLOBAL_PWD) == $row['password']){
if ($_POST['code']==$_SESSION['code']) {
$_SESSION['uid'] = $row['uid'];
$_SESSION['user_shell'] = md5($row['username'].$row['password'].GLOBAL_PWD);
$_SESSION['login_time']=mktime();
echo "ok";
}else{
echo "error";
}
}
}else{
echo "密码或者用户名错误!";
session_destroy();
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="no-cache" content="text/html; charset=utf-8" />
<title>Member</title>
</head>
<body>
<form action="" method="post">
用 户 名:<input type="text" name="username" /><br />
密 码:<input type="password" name="password" /><br />
验 证 码:<input type="text" name="code" size="10" /><img src="code.php" onclick="javascript:this.src='code.php?time='+Math.random();" /><br />
<input type="submit" name="submit" value="Login" />
</form>
</body>
</html>
user_system.php
<?php
header("Content-Type:text/html;charset=utf-8");
require_once './config.php';
user_shell($_SESSION['uid'],$_SESSION['user_shell'],2);
user_mktime($_SESSION['login_time']);
?>
权限内容
code.php
<?php
session_start();
require_once 'VerificationCode.class.php';
$code = new VerificationCode();
$code->showCode();
$_SESSION['code']=$code->getCode();
?>
VerificationCode.class.php
<?php
class VerificationCode{
private $charset="abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"; //随机因子
private $code; //验证码
private $codelen=4; //验证码长度
private $width=110; //宽度
private $height=30; //高度
private $img; //图像资源句柄
private $font; //制定字体
private $fontSize=25; //字体大小
private $fontColor; //字体颜色
public function __construct(){
$this->font="CALIBRIZ.TTF";
}
//生成验证码
private function createCode(){
$len=strlen($this->charset)-1;
for ($i = 0; $i < $this->codelen; $i++) {
$this->code .= $this->charset[mt_rand(0,$len)];
}
}
//生成背景
private function createBg(){
$this->img=imagecreatetruecolor($this->width,$this->height);
$color = imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
//生成文字
private function createFont(){
$x=$this->width/$this->codelen;
for ($i = 0; $i < $this->codelen; $i++) {
$this->fontColor=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
// imagettftext($this->img,$this->fontSize,mt_rand(-30,30),$i*$x+mt_rand(1,5),$this->height/1.4,$this->fontColor,$this->font,$this->code[$i]);
imagestring($this->img,5,$i*$x+mt_rand(1,5),5,$this->code[$i],$this->fontColor);
}
}
//生成线条、雪花
private function createDisturb(){
for ($i = 0; $i < 6; $i++) {
$color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->width),mt_rand(0,$this->width),mt_rand(0,$this->width),$color);
}
for ($i = 0; $i < 100; $i++) {
$color=imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
//输出
private function outPut(){
header("Content-Type:image/png");
imagepng($this->img);
imagedestroy($this->img);
}
public function showCode(){
$this->createBg();
$this->createCode();
$this->createDisturb();
$this->createFont();
$this->outPut();
}
//获取验证码
public function getCode(){
return strtolower($this->code);
}
}