首页 > 编程知识 正文

php校验isbn码的函数代码的简单介绍

时间:2023-12-07 20:37:48 阅读:313149 作者:TOFJ

本文目录一览:

  • 1、php如何实现验证码?许昌鲤鱼IT计算机电脑软件编程培训中心
  • 2、php的验证码提示怎样制作
  • 3、php的图片验证码代码

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的验证码提示怎样制作

一般制作验证码会按照下面的几步走:

一:创建出来一个图片,通常我们成为源,可以用imagecreatetruecolor()这个函数搞定

二:给这个源 添加背景色,同时设置文本显示的颜色,GD库函数为我们提供了imagecolorallocate()函数

三:材料弄好了,我们要给它添点内容了,就是我们随机生成的数字或者字母,甚至可以是它们的组合,这里我们可以选择两个函数 imagettftext()、imagesrting(),这两个函数的不同,我们会在后面讲解。

例:

?php

session_start();//开启session,用来记录获得的验证码,这个函数要写在程序的开头,不然会出现错误

header(“Content-type :image/gif”);//把文件的返回类型设为image/gif格式,这个格式可以输出图片

$codelen=4;//设置你要让用户输入字符的个数,一般为4,过长用户体验不好。

$charset =”ABCDEFGHKLMNPRSTUVWYZ23456789″;//我们可以尽量把一些难以辨认的字符去掉,比如阿拉伯数字0和字母o,这也是提高用户体验的一种方法。

$code =”;

for($i=0;$i$codelen;$i++){//用for循环得到4个随机的字符,在这里用到了mt_rand,这个函数比rand的效率要高的多,建议大家用这个

$code .=$charset{mt_rand(0,strlen($charset)-1)};

}

$_SESSION['code']=$code;//下篇关于session验证的文章将会用到

$width = 80;

$height = 40;

$im = imagecreatetruecolor($width,$height);//用imagecreatetruecolor()函数来建立一个新的图片,里面的两个数值分别是宽度和高度,这是制作验证码的第一步

$bg = imagecolorallocate($im,255,255,0); //图片背景的颜色,这里是第二步

$textcolor = imagecolorallocate($im,255,0,0);//文字的颜色

imagefill($im,0,0,$bg);//给图片填充背景色

//好了上面的铺垫任务做的差不多了,现在关键就是让字符显示在图片上,这里有两种方法我们一一介绍。

$font =”ggbi.ttf”;//如果你有字体的话 就填上字体的相对路径,如果没有就留空。下面的两个用法我会一一讲解。

if($font!==”"){

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

imagettftext($im,mt_rand(12,16),(mt_rand(0,75)+330)%360,5+15*$num,20+mt_rand(2,5),$textcolor,$font,$code[$num]);//这里是第三步

}

}

else{

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

imagestring($im,5,10+15*$num,10+mt_rand(0,5),$code[$num],$textcolor);

}

}

header(“Content-type: image/jpeg”);

imagejpeg($im);

?

php的图片验证码代码

这个是phpcms的验证码,经过十几万个网站经验的,非常好用

?php

session_start();

$enablegd = 1;

//判断图像处理函数是否存在

$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');

foreach($funcs as $func)

{

if(!function_exists($func))

{

$enablegd = 0;

break;

}

}

ob_clean(); //清理缓冲

if($enablegd)

{

//create captcha

$consts = 'cdfgkmnpqrstwxyz23456';

$vowels = 'aek23456789';

for ($x = 0; $x 6; $x++)

{

$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //获取$consts中的一个随机数

$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //获取$vowels中的一个随机数

}

$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];

$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //显示4个字符

$imageX = strlen($radomstring)*8; //图像的宽

$imageY = 20; //图像的高

$im = imagecreatetruecolor($imageX,$imageY); //新建一个真彩色图像

//creates two variables to store color

$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色

$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),

imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),

imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),

imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))

);

$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配颜色并说明透明度

$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中间背景

$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中间背景2

//与左上角的颜色相同的都会被填充

imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));

//往图像上写入文字

imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[0]);

imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ARIALNI.TTF', $string[1]);

imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[2]);

imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/arial.ttf', $string[3]);

//画边框

$border = imagecolorallocate($im, 133, 153, 193);

imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);

//画一些随机出现的点

$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));

for ($i=0;$i80;$i++)

{

imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);

}

//画随机出现的线

for ($x=0; $x9;$x++)

{

if(mt_rand(0,$x)%2==0)

{

imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //画线

imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //画椭圆

}

else

{

imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));

imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);

}

}

//output to browser

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

imagepng($im);

imagedestroy($im);

}

else

{

$files = glob(XINCHENG_ROOT.'images/checkcode/*.jpg');

if(!is_array($files)) die('请检查文件目录完整性:/images/checkcode/');

$checkcodefile = $files[rand(0, count($files)-1)]; //随机其中一个文件

$_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4); //获得文件名

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

include $checkcodefile;

}

?

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