首页 > 编程知识 正文

非常实用的php验证码类(非常实用的php验证码类软件)

时间:2023-12-14 13:33:52 阅读:315627 作者:ANJH

本文目录一览:

php验证码类帮看下,谢谢很短

一、修改php.ini(保举)

memory_limit = 12M

2、在程序里面添加如下语句

ini_set(''memory_limit'', ''12M'');

3、在根目录建立

.htaccess文件,添加如下内容

php_value memory_limit 12M

如果还不能解决,就把它改得再大一些。

最后注意的是重启服务器

==========================

上面是我抄的,看到你的提问我感觉是配置的问题,但我这人记不住东西,只能找,其实你把报错的提示一搜就会有答案的

怎样用PHP制作验证码

?php

//验证码类

class ValidateCode {

 private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子

 private $code;//验证码

 private $codelen = 4;//验证码长度

 private $width = 90;//宽度

 private $height = 40;//高度

 private $img;//图形资源句柄

 private $font;//指定的字体

 private $fontsize = 20;//指定字体大小

 private $fontcolor;//指定字体颜色

 //构造方法初始化

 public function __construct() {

  $this-font = dirname(__FILE__).'/font/elephant.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),$_x*$i+mt_rand(1,5),$this-height / 1.4,$this-fontcolor,$this-font,$this-code[$i]);

  }

 }

 //生成线条、雪花

 private function createLine() {

  //线条

  for ($i=0;$i6;$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-height),mt_rand(0,$this-width),mt_rand(0,$this-height),$color);

  }

  //雪花

  for ($i=0;$i100;$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 doimg() {

  $this-createBg();

  $this-createCode();

  $this-createLine();

  $this-createFont();

  $this-outPut();

 }

 //获取验证码

 public function getCode() {

  return strtolower($this-code);

 }

}

如何用PHP生成验证码

PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有:

1、产生一张png的图片;

2、为图片设置背景色;

3、设置字体颜色和样式;

4、产生4位数的随机的验证码;

5、把产生的每个字符调整旋转角度和位置画到png图片上;

6、加入噪点和干扰线防止注册机器分析原图片来恶意破解验证码;

7、输出图片;

8、释放图片所占内存。

session_start(); 

getCode(4,60,20); 

 

function getCode($num,$w,$h) { 

    $code = ""; 

    for ($i = 0; $i  $num; $i++) { 

        $code .= rand(0, 9); 

    } 

    //4位验证码也可以用rand(1000,9999)直接生成 

    //将生成的验证码写入session,备验证时用 

    $_SESSION["helloweba_num"] = $code; 

    //创建图片,定义颜色值 

    header("Content-type: image/PNG"); 

    $im = imagecreate($w, $h); 

    $black = imagecolorallocate($im, 0, 0, 0); 

    $gray = imagecolorallocate($im, 200, 200, 200); 

    $bgcolor = imagecolorallocate($im, 255, 255, 255); 

    //填充背景 

    imagefill($im, 0, 0, $gray); 

 

    //画边框 

    imagerectangle($im, 0, 0, $w-1, $h-1, $black); 

 

    //随机绘制两条虚线,起干扰作用 

    $style = array ($black,$black,$black,$black,$black, 

        $gray,$gray,$gray,$gray,$gray 

    ); 

    imagesetstyle($im, $style); 

    $y1 = rand(0, $h); 

    $y2 = rand(0, $h); 

    $y3 = rand(0, $h); 

    $y4 = rand(0, $h); 

    imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED); 

    imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED); 

 

    //在画布上随机生成大量黑点,起干扰作用; 

    for ($i = 0; $i  80; $i++) { 

        imagesetpixel($im, rand(0, $w), rand(0, $h), $black); 

    } 

    //将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成 

    $strx = rand(3, 8); 

    for ($i = 0; $i  $num; $i++) { 

        $strpos = rand(1, 6); 

        imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black); 

        $strx += rand(8, 12); 

    } 

    imagepng($im);//输出图片 

    imagedestroy($im);//释放图片所占内存 

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。