RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
php生成验证码函数

php生成验证码的函数,实用靠谱。先上下生成的验证码的效果图(这里生成的是全数字的验证码的示例效果):

10年积累的成都网站建设、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有信宜免费网站建设让你可以放心的选择与我们合作。

php生成验证码函数

下面是php生成验证码的源码:

  1. session_start();

  2. //session_register('CheckCode');

  3. //PHP4.2以上版本不需要用session_register()注册SESSION变量

  4. $type='gif';

  5. $width= 45;

  6. $height= 20;

  7. header("Content-type: p_w_picpath/".$type);

  8. srand((double)microtime()*1000000);

  9. if(isset($_GET['action'])){

  10. $randval=randStr(4,$_GET['action']);    

  11. }else{

  12. $randval=randStr(4,'');  

  13. }

  14. if($type!='gif'&&function_exists('p_w_picpathcreatetruecolor')){

  15. $im=@p_w_picpathcreatetruecolor($width,$height);

  16. }else{

  17. $im=@p_w_picpathcreate($width,$height);

  18. }

  19. $r=Array(225,211,255,223);

  20. $g=Array(225,236,237,215);

  21. $b=Array(225,236,166,125);

  22. $key=rand(0,3);

  23. $backColor=ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机)

  24. $borderColor=ImageColorAllocate($im,127,157,185);//边框色

  25. $pointColor=ImageColorAllocate($im,255,170,255);//点颜色

  26. @p_w_picpathfilledrectangle($im,0,0,$width - 1,$height - 1,$backColor);//背景位置

  27. @p_w_picpathrectangle($im,0,0,$width-1,$height-1,$borderColor);//边框位置

  28. $stringColor=ImageColorAllocate($im,255,51,153);

  29. for($i=0;$i<=100;$i++){

  30. $pointX=rand(2,$width-2);

  31. $pointY=rand(2,$height-2);

  32.    @p_w_picpathsetpixel($im,$pointX,$pointY,$pointColor);

  33. }

  34. @p_w_picpathstring($im,5,5,1,$randval,$stringColor);

  35. $ImageFun='Image'.$type;

  36. $ImageFun($im);

  37. @p_w_picpathdestroy($im);

  38. $_SESSION['CheckCode']=$randval;

  39. function randStr($len=6,$format='ALL'){

  40. switch($format){

  41. case'ALL'://生成包含数字和字母的验证码

  42. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';break;

  43. case'CHAR'://仅生成包含字母的验证码

  44. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';break;

  45. case'NUMBER'://仅生成包含数字的验证码

  46. $chars='0123456789';break;

  47. default :

  48. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';break;

  49.    }

  50. $string='';

  51. while(strlen($string)<$len)

  52. $string.=substr($chars,(mt_rand()%strlen($chars)),1);

  53. return$string;

  54. }

该函数的具体使用方法请看如下这个示例(这里是生成全数字的验证码): 更多PHP教程,请访问代码家园;


本文原地址:http://www.daimajiayuan.com/sitejs-16919-1.htm


网站题目:php生成验证码函数
链接地址:http://scyingshan.cn/article/pojpgh.html