当前位置:文档之家› php开发实例---用户登陆模块的实现(用户权限的控制、验证码的生成)

php开发实例---用户登陆模块的实现(用户权限的控制、验证码的生成)

PHP开发一个用户登陆模块,关键技术(session会话、cookie应用、GD库应用)大家好,今日我们一起来开发一个PHP随机验证码的生成实例。

首先,我们来看一下效果:开发随机验证码使用到的函数有以下几个。

如果有不明白这些函数的使用方法的朋友,请参考PHP5开发手册。

1.imagecreatetruecolor(); //创建一个真彩的画布2.imagecolorallocate(); //分配颜色3.imagefill(); //填充颜色4.imagerectangle(); //画一个矩形,用于绘制验证码的边框5.imagettftext(); //将文本内容写入到图像中6.imagesetpixel(); //绘制一个单一像素6imageline(); //绘制一条线7.imagepng(); //输出图像8.imagedesctroy(); //销毁图像,释放内容PHP开发随机验证码的步骤与思路1.获取随机验证码的内容2.创建一个真彩画布,分配字体颜色跟背景颜色3.开始绘画4.输出图像5.销毁图像6.自定义一个函数用来获取随机验证码(本人目前在淘宝网上注册了一家小小的店铺,主要营业服装之类的商品。

有兴趣有需要的朋友请访问:可以直联系到本人!)接下来我们开始开发,在网站的根目录下新建一个PHP文件(code.php):<?phpsession_start();//生成随机验证码$num=4;$str=getCode($num,0);$_SESSION["code"]=$str;//1.创建图像,定义颜色$width=$num*20;$height=25;$im=imagecreatetruecolor($width,$height);//1.1字体颜色$color[] = imagecolorallocate($im,111,0,55);$color[] = imagecolorallocate($im,0,77,0);$color[] = imagecolorallocate($im,0,0,160);$color[] = imagecolorallocate($im,221,111,0);$color[] = imagecolorallocate($im,220,0,0);//1.2背景颜色$bgcolor = imagecolorallocate($im,240,240,240);//2.开始绘画,使用imagefill()、imagerectangle()函数给图像增加背景颜色与边框颜色imagefill($im,0,0,$bgcolor);imagerectangle($im,0,0,$width-1,$height-1,$color[rand(0,4)]);//2.1使用imagettftext()函数,将文本内容写入到图像中for($i=0;$i<$num;$i++){imagettftext($im,14,rand(-20,20),5+(18*$i),20,$color[rand(0,4)],"msyh.ttf",$str[$i]); }//2.2使用imagesetpixel()增加小于100个的干扰点for($i=0;$i<100;$i++){//随机颜色$c=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));imagesetpixel($im,rand(0,$width),rand(0,$height),$c);}//2.3使用imageline()增加小于$num个数的干扰线for($i=0;$i<$num;$i++){//随机颜色$c=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));imageline($im,rand(0,$width),rand(0,$height),rand(0,$width),rand(0,$height),$c); }//3.输出图像,通过header()函数,告诉浏览器输出PNG图像格式,而不是文本形式header("Content-Type:image/png");imagepng($im);//4.销毁图像,释放内容imagedestroy($im);//5.自定义函数,获取随机验证码functiongetCode($m=4,$type=0){//随机验证码的内容$str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//各个类型验证码索引数组的结束位置,9的位置是纯数字结束位置,35的位置为小写字母的结束位置,62的位置为大写字母的结束位置//因位置是从0开始计算,所以最后的位置应减1.$t=array(9,35,strlen($str)-1);//从$str字符串中,生成随机验证码$c="";for($i=0;$i<$m;$i++){$c.=$str[rand(0,$t[$type])];}//将生成的验证码赋值于SESSION会话,实现不同页面传递return $c;}项目完成到这一步的时候,随机验证码已经制作完成了!接下来我们来制作LOGIN登陆页面LOGIN.PHP。

