首页 > 编程知识 正文

php实现验证码的制作与校验(php实现验证码的制作与校验的区别)

时间:2023-12-12 12:54:03 阅读:314681 作者:GRQG

本文目录一览:

php怎么实现验证码的

验证码功能机制实现思路

常规的验证码实现:

a、产生一张png的图片

b、为图片设置背景色

c、设置字体颜色和样式

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

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

f、加入噪点和干扰线防止注册机器分析原图片来恶意注册

g、输出图片

h、释放图片所占内存

i、将验证码保存到session或是数据库

j、将和输入的验证码进行对比

短信(邮箱)验证码机制:

a、产生4-6位数的随机的验证码

b、把产生的每个字符保存到session或是数据库

c、将验证码发送到用户的手机(邮箱)

d、用户在规定时间内进行输入

e、将验证码从session或是数据库中取出

f、将和输入的验证码进行对比验证

php验证码怎么实现

1. 新建code.php验证码生成文件

在此之前必须打开php的GD库,修改php.ini文件的配置,取消extension=php_gd2.dll前面的分号。代码如下:

?php

session_start();

//生成验证码图片

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

$im = imagecreate(44,18);

$back = ImageColorAllocate($im, 245,245,245);

imagefill($im,0,0,$back); //背景

srand((double)microtime()*1000000);

//生成4位数字

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

$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));

$authnum=rand(1,9);

$vcodes.=$authnum;

imagestring($im, 5, 2+$i*10, 1, $authnum, $font);

}

for($i=0;$i100;$i++) //加入干扰象素

{

$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($im, rand()p , rand()0 , $randcolor);

}

ImagePNG($im);

ImageDestroy($im);

$_SESSION['Checknum'] = $vcodes;

?

2. 显示验证码图片

在需要显示验证码的页面中加入

input type="text" name="passcode"

img src="code.php"

3.判断并获取验证码的值

验证码是通过第一步骤代码中的$_SESSION['Checknum'] = $vcodes;赋的值,所以验证码的值存在$_SESSION['Checknum']当中。在验证页面,使用以下代码,

...

session_start();//启动会话

$code=$_POST["passcode"];

if( $code == $_SESSION["Checknum"])

{...}即可完成验证码登录。

运行截图:

望采纳,谢谢

怎样用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);

 }

}

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