Home > Backend Development > PHP Tutorial > php绘图当用之验证码

php绘图当用之验证码

WBOY
Release: 2016-06-13 12:16:32
Original
885 people have browsed it

php绘图应用之验证码

在绘图里面,就像之前所说的,php的绘图是真正动态绘图,虽然自己承认会出的图是很不好看的,但我们主要关注的应该还是数据处理。

验证码我们几乎无处不见,它的产生和绘图技术是密不可分的,其实,简单的验证码绘制出来是很简单的,就像下面的一段代码就可以绘制出包含字母和数字的元素:

<?php $checkCode="";	for($i=0; $i<=3;$i++){		$checkCode.=dechex(rand(1,15));	}	session_start();	$_SESSION[&#39;checkCode&#39;]=$checkCode;	//建立画布	$img_1=imagecreatetruecolor(110, 30);	$red=imagecolorallocate($img_1, 255, 0, 0);	imagestring($img_1, 5, 0, 0, $checkCode, $red);	header("Content-type: image/png");	imagepng($img_1);	imagedestroy($img_1);?>
Copy after login

上面的代码只是简单的说了一下验证码的绘制,其实代码方面并没有进行安全处理。

下面是相对的把验证码处理的相对复杂了一些:

<?php $checkCode="";	for($i=0; $i<=3;$i++){		$checkCode.=dechex(rand(1,15));	}	session_start();	$_SESSION[&#39;checkCode&#39;]=$checkCode;	//建立画布	$img_1=imagecreatetruecolor(70, 40);	//必须是先创建画布,在创建颜色。	$red=imagecolorallocate($img_1, rand(0,255), rand(0,255), rand(0,255));	for($k=0; $k<=20; $k++){		imageline($img_1, rand(0,50),rand(0,30),0,0,0,0, imagecolorallocate($img_1, rand(0,255), rand(0,255), rand(0,255)));	}	imagestring($img_1, rand(3,7), rand(0,40), rand(0,20), $checkCode, $red);	header("Content-type: image/png");	imagepng($img_1);	imagedestroy($img_1);?>
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template