您的位置:

php验证码编写,php实现验证码判断

本文目录一览:

php怎么编写手机短信验证码功能

以前在远标做过你的应用应该是这样吧,用户输入手机号码,点击发送短信,用户收到验证码,输入对应的验证码 判断是否正确。

需要:

申请一个短信接口,就是点击发送验证码的时候,提交到接口给该号码下发验证码。

技术方面的实现:

1、点击获取验证码

2、程序ajax post提交到短信接口

3、短信接口服务商 接口判断用户和口令,正确后,下发短信给该号码。

4、用户输入号码,程序判断验证码是否一致。

怎样制作PHP验证码

?php

//验证码:文本类型为图像

header("content-type:image/png");

define('TYPE',3);//1.字母 2.字母数字 3.数字 4.逻辑 5.汉字

session_start();

//创建画布

$img = imagecreatetruecolor(90,33);

//创建颜色

//$bgcolor = imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));

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

$textcolor = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));

//填充颜色到画布

imagefill($img,0,0,$bgcolor);

//创建但像素的点为干扰项

//for($i=0;$i100;$i++){

// $pixelcolor = imagecolorallocate($img,rand(150,200),rand(150,200),rand(150,200));

// imagesetpixel($img,rand(0,70),rand(0,30),$pixelcolor);

//}

//

////划线

//$linecolor = imagecolorallocate($img,255,0,0);

//imageline($img,0,0,70,30,$linecolor);

//

////多边形

// $col_poly = imagecolorallocate ( $img , 0 , 255 , 0 );

// imagepolygon ( $img ,

// array (

// 5 , 5,

// 5, 15 ,

// 20,15,

// 20,5

// ),

// 4 ,

// $col_poly );

////弧线

//$arcColor = imagecolorallocate ( $img , 0 , 0 , 255 );

//imagearc($img,35,15,30,30,0,360,$arcColor);

//创建验证码的内容

//#字母

$letter = range('A','Z');

$letterStr = $letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)];

//数字字母

$num = range(0,9);

$numberAndLetter = array_merge($letter,$num);

$nal = $numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)];

//#数字

$number = rand(1000,9999);

//#逻辑

$x = rand(1,9);

$y = rand(1,9);

$expression = $x."+".$y."=?";

$sum = $x+$y;

//#汉字

$CH = array('恭喜发财','财源滚滚','财源广进','才高八斗','学富五车','抬头见喜');

$chstr = $CH[rand(0,count($CH)-1)];

switch(TYPE){

case 1 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$letterStr);$_SESSION['code']=$letterStr;break;

case 2 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$nal);$_SESSION['code']=$nal;break;

case 3 : imagettftext($img,14,0,13,23,$textcolor,'MSYH.TTF',$number);$_SESSION['code']=$number;break;

case 4 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$expression);$_SESSION['code']=$sum;break;

case 5 : imagettftext($img,11,0,6,21,$textcolor,'MSYHBD.TTF',$chstr);$_SESSION['code']=$chstr;break;

}

//输入图像到浏览器

imagepng($img);

?

php如何实现验证码?许昌鲤鱼IT计算机电脑软件编程培训中心

验证码在表单实现越来越多了,但是用js的写的验证码,总觉得不方便,所以学习了下php实现的验证码。好吧,其实是没有事情干,但是又不想浪费时间,所以学习了下php实现验证码。正所谓,技多不压身。而且,也可以封装成一个函数,以后使用的时候也是很方便的,当然现在未封装。

现在来说说简单的纯数字验证码吧。

如果是初学者,建议按照我代码的注释 //数字 一步步来。最简单的方法,还是把整个代码复制走了。

新建一个captcha.php:

php //10设置session,必须处于脚本最顶部

session_start(); $image = imagecreatetruecolor(100, 30); //1设置验证码图片大小的函数

//5设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);

$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff

//6区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色

imagefill($image, 0, 0, $bgcolor); //10设置变量

$captcha_code = ""; //7生成随机数字

