<?php class db{ private $hostname; private $dbname; private $username; private $password; private $recourse; function __construct($hostname,$dbname,$username,$password){ $this->hostname=$hostname; $this->dbname=$dbname; $this->username=$username; $this->password=$password; $this->connect(); } function connect(){ $this->recourse=mysql_connect($this->hostname,$this->username,$this->password); mysql_select_db($this->dbname); mysql_query('set names utf8'); } function getrow($sql){ $result = mysql_query($sql); return mysql_fetch_assoc($result);//从结果集中取得一行作为关联数组 } function __destruct(){ mysql_close($this->recourse); } }
<?php class Captcha{ private $width; //验证码的宽度 private $height; //验证码的高度 private $strNum; //验证码字符的数量 private $image; private $disTurbNum; public function __construct($width,$height,$strNum){ $this->width = $width; $this->height = $height; $this->strNum = $strNum; $this-> disTurbNum = $this->width * $this->height/50; //像素密度 } public function showImg(){ //创建矩形框(背景颜色) $this->createBg(); //设置干扰元素(像素点、线条) $this->setDisturb(); //输出文字到矩形框 $this->outPutStr(); //输出\保存图像 $this->outPutImg(); } function createBg(){ //创建画布 $this->image = imagecreatetruecolor($this->width,$this->height); //分配画笔颜色 $bgColor =imagecolorallocate($this->image,mt_rand(50,255),mt_rand(50,255),mt_rand(50,255)); //填充颜色 imagefill($this->image,0,0,$bgColor); //给背景创建一个边框 $borderColor = imagecolorallocate($this->image,0,0,0); imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border); } //设置干扰元素 private function setDisturb(){ //像素点 for($i=0;$i<$this->disTurbNum;$i++){ $pxColor = imagecolorallocate($this->image,mt_rand(100,225),mt_rand(100,225),mt_rand(100,225)); imagesetpixel($this->image,mt_rand(1,$this->width-2),mt_rand(1,$this->height-2),$pxColor); } //线条 for($j=0;$j<20;$j++){ $lineColor = imagecolorallocate($this->image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200)); imageline($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$lineColor); } } private function outPutStr(){ //a-z A-Z 0-9 $lower_str = range('a','z'); //获得 从a-z之间的所有的单元 $upper_str = range('A','Z'); $num = range(0,9); //把这些数组合并 $new_all = array_merge($lower_str,$upper_str,$num); //随机获得几个数组下标 $keys = array_rand($new_all,$this->strNum); $str = ''; //循环遍历下标,找到对应的元素 foreach($keys as $value){ $str .= $new_all[$value]; } $strColor = imagecolorallocate($this->image,255,255,255); imagestring($this->image,5,50,3,$str,$strColor); //通常是保存到session $_SESSION['str'] = $str; } function getstr($userstr){ if($userstr== $_SESSION['str']){ return true; }else{ return false; } } private function outPutImg(){ header('Content-Type:image/png'); imagepng($this->image); } }
<?phpsession_start();header('Content-Type:text/html;charset=utf-8');if(isset($_GET['act'])){require 'includes/Captcha.class.php';$captcha=new Captcha(145,20,5);$captcha->showImg();}//验证验证码if(isset($_POST['sub'])){require 'includes/Captcha.class.php';$captcha=new Captcha(145,20,5);$userstr=$_POST['captcha'];$result=$captcha->getstr($userstr);if($result){//验证名字//验证密码$username=$_POST['username'];$password=$_POST['password'];require 'includes/db.class.php';$db=new db('localhost','ecshoptest','root','');$sql="select *from user where username='$username' and password='$password' ";$row=$db->getrow($sql);//var_dump($row);//if($row){header('location:index1.php');}else{echo'用户名或密码不正确,请重新输入';}is_array($row)?header('location:index1.php'):$alert='用户名或密码不正确,请重新输入';//三目运算符判断返回值是否为数组 如果不是给alert赋值}else{ echo redirect_url('验证码错误','privilege.php');}}function redirect_url($message,$url){if($message){$alert_str = "alert('$message')";}else{$alert_str = '';}echo <<<STR<script type="text/javascript">$alert_strwindow.location.href="$url";</script>STR;}?><html xmlns="http://www.w3.org/1999/xhtml"><head><title>ECSHOP 管理中心</title><meta content="text/html; charset=utf-8" http-equiv="Content-Type"><link type="text/css" rel="stylesheet" href="styles/general.css"><link type="text/css" rel="stylesheet" href="styles/main.css"><style type="text/css">body { color: white;}</style><script src="../js/utils.js" type="text/javascript"></script><script src="js/validator.js" type="text/javascript"></script><script language="JavaScript"><!--// 这里把JS用到的所有语言都赋值到这里var process_request = "正在处理您的请求...";var todolist_caption = "记事本";var todolist_autosave = "自动保存";var todolist_save = "保存";var todolist_clear = "清除";var todolist_confirm_save = "是否将更改保存到记事本?";var todolist_confirm_clear = "是否清空内容?";var user_name_empty = "管理员用户名不能为空!";var password_invaild = "密码必须同时包含字母及数字且长度不能小于6!";var email_empty = "Email地址不能为空!";var email_error = "Email地址格式不正确!";var password_error = "两次输入的密码不一致!";var captcha_empty = "您没有输入验证码!";if (window.parent != window){ window.top.location.href = location.href;}//--></script></head><body style="background: #278296"><form οnsubmit="return validate()" name="theForm" action="privilege.php" method="post"> <table cellspacing="0" cellpadding="0" align="center" style="margin-top: 100px"> <tbody><tr> <td><img width="178" height="256" border="0" alt="ECSHOP" src="images/login.png"></td> <td style="padding-left: 50px"> <table> <tbody><tr> <td>管理员姓名:</td> <td><input type="text" name="username"></td><?if(isset($alert))echo $alert ?> </tr> <tr> <td>管理员密码:</td> <td><input type="password" name="password"></td> </tr> <tr> <td>验证码:</td> <td><input type="text" name="captcha"></td> </tr> <tr> <td align="right" colspan="2"><img width="145" height="20" border="1" title="看不清?点击更换另一个验证码。" style="cursor: pointer;" οnclick="this.src='privilege.php?act=captcha&'+Math.random()" alt="CAPTCHA" src="privilege.php?act=captcha"> </td> </tr> <tr><td colspan="2"><input type="checkbox" id="remember" name="remember" value="1"><label for="remember">请保存我这次的登录信息。</label></td></tr> <tr><td> </td><td><input type="submit" class="button" name="sub" value="进入管理中心"></td></tr> <tr> <td align="right" colspan="2">» <a style="color:white" href="../">返回首页</a> » <a style="color:white" href="get_password.php?act=forget_pwd">您忘记了密码吗?</a></td> </tr> </tbody></table> </td> </tr> </tbody></table> <input type="hidden" value="signin" name="act"></form><script language="JavaScript"><!-- document.forms['theForm'].elements['username'].focus(); /** * 检查表单输入的内容 */ function validate() { var validator = new Validator('theForm'); validator.required('username', user_name_empty); //validator.required('password', password_empty); if (document.forms['theForm'].elements['captcha']) { validator.required('captcha', captcha_empty); } return validator.passed(); } //--></script></body></html>