使用到的CSS、JS文件内容都在下面提供,使用到的图片:1:(login_button.jpg)2:(bg_title.jpg)3:(bg_user.jpg)<?phpsession_start();if(isset($_POST["login_x"])){$username=trim($_POST["username"]);$password=md5($_POST["password"]);if($_POST["code"] != $_SESSION["code"]){echo "<script>alert('验证码错误!');history.back();</script>";exit;}//登陆有效期,如果选择是7天,则保留时间为604800秒,如果选择的是1个月,则保留时间为2592000秒。

否则,保留为1小时if($_POST["radiobutton"]==1){$time=time()+604800;}if($_POST["radiobutton"]==2){$time=time()+2592000;}$sql="select id from web_user where username='$username' and password='$password'";include "dbconfig.inc.php";$result=$mysqli->query($sql);if($result->num_rows> 0){setCookie("username",$username,$time);setCookie("isLogin",1,$time);echo "<script>window.location.href='index.php?lid=$_POST[radiobutton]';</script>";}else{echo "<script>alert('用户名或密码有误!');history.back();</script>";exit;}}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>用户登陆</title><link rel="stylesheet" type="text/css" href="css/login.css"></style><script type="text/javascript" src="js/checkinput.js"></script></head><body><div class="login"><div id="tit">用户登陆</div><div class="login_body"><form action="login.php" method="post" onsubmit="return check(this)"><div id="username">用户名:<input type="text" name="username" size="30" class="login_id"/></div><div id="username">密&nbsp;&nbsp;码:<input name="password" type="password" class="login_id" size="30" /></div><div id="username">验证码:<input name="code" type="text" size="8" />&nbsp;<!—验证码的调用,鼠标经过时变成手型,点击自动更换不同的验证码--><imgsrc="phpcode/code.php" name="img" align="absmiddle" onclick="this.src='phpcode/code.php?id='+Math.random()" style="cursor:pointer"/> </div><div id="username">有效期:<input name="radiobutton" type="radio" value="1" checked="checked" />7天<input type="radio" name="radiobutton" value="2" />1个月</div><div id="login_button"><input type="image" src="images/login_button.jpg" name="login" /></div><div id="login_button"><a href="forgetpass.php">忘记密码?</a> | <a href="reg.php">免费注册</a></div></form></div></div></body></html>LOGIN.PHP页面CSS文件内容LOGIN.CSS<!--.login{width:330px;margin:0px;padding-top:50px;margin:auto;font-size:12px;}#tit{width:300px;height:34px;border:1px #ccc solid;margin:auto;line-height:34px;padding-left:30px;font-family:Geneva, Arial, Helvetica, sans-serif;color:#000066;font-size:15px;font-weight:bold;background:url(../images/bg_title.jpg) repeat-x;}.login_body{width:300px;height:auto;padding-left:30px;padding-top:10px;line-height:30px;border-left:1px #ccc solid;border-right:1px #ccc solid;border-bottom:1px #ccc solid;background:url(../images/bg_user.jpg) repeat;}.login_body #username{height:30px;line-height:30px;font-size:12px;margin-bottom:5px;}#login_button{font-size:12px;padding-left:49px;margin-bottom:5px;}.login_id{width:200px;}a:link{color:#000000;text-decoration:none;}a:hover{color:#FF0000;text-decoration:none;}LOGIN.PHP页面判断用户输入是否合法的JS代码内容checkinput.js function check(form){if(ername.value == ""){alert('请输入用户名!');ername.select();return false;}if(form.password.value == ""){alert('请输入密码!');form.password.select();return false;}if(form.code.value == ""){alert('请输入验证码!');form.code.select();return false;}}项目完成到这一步,可以在浏览器上查看效果:接下来,为了站点的安全必须制作一个安公共安全配置文件判断用户是否正常登陆的安全公共配置文件comm.inc.php<?phpif(!$_COOKIE["isLogin"]){header("Location:login.php");}MYSQL连接文件dbconfig.inc.php<?php$mysqli=new mysqli(“localhost”,”root”,”saiheadmin”,”xsphp”);?>主页面的显示index.php<?phpinclude "comm.inc.php";include "dbconfig.inc.php";$username=$_COOKIE["username"];$time=$_GET['lid'];if($time==1){$str="<b><font color='red'>7天</font></b>";}if($time==2){$str="<b><font color='red'>1个月</font></b>";}$sql="select allow_1,allow_2,allow_3,allow_4 from web_user where username='{$username}'";$result=$mysqli->query($sql);$user=$result->fetch_array();if($user[allow_1] && $user[allow_2] && $user[allow_3] && $user[allow_4]){ $power="<b><font color='green'>超级管理员</font></b>";}if(($user[allow_1])==0 && $user[allow_2]==0 && $user[allow_3]==0 && $user[allow_4]==0){ $power="<b><font color='green'>普通浏览者</font></b>";}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>主页面</title></head><body><p>欢迎,<b><font color="green"><?php echo $username;?></font></b>光临本站!您帐户登陆有效期为<?php echo $str;?>。

相关主题