为大家介绍一个php使用GD库生成验证码的例子,有需要的朋友,可以参考下。
通过GD生成PNG图片,并把$randval随机数字赋给$_SESSION['login_check_num'],在通过用户输入的$_POST进行比较,来判断是否正确。
注意:如果服务器未开启GD库支持,需要修改php.ini文件,使php支持GD库。
代码如下:
04 | if ( $_GET [ "action" ]== "verifycode" ) |
09 | function rand_create() |
12 | Header( "Content-type: image/PNG" ); |
14 | srand((double)microtime()*1000000); |
16 | $im = imagecreate(62,20); |
17 | $black = ImageColorAllocate( $im , 0,0,0); |
18 | $white = ImageColorAllocate( $im , 255,255,255); |
19 | $gray = ImageColorAllocate( $im , 200,200,200); |
21 | imagefill( $im ,0,0, $gray ); |
22 | while (( $randval =rand()%100000)<10000);{ |
23 | $_SESSION [ "login_check_num" ] = $randval ; |
25 | imagestring( $im , 5, 10, 3, $randval , $black ); |
28 | for ( $i =0; $i <200; $i ++){ |
29 | $randcolor = ImageColorallocate( $im ,rand(0,255),rand(0,255),rand(0,255)); |
30 | imagesetpixel( $im , rand()%70 , rand()%30 , $randcolor ); |
40 | if ( $_POST [ "reg_rand" ] == $_SESSION [ "login_check_num" ]){ |
您可能感兴趣的文章:
php图片验证码的例子
php彩色验证码的简单例子
php GD库生成验证码的实例
php生成验证码的例子
php 验证码类 php 验证码
php随机验证码 php生成随机验证码(图文)
一个比较稳定的php登陆系统验证码
用php生成带有雪花背景的验证码
php写的一个验证码
php生成动态图片验证码的一段代码