A-A+
php不错的验证码
php的验证码其实网上有许多的,包括成熟的类也非常的多,而下面的这个是个再普通不过的php验证码了,只是为了让新手练习学习的,里面的源码倒不是很复杂,直接将其保存为.php,运行即可,而源码的实用方式也很简单,就是判断 $_SESSION['yanzheng'] 的值和自己提交的是否相同,如果相同那么就可以验证通过,不是很难的原理,就不多介绍了,运行一下试试看吧。
<?php session_start(); function rand_location() //获得4个不行等的相差20左右倍数的 //数,以免验证码重叠 { $i = 0; $location = array(); while ($i < 4) { static $rand=0; $rand +=20; $location[$i] = $rand; $i++; } return $location; } $range = '123456789zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPAS DFGHJKLMNBVCXZ'; for ($i = 0; $i < 4; $i++) { $rand[$i] = $range{rand(0, 42)}; } $see=$rand; $_SESSION['yanzheng']=implode("",$see); $im = imagecreatetruecolor(100, 30); $bg = imagecolorallocate($im, 180,239,90); imagefilledrectangle($im, 0, 0, 150, 50, $bg); /*for ($i = 0; $i < 200; $i++) //画点 { $color = imagecolorallocate($im, 255,255,255); imagesetpixel($im, rand(1, 100), rand(1, 30), $color); } */ for ($i = 0; $i < 4; $i++) //画曲线 { $color = imagecolorallocate($im, rand(1, 255), rand(1, 25 5), rand(1, 255)); imagearc($im, rand(5,90), rand(1, 10), rand(80, 100), ra nd(10, 30), rand(0,90), rand(0, 360), $color); } $location_x = rand_location(); //获得随机数 for ($i = 0; $i < 4; $i++) { $color = imagecolorallocate($im, rand(178, 233),46, rand(4 6, 233)); imagettftext($im, 20, rand(0, 60), $location_x[$i], 27, $c olor, "C:\WINDOWS\Fonts\simhei.ttf",$rand[$i]); } header("Content-Type:image/jpeg"); imagejpeg($im); ?>
我是奇怪为什么不能复制才回复试试的。。。