网站注册、登录又或许是留言页面,都须要注册码来考证当前操作者的合法性,为了防备网站被机械歹意注册。
生成考证码不过就那末几个步骤,首先是猎取一个随机字符串,然后建立一个布画,将生成的字符串写到布画上,我们还能够在布画上画线画雪花,如今帖一段生成考证码的代码。
相干引荐:《PHP入门教程》
源代码:
<?php session_start(); //开启session //建立随机码,并保存在session中 for($i=0;$i<4;$i++) { $_nmsg.=dechex(mt_rand(0,15)); } //保存到session中 $_SESSION['code']=$_nmsg; //设置图片长和高 $_width=75; $_height=25; //建立一张图象 $_img=imagecreatetruecolor($_width,$_height); //白色背景 $_white=imagecolorallocate($_img,255,255,255); //添补到背景上 imagefill($_img,0,0,$_white); //黑色边框 $_black=imagecolorallocate($_img,0,0,0); imagerectangle($_img,0,0,$_width-1,$_height-1,$_black); //随即画出5个线条 for($i=0;$i<5;$i++) { $_rnd_color=imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color); } //雪花 for($i=0;$i<10;$i++) { $_rnd_color=imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),"*",$_rnd_color); } //输出考证码 for($i=0;$i<strlen($_SESSION['code']);$i++) { imagestring($_img,5,10+$i*15,mt_rand(0,10),$_SESSION['code'][$i],$_blackr); } //输出图象 header('Content-Type:image/png'); imagepng($_img); //烧毁图象 imagedestroy($_img); ?>
代码中将运用以下函数:
mt_rand — 生成更好的随机数
int mt_rand ([ int $min ], int $max )很多老的 libc 的随机数发生器具有一些不确定和未知的特征而且很慢。PHP 的 rand() 函数默许运用 libc 随机数发生器。
mt_rand()函数黑白正式用来替代它的。该函数用了Mersenne Twister中已知的特征作为随机数发生器,它能够发生随机数值的平均速度比 libc 供应的 rand() 快四倍。
dechex — 十进制转换为十六进制返回一字符串,包括有给定 number参数的十六进制示意。所能转换的最大数值为十进制的 4294967295,其效果为 "ffffffff"。
imagecreatetruecolor — 新建一个真彩色图象。
resource imagecreatetruecolor ( int $x_size , int $y_size )
imagecreatetruecolor() 返回一个图象标识符,代表了一幅大小为 x_size 和 y_size 的黑色图象。
imagecolorallocate — 为一幅图象分派色彩。
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 身分构成的色彩。red,green 和 blue 分别是所须要的色彩的红,绿,蓝身分。这些参数是 0 到 255 的整数或许十六进制的 0x00 到 0xFF。imagecolorallocate()必需被挪用以建立每一种用在 image 所代表的图象中的色彩。
imagefill — 地区添补。
bool imagefill ( resource $image , int $x , int $y , int $color )
imagefill() 在 image图象的坐标 x,y(图象左上角为 0, 0)处用 color色彩实行地区添补(即与 x, y 点色彩雷同且相邻的点都会被添补)。
imagerectangle — 画一个矩形。
bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
imagerectangle() 用 col 色彩在 image 图象中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图象的左上角坐标为 0, 0。
imageline — 画一条线段。
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用 color色彩在图象 image 中从坐标 x1,y1 到 x2,y2(图象左上角为 0, 0)画一条线段。
imagestring — 水高山画一行字符串。
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
imagestring() 用 col色彩将字符串 s 画到 image所代表的图象的 x,y坐标处(这是字符串左上角坐标,整幅图象的左上角为 0,0)。假如 font 是 1,2,3,4 或 5,则运用内置字体。
imagepng — 以 PNG 花样将图象输出到浏览器或文件
imagepng() 将 GD 图象流(image)以 PNG 花样输出到规范输出(一般为浏览器),或许假如用 filename 给出了文件名则将其输出到该文件。
imagedestroy — 烧毁一图象
imagedestroy() 开释与 image 关联的内存。
将源代码保存为code.php是个php文件,我们该怎样运用他呢?
imagepng已将这个php文件输出成了png文件
直接挪用就能够了。
<img src="mycode.php"/>
假如要运用考证码,记得开启session哦。
<?php session_start(); echo $_SESSION['code']; ?>
以上就是php设想考证码的历程是什么的细致内容,更多请关注ki4网别的相干文章!