PHP数字验证码的例子

在php中实现验证码,关键点在于掌握php gd库与session的用法。

纵观网上php 生成验证码的例子,无不是php gd库与session相结合,并利用php 生成随机数的方法来完成。

PHP验证码,可以分为很多种,包括 php 图片验证码php 随机验证码,以及php 中文验证码等,根据不同的应用场合来使用不同的验证码。

这里分享一个php数字验证码,供大家参考。


1,数字验证码
[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <?  
  2. /* 
  3. *Filename:authpage.php 
  4. */  
  5. session_start();  
  6. //srand((double)microtime()*1000000);  
  7. $authnum=$_SESSION['authnum'];  
  8. //验证用户输入是否和验证码一致  
  9. if(isset($_POST['authinput']))  
  10. {  
  11. if(strcmp($_POST['authinput'],$_SESSION['authnum'])==0)  
  12. echo"验证成功!";  
  13. else  
  14. echo"验证失败!";  
  15. }  
  16.   
  17.   
  18. //生成新的四位整数验证码  
  19.   
  20.   
  21. //while(($authnum=rand()%10000)<1000);  
  22. ?>  
  23. <formaction=test4.phpmethod=post>  
  24. <table>  
  25. 请输入验证码:<inputtype=textname=authinputstyle="width:80px"><br>  
  26. <inputtype=submitname="验证"value="提交验证码">  
  27. <inputtype=hiddenname=authnumvalue=<?echo$authnum;?>>  
  28. <imgsrc=authimg.php?authnum=<?echo$authnum;?>>  
  29. </table>  
  30. </form>  

2,authimg.php
[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <?php  
  2. //生成验证码图片  
  3. Header("Content-type:image/PNG");  
  4. srand((double)microtime()*1000000);//播下一个生成随机数字的种子,以方便下面随机数生成的使用  
  5.   
  6. session_start();//将随机数存入session中  
  7. $_SESSION['authnum']="";  
  8. $im=imagecreate(62,20);//制定图片背景大小  
  9.   
  10. $black=ImageColorAllocate($im,0,0,0);//设定三种颜色  
  11. $white=ImageColorAllocate($im,255,255,255);  
  12. $gray=ImageColorAllocate($im,200,200,200);  
  13.   
  14. imagefill($im,0,0,$gray);//采用区域填充法,设定(0,0)  
  15.   
  16. while(($authnum=rand()%100000)<10000);  
  17. //将四位整数验证码绘入图片  
  18. // www.jbxue.com  
  19. $_SESSION['authnum']=$authnum;  
  20. imagestring($im,5,10,3,$authnum,$black);  
  21. //用col颜色将字符串s画到image所代表的图像的x,y座标处(图像的左上角为0,0)。  
  22. //如果font是1,2,3,4或5,则使用内置字体  
  23.   
  24. for($i=0;$i<200;$i++)//加入干扰象素  
  25. {  
  26. $randcolor=ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));  
  27. imagesetpixel($im,rand()%70,rand()%30,$randcolor);  
  28. }  
  29. ImagePNG($im);  
  30. ImageDestroy($im);  
  31. ?>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值