一般的方法:
checkNumbers.php
<?php session_start(); if($act == "init") { header("content-type: image/png"); //srand(microtime() * 100000); //$login_check_number = strval(rand("1111","9999")); $login_check_number = generate_password( 4 ); session_register("login_check_number"); //这里是使用了session来保存校验码. //当然也可以用cookie //setcookie("login_check_number",$login_check_number); //然后将第一行的session_start()删除; //不推荐使用cookie,因为使用cookie并不能进行安全的验证. $h_img = imagecreate(40,17); $c_black = imagecolorallocate($h_img, 0,0,0); $c_white = imagecolorallocate($h_img, 255,255,255); imageline($h_img, 1, 1, 350, 25, $c_black); imagearc($h_img, 200, 15, 20, 20, 35, 190, $c_white); imagestring($h_img, 5, 2, 1, $login_check_number, $c_white); imagepng($h_img); imagedestroy($h_img); die(); } function generate_password( $length = 4 ) { // 密码字符集,可任意添加你需要的字符 $chars = '0123456789'; //$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $password = ''; for ( $i = 0; $i < $length; $i++ ) { // 这里提供两种字符获取方式 // 第一种是使用 substr 截取$chars中的任意一位字符; // 第二种是取字符数组 $chars 的任意元素 // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); $password .= $chars[ mt_rand(0, strlen($chars) - 1) ]; } return $password; } ?>
<? session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>登陆页面</title> </head> <? include_once('./checkNumbers.php'); if(isset($_POST['number'])) { $number = $_POST['number']; echo $number.' + '; if($number != $login_check_number ) { print("验证码不正确!"); die(); } else printf("Successful."); } ?> <?php //应用方法: //在HTML文件中参加在登陆校验PHP页面中参加以下代码(留心:在参加代码前不能有输出,由于应用了SESSION) //$number 是你输进的校验码的值 //检验校验码 ?> <body> <form method="post"> <input type="text" name= "number" maxlength = "4"><img src=checkNumbers.php?act=init> <input type="submit" name="submit" /> </form> </body> </html>
<?php class ValidationCode{ private $width; private $height; private $codeNum; private $image; private $disturbColorNum; private $checkCode; function __construct($width=80,$height=40,$codeNum=4){ $this->width=$width; $this->height=$height; $this->codeNum=$codeNum; $this->checkCode=$this->createCheckCode(); $number=floor($width*$height/15); if($number>240-$codeNum){ $this->disturbColorNum=240-$codeNum; } else{ $this->disturbColorNum=$codeNum; } } //通过访问该方法向浏览器输出图像 function showImage(){ //创建图片背景 $this->createImage(); //创建干扰元素 $this->setDisturbColor(); //输出文本 $this->outputText(); //输出图像 $this->outputImage(); } //通过调用该方法获取随机创建的验证码字符串 function getCheckCode(){ return $this->checkCode; } //创建背景图片 private function createImage(){ //创建图片资源 $this->image=imagecreatetruecolor($this->width,$this->height); //随机获得背景色 $backColor=imagecolorallocate($this->image,rand(100,255),rand(120,255),rand(120,255)); //填充背景色 imagefill($this->image,0,0,$backColor); //设置边框颜色 $border=imagecolorallocate($this->image,0,0,0); //画出矩形边框 imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border); } //创建干扰元素 private function setDisturbColor(){ //设置像素点 for($i=0;$i<$this->disturbColorNum;$i++){ $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color); } //设置干扰线条,设为10条 for($i=0;$i<10;$i++){ //颜色设置 $color=imagecolorallocate($this->image,rand(2,255),rand(2,244),rand(2,244)); imagearc($this->image,rand(-10,$this->width),rand(-10,$this->height),rand(20,200),rand(30,300),39,34,$color); } } //创建验证字符 private function createCheckCode(){ $code="123456789QWERTYUIPLKJHGFDSAZXCVBNM"; $string=''; for($i=0;$i<$this->codeNum;$i++){ $char=$code{rand(0,strlen($code)-1)}; $string.=$char; } return $string; } //创建输出的字符 private function outputText(){ for($i=0;$i<$this->codeNum;$i++){ $fontcolor=imagecolorallocate($this->image,rand(0,125),rand(1,124),rand(2,125)); $fontsize=rand(2,5); $x=floor($this->width/$this->codeNum)*$i+3; $y=rand(0,$this->height-15); imagechar($this->image,$fontsize,$x,$y,$this->checkCode{$i},$fontcolor); } } private function outputImage(){ if(imagetypes()&IMG_GIF){ header("Content-Type:image/gif"); imagepng($this->image); } else if(imagetypes()&IMG_JPG){ header("Content-Type:image/jpeg"); imagepng($this->image); } else if(imagetypes()&IMG_PNG){ header("Content-Type:image/png"); imagepng($this->image); } } function __destruct(){ imagedestroy($this->image); } } ?>