Home> php教程> php手册> body text

PHP验证码在验证页面中的应用分析

WBOY
Release: 2016-06-13 11:06:54
Original
1437 people have browsed it

对于一个网站来说,肯定会需要一个提供验证功能的页面。那么我们现在就需要考虑验证页面中的验证码的应用了。我们接下来将要为大家具体讲解有关一、准备一个展示并提交PHP验证码的页面

  1. php
  2. @header("content-type:text/html; charset=UTF-8");
  3. //打开session
  4. session_start();
  5. ?>
  6. html>
  7. head>
  8. meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  9. title>PHP验证码示例title>
  10. head>
  11. body>
  12. 验证码:br/>
  13. iframe id="iimg" height="100" width=300 src="img.php" frameborder="0" >iframe>
  14. br/>
  15. input type=button value="看不清,换一张" onclick="iimg.location.reload();">
  16. br>
  17. form action="validate.php" method="post">
  18. 输入验证码:input name="imgId" style="width:60">
  19. input type="submit" value="确定">
  20. form>
  21. body>
  22. html>
Copy after login

二、以下是PHP验证码生成页面,该页面在第一页面中被PHP验证码在验证页面中的应用分析调用

  1. php
  2. Header("Content-type: image/gif");
  3. session_start();
  4. //验证码为随机字符,以下是算法
  5. $randval;
  6. for($i=0;$i7;$i++){
  7. $randstr = mt_rand(ord('A'),ord('Z'));
  8. srand((double)microtime()*1000000);
  9. $randv = mt_rand(0,10);
  10. if($randv%2==0){
  11. $randval.=mt_rand(0,10);
  12. }else{
  13. $randval.=chr($randstr);
  14. }
  15. }
  16. //注册PHP验证码到session
  17. session_register($randval);
  18. //以下是绘制验证码图
  19. $height = 50;//图高
  20. $width = 100;//图宽
  21. $im = ImageCreateTrueColor($width, $height);
  22. $white = ImageColorAllocate($im, 255, 255, 255);
  23. $blue = ImageColorAllocate($im, 0, 0, 64);
  24. ImageFill($im, 0, 0, $white);
  25. srand((double)microtime()*1000000000);
  26. ImageLine($im, mt_rand(0,$width/3), mt_rand(0,$height/3), mt_rand($width/3,$width), mt_rand($height/3,$height), $blue);
  27. srand((double)microtime()*1000000);
  28. ImageLine($im, mt_rand($width/3,$width), mt_rand(0,$height/3), mt_rand(0,$width/3), mt_rand(0,$height/3), $blue);
  29. srand((double)microtime()*1000000);
  30. ImageString($im,16 , mt_rand(0,$width - strlen($randval) * 10), mt_rand(0,$height-12), $randval, $blue);
  31. ImageGIF($im);
  32. ImageDestroy($im);
  33. /*
  34. 需要注意的是:为了支持以上绘图函数,我们必须在PHP载入GD2图形处理库,可修改 php.ini 文件中的
  35. ;extension=php_gd2.dll
  36. extension=php_gd2.dll
  37. 即开启GD2库
  38. */
  39. ?>
Copy after login

三、这是验证页面,提示PHP验证码是否通过验证

  1. php @header("content-type:text/html; charset=UTF-8");
  2. //开启session
  3. session_start();
  4. //得到用户输入的验证码,并转换成大写
  5. $imgId_req = $_REQUEST['imgId'];
  6. $imgId_req = strtoupper($imgId_req);
  7. //验证该字符串是否注册了session变量
  8. if (session_is_registered($imgId_req)) {
  9. echo "font color=blue >通过验证!font>";
  10. } else {
  11. echo "font color=red >验证错误!font>";
  12. }
  13. //关闭session,以清除所有注册过的变量
  14. session_destroy();
  15. ?>
Copy after login

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 Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!