for($i=0;$i4;$i++){ //设置字体大小

$fontsize = 6;

//设置字体颜色,随机颜色

$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜色

//设置数字

$fontcontent = rand(0,9); //10.=连续定义变量

$captcha_code .= $fontcontent;

//设置坐标

$x = ($i*100/4)+rand(5,10); $y = rand(5,10);

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);

} //10存到session

$_SESSION['authcode'] = $captcha_code; //8增加干扰元素,设置雪花点

for($i=0;$i200;$i++){ //设置点的颜色,50-200颜色比数字浅,不干扰阅读

$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));

//imagesetpixel — 画一个单一像素

imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);

} //9增加干扰元素,设置横线

for($i=0;$i4;$i++){ //设置线的颜色

$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //设置线,两点一线

imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);

} //2设置头部,image/png

header('Content-Type: image/png'); //3imagepng() 建立png图形函数

imagepng($image); //4imagedestroy() 结束图形函数 销毁$image

imagedestroy($image);

接着就是静态页的代码了:index.html

doctype htmlhtml

head

meta http-equiv="Content-Type" content="text/html; charset=UTF-8"

title确认验证码title

head

body

form method="post" action="./form.php"

p验证码: img id="captcha_img" border='1' src='./captcha.php?r=echo rand(); ?' style="width:100px; height:30px" / a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()"换一个?a

p

P请输入验证码:input type="text" name='authcode' value=''/p

pinput type='submit' value='提交' style='padding:6px 5px;'/p

bodyhtml

从index.html可以看到,提交的表单是到form.php的,所以还要有一个判断的form.php代码:

php header("Content-Type:text/html;charset=utf-8"); //设置头部信息

//isset()检测变量是否设置

if(isset($_REQUEST['authcode'])){ session_start(); //strtolower()小写函数

if(strtolower($_REQUEST['authcode'])== $_SESSION['authcode']){ //跳转页面

echo "script language=\"javascript\""; echo "document.location=\"./form.php\""; echo "/script";

}else{ //提示以及跳转页面

echo "script language=\"javascript\""; echo "alert('输入错误!');"; echo "document.location=\"./form.php\""; echo "/script";

} exit();

}

那么,纯数字的实现了,数字加英文的也应该不难了。要修改的代码 只是在 captcha.php 将 //7生成随机数字 修改成 //7生成随机的字母和数字,如果你真的很可爱的就修改这几个字就认为可以实现的话,那么祝贺你,你永远保持快乐。脑残儿童欢乐多。

废话不多说了,拉代码吧。

php //10设置session,必须处于脚本最顶部

session_start(); $image = imagecreatetruecolor(100, 30); //1设置验证码图片大小的函数

//5设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);

$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff

//6区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色

imagefill($image, 0, 0, $bgcolor); //10设置变量

$captcha_code = ""; //7生成随机的字母和数字

for($i=0;$i4;$i++){ //设置字体大小

$fontsize = 8;

//设置字体颜色,随机颜色

$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜色

//设置需要随机取的值,去掉容易出错的值如0和o

$data ='abcdefghigkmnpqrstuvwxy3456789'; //取出值,字符串截取方法 strlen获取字符串长度

$fontcontent = substr($data, rand(0,strlen($data)),1); //10.=连续定义变量

$captcha_code .= $fontcontent;

//设置坐标

$x = ($i*100/4)+rand(5,10); $y = rand(5,10);

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);

} //10存到session

$_SESSION['authcode'] = $captcha_code; //8增加干扰元素,设置雪花点

for($i=0;$i200;$i++){ //设置点的颜色,50-200颜色比数字浅,不干扰阅读

$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));

//imagesetpixel — 画一个单一像素

imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);

} //9增加干扰元素,设置横线

for($i=0;$i4;$i++){ //设置线的颜色

$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //设置线,两点一线

imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);

} //2设置头部,image/png

header('Content-Type: image/png'); //3imagepng() 建立png图形函数

imagepng($image); //4imagedestroy() 结束图形函数 销毁$image

imagedestroy($image);

其他的两个页面,不许要修改。

一般而言,现在就已经够用了。但是就像动漫一样,总会有番外。

那么,我们来个汉字的番外吧。其实我也准备将汉字的验证码放到我的毕业设计里面,虽然现在很流行滑动验证码,但是本人毕竟不是专门学习js的。

而且,还可以和答辩的老师说,我们验证码不需要素材,连图片也是生成的,用自己的知识装13,也没有设么的。

php //11设置session,必须处于脚本最顶部

session_start(); //1设置验证码图片大小的函数

$image = imagecreatetruecolor(200, 60);

//5设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);

$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff

//6区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色

imagefill($image, 0, 0, $bgcolor); //7设置ttf字体

$fontface = 'FZYTK.TTF'; //7设置字库,实现简单的数字储备

$str='天地不仁以万物为刍狗圣人不仁以百姓为刍狗这句经常出现在控诉暴君暴政上地残暴不仁把万物都当成低贱的猪狗来看待而那些高高在上的所谓圣人们也没两样还不是把我们老百姓也当成猪狗不如的东西但实在正取的解读是地不情感用事对万物一视同仁圣人不情感用事对百姓一视同仁执子之手与子偕老当男女主人公含情脉脉看着对方说了句执子之手与子偕老女方泪眼朦胧含羞地回一句讨厌啦这样的情节我们是不是见过很多但是我们来看看这句的原句死生契阔与子成说执子之手与子偕老于嗟阔兮不我活兮于嗟洵兮不我信兮意思是说战士之间的约定说要一起死现在和我约定的人都走了我怎么活啊赤裸裸的兄弟江湖战友友谊啊形容好基友的基情比男女之间的爱情要合适很多吧'; //str_split()切割字符串为一个数组,一个中文在utf_8为3个字符

$strdb = str_split($str,3);

//11

$captcha_code = ''; //8生成随机的汉子

for($i=0;$i4;$i++){ //设置字体颜色,随机颜色

$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜色

//随机选取中文

$in = rand(0,count($strdb)); $cn = $strdb[$in]; //将中文记录到将保存到session的字符串中

$captcha_code .= $cn; /*imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color,

string $fontfile ,string $text ) 幕布 ,尺寸,角度,坐标,颜色,字体路径,文本字符串

mt_rand()生成更好的随机数,比rand()快四倍*/

imagettftext($image, mt_rand(20,24),mt_rand(-60,60),(40*$i+20),mt_rand(30,35),$fontcolor,$fontface,$cn);

} //11存到session

$_SESSION['authcode'] = $captcha_code; //9增加干扰元素,设置点

for($i=0;$i200;$i++){ //设置点的颜色,50-200颜色比数字浅,不干扰阅读

$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));

//imagesetpixel — 画一个单一像素

imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor);

} //10增加干扰元素,设置线

for($i=0;$i4;$i++){ //设置线的颜色

$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //设置线,两点一线

imageline($image,rand(1,199), rand(1,59),rand(1,199), rand(1,59),$linecolor);

} //2设置头部,image/png

header('Content-Type: image/png'); //3imagepng() 建立png图形函数

imagepng($image); //4imagedestroy() 结束图形函数 销毁$image

imagedestroy($image);

其他的页面也是不需要修改的。

效果图如下:

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生成验证码的类怎么写?

?php

class Code{

// 1. 定义各个成员 有宽、高、画布、字数、类型、画类型

private $width; //宽度

private $height; //高度

private $num; //验证码字数

private $imgType; //生成图片类型

private $Type; //字串类型 1,2,3 三个选项 1 纯数字 2 纯小写字母 3 大小写数字混合

private $hb; //画布

public $codestr; // 验证码字串

public function __construct($height=20,$num=4,$imgType="jpeg",$Type=1){

$this-width = $num*20;

$this-height = $height;

$this-num = $num;

$this-imgType = $imgType; 

$this-Type = $Type; 

$this-codestr = $this-codestr();

$this-zuhe();

}

// 2. 定义随机获取字符串函数

private function codestr(){

switch($this-Type){

case 1: // 类型为1 获取1-9随机数

$str = implode("",array_rand(range(0,9),$this-num));

break;

case 2: // 类型为2 获取a-z随机小写字母

$str = implode("",array_rand(array_flip(range(a,z)),$this-num));

break;

case 3: // 类型为3 获取数字,小写字母,大写字母 混合

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

$m = rand(0,2);

switch($m){

case 0:

$o = rand(48,57);

break;

case 1:

$o = rand(65,90);

break;

case 2:

$o = rand(97,122);

break; 

}

$str .= sprintf("%c",$o);

}

break; 

}

return $str; 

}

// 3. 初始化画布图像资源

private function Hb(){

$this-hb = imagecreatetruecolor($this-width,$this-height); 

}

// 4. 生成背景颜色

private function Bg(){

return imagecolorallocate($this-hb,rand(130,250),rand(130,250),rand(130,250)); 

}

// 5. 生成字体颜色

private function Font(){

return imagecolorallocate($this-hb,rand(0,100),rand(0,100),rand(0,100)); 

}

// 6. 填充背景颜色

private function BgColor(){

imagefilledrectangle($this-hb,0,0,$this-width,$this-height,$this-Bg()); 

}

// 7. 干扰点

private function ganrao(){

$sum=floor(($this-width)*($this-height)/3);

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

imagesetpixel($this-hb,rand(0,$this-width),rand(0,$this-height),$this-Bg()); 

}

}

// 8. 随机直线 弧线

private function huxian(){

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

imageArc($this-hb,rand(0,$this-width),rand(0,$this-height),rand(0,$this-width),rand(0,$this-height),rand(0,360),rand(0,360),$this-Bg()); 

}

// 9. 写字

private function xiezi(){

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

$x=ceil($this-width/$this-num)*$i; 

$y=rand(1,$this-height-15);

imagechar($this-hb,5,$x+4,$y,$this-codestr[$i],$this-Font());

}

// 10. 输出

private function OutImg(){

$shuchu="image".$this-imgType; 

$header="Content-type:image/".$this-imgType;

if(function_exists($shuchu)){

header($header);

$shuchu($this-hb); 

}else{

exit("GD库没有此类图像"); 

}

}

// 11. 拼装

private function zuhe(){

$this-Hb();

$this-BgColor();

$this-ganrao();

$this-huxian();

$this-xiezi();

$this-OutImg(); 

}

public function getCodeStr(){

return $this-codestr; 

}

}

$a = new Code();

$a -getCodeStr();

?

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实现验证码判断

2023-01-08
php会话验证(php实现验证码)

2022-11-09
php验证head,php验证码错误

2023-01-04
php显示验证码,php 短信验证码

2023-01-06
js验证与php验证的区别,php实现验证码判断

2022-11-16
php写了验证码,php写验证码随机生成字母数字

2022-11-29
php验证库,php验证器

2023-01-06
php验证google(php验证码代码怎么写)

2022-11-12
phpjs验证登录(php网络验证源码)

本文目录一览: 1、PHP 验证网页跳转到登陆页面后登陆页面JS运行不完全 2、js 判断是否登录 3、php简单的登陆验证用户名和密码怎么写 4、求助,php+js实现登录验证 5、用PHP+JS+

2023-12-08
php不为空验证(php判断空)

2022-11-09
php编程每天必学之验证码,php验证码功能怎么实现

2022-11-29
phpjs验证登录,php手机验证码登录

本文目录一览: 1、求助,php+js实现登录验证 2、用PHP+JS+MYSQL实现用户登陆验证,的具体步骤是怎么样的呢 3、php简单的登陆验证用户名和密码怎么写 4、PHP 中JS验证表单的问题

2023-12-08
php支持验证码,PHP生成验证码

2023-01-04
php版本验证码识别,PHP生成验证码

2022-11-20
php发验证码,Php网络验证源码

2022-12-01
php打造验证码,php识别验证码

2023-01-07
phpjquery验证码,php验证码生成

2022-11-27
php登录验证模块,php验证码如何实现登录验证

2022-11-25
php验证后提交,php签名验证

2023-01-07
php的验证码类,PHP生成验证码

2022-11-21