当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP对验证码的认证过程防止机器注册

发布:smiling 来源: PHP粉丝网  添加日期:2016-01-20 16:32:52 浏览: 评论:0 

PHP对验证码的认证过程文章重点为告诉各位如何防止机器注册这个问题了,下面我们一起来看看例子,希望对各位有帮助.

这段时间在写php脚本,接触到web前端以及web安全问题比较多,这时给大家简单地谈一下我们网站验证码的验证过程及其安全问题。

从三个方面去谈一下关于验证码的使用:验证码的生成,验证的过程,验证中注意的安全问题。

验证码的生成,首先还是要说说验证码的作用。众所周知,验证码的存在,是为了防止一些机器,或是刷恶意留言、无限注册用户或是暴力破解账号密码。现在普通的验证码是由一个php脚本生成的,比如打开我们emlog的include/lib/文件夹,底下有个checkcode.php,这就是生成验证码的脚本。

我们可以简单看一下它的代码:

  1. session_start(); 
  2. $randCode = ''
  3. $chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPRSTUVWXYZ23456789'
  4. for ( $i = 0; $i < 5; $i++ ){ 
  5.  $randCode .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 
  6. $_SESSION['code'] = strtoupper($randCode); 
  7. $img = imagecreate(70,22); 
  8. $bgColor = isset($_GET['mode']) && $_GET['mode'] == 't' ? imagecolorallocate($img,245,245,245) : imagecolorallocate($img,255,255,255); 
  9. $pixColor = imagecolorallocate($img,mt_rand(30, 180), mt_rand(10, 100), mt_rand(40, 250)); 
  10. for($i = 0; $i < 5; $i++){ 
  11.  $x = $i * 13 + mt_rand(0, 4) - 2; 
  12.  $y = mt_rand(0, 3); 
  13.  $text_color = imagecolorallocate($img, mt_rand(30, 180), mt_rand(10, 100), mt_rand(40, 250)); 
  14.  imagechar($img, 5, $x + 5, $y + 3, $randCode[$i], $text_color); 
  15. //phpfensi.com 
  16. for($j = 0; $j < 60; $j++){ 
  17.  $x = mt_rand(0,70); 
  18.  $y = mt_rand(0,22); 
  19.  imagesetpixel($img,$x,$y,$pixColor); 
  20. header('Content-Type: image/png'); 
  21. imagepng($img); 
  22. imagedestroy($img); 

第一个for循环在$chars这个字符串中随机取了5个字符,这实际上就是我们的真实验证码。然后我们可以看到:$_SESSION['code'] = strtoupper($randCode); 他把我们的验证码转换成大写赋值到session里了。

有的朋友要问,问什么赋值到SESSION里了不赋值到COOKIE里。这就说明你对他们二者关系不了解。cookie和session都作为网站临时保存客户端相关信息的一个“容器”,但是cookie是保存在客户端里的,也就是网站的访问者可以随意查看和修改cookie里的内容,那就没有验证码存在的意义了,因为用户可以直接从cookie中读到验证码,当然机器也可以。而session是保存在服务器上的内容,我生成好的验证码,用户不可能读取到。

再看源码,后面的两个循环分别是生成彩色的带验证码的图片和在图片上加噪点。是为了加大机器识别验证码的难度。最后我们看到,header('Content-Type: image/png'); 把我们这个页面定义成为图片的格式。

这样,我们就可以用html代码来让验证码显示出来:

<img src="checkcode.php" />

类似这样:

那么验证的过程就是,我们首先生成5个随机字符,保存到session里。然后把这5个字符画成一个图片给用户看,让用户识别,填写在表单里提交后和我们session里的验证码比对。

其实就是这么简单。

最后来说说验证码的安全性。我们emlog和wordpress其实验证码并不是很强大,我们这个简单的验证码可以写一个小脚本很容易地识别,所以并不适合比较大型的网站使用。像类似腾讯、百度这种网站的验证码很多字符能旋转、扭曲,并且背影上的干扰物更多,甚至是中文验证码。不过对于小型网站来说,普通等级的验证码足矣防范很多刷评论的机器。

还有一点很重要,注意验证码使用过后要记住删除相应的session。否则验证码就失去了其意义,这也是我之前犯过的错误。

为什么这么说。作为一个正常用户,我们每访问一次需要填写验证码的页面,生成验证码的脚本都会执行一次,也就说会生成一个新验证码赋值到session里,没有任何问题。但对于一个机器(或一个暴力破解密码脚本),它第一次访问需要填写验证码的页面,然后在session中得到一个验证码,它以后就不用再次访问这个页面了。直接把第一次的数据包更改并再次发送即可,于是,如果没有清除session的网站,每次的验证码都和第一次相同,也就丧失了验证码的本来作用。

这是我们做网站需要注意的地方。希望大家在写程序的时候多注意网站的安全性,避免在网站发布后出现问题

通过网上的一些实例,拼凑出一个验证码登陆测试程序,详细代码如下:

生成验证码程序ttt.php,通过random生成随机数,然后保存在session里:

login.htm页面

  1. <html> 
  2. <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> 
  3. <title>login</title> 
  4. <style type="text/css"> 
  5. <!-- 
  6. .textbox { 
  7. height: 18px; 
  8. width: 100px; 
  9. .text { 
  10. font-size: 14px; 
  11. vertical-align: bottom; 
  12. color: #0000FF; 
  13. .style1 { 
  14. font-size: 18px; 
  15. color: #0000FF; 
  16. font-family: "幼圆"; 
  17. --> 
  18. </style> 
  19. </head> 
  20. <body> 
  21. <table width="200"> 
  22. <tr><td align="center" valign="bottom" class="style1" bgcolor="#C7D3F9">请输入验证码</td> 
  23. </tr> 
  24. </table> 
  25. <form method="post" action="login.php"> 
  26. <table width="200" border="0" bgcolor="C7D3F9"> 
  27.   <tr> 
  28.     <td class="text">验证码:</td> 
  29.     <td align="right" valign="bottom"><input type="text" name="auth" class="textbox"></td> 
  30.   </tr> 
  31. </table> 
  32. <img src="ttt.php?act=yes" align="middle"> 
  33. <table width="200"><tr><td align="right"><input type="button" value="看不清楚验证码" onclick="window.location.reload();"><input name="submit" type="submit" value="Submit"></td></tr></table> 
  34. </form> 
  35. </body> 
  36. </html> 

login.php

  1. <?php 
  2. session_start(); 
  3. $name = $_POST['user']; 
  4. $password = $_POST['passwd']; 
  5. $auth = $_POST['auth']; 
  6. #require("db.php"); 
  7. #$db = new db(); 
  8. #$sql = "select * from user where name = '$name' and password = '$password'"
  9. #$result = $db->query($sql); 
  10. if($_SESSION['seccode'] == $auth
  11.   #$_SESSION['user'] = $name
  12.   #$_SESSION['passwd'] = $password
  13.  # header("Location: main.php"); 
  14. #echo ("登录成功!"); 
  15. $_SESSION['seccode']=''
  16.   print ' 
  17.   <script language=javascript> 
  18.    alert("登录成功!"); 
  19.   </script>'; 
  20. }else 
  21.   print ' 
  22.   <script language=javascript> 
  23.    alert("登录失败,请重新登录!"); 
  24.    self.window.location="login.html"
  25.   </script>'; 
  26. ?> 

ttt.php验证码生成程序

  1. <?php 
  2. session_start(); 
  3. function random($length$numeric = 0) { 
  4.  mt_srand((double)microtime() * 1000000); 
  5.  if($numeric) { 
  6.   $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1)); 
  7.  } else { 
  8.   $hash = ''
  9.   $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'
  10.   $max = strlen($chars) - 1; 
  11.   for($i = 0; $i < $length$i++) { 
  12.    $hash .= $chars[mt_rand(0, $max)]; 
  13.   } 
  14.  } 
  15.  return $hash
  16. #if(preg_replace("/https?:\/\/([^\:\/]+).*/i""\\1"$_SERVER['HTTP_REFERER']) != preg_replace("/([^\:]+).*/""\\1"$_SERVER['HTTP_HOST'])) { 
  17. exit('Access Denied'); 
  18. #} 
  19.  
  20. //if($_GET['update']) { 
  21.  $seccode = random(4, 1); 
  22. //} 
  23. #if($seccode < 1 || $seccode > 9999) { 
  24. exit('Access Denied'); 
  25. #} 
  26. $_SESSION['seccode'] = $seccode
  27. $seccode = sprintf('%04d'$seccode); 
  28. if(!$nocacheheaders) { 
  29.  @header("Expires: -1"); 
  30.  @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE); 
  31.  @header("Pragma: no-cache"); 
  32. if(function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') && function_exists('imagecolorallocate') && function_exists('imagesetpixel') && function_exists('imagechar') && function_exists('imagecreatefromgif') && function_exists('imagepng')) { 
  33.  $im = imagecreate(62, 25); 
  34.  $backgroundcolor = imagecolorallocate ($im, 255, 255, 255); 
  35.  $numorder = array(1, 2, 3, 4); 
  36.  shuffle($numorder); 
  37.  $numorder = array_flip($numorder); 
  38.  for($i = 1; $i <= 4; $i++) { 
  39.   $imcodefile = 'seccode/'.$seccode[$numorder[$i]].'.gif'
  40.   $x = $numorder[$i] * 13 + mt_rand(0, 4) - 2; 
  41.   $y = mt_rand(0, 3); 
  42.   if(file_exists($imcodefile)) { 
  43.    $imcode = imagecreatefromgif($imcodefile); 
  44.    $data = getimagesize($imcodefile); 
  45.    imagecolorset($imcode, 0 ,mt_rand(50, 255), mt_rand(50, 128), mt_rand(50, 255)); 
  46.    imagecopyresized($im$imcode$x$y, 0, 0, $data[0] + mt_rand(0, 6) - 3, $data[1] + mt_rand(0, 6) - 3, $data[0], $data[1]); 
  47.   } else { 
  48.    $text_color = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 128), mt_rand(50, 255)); 
  49.    imagechar($im, 5, $x + 5, $y + 3, $seccode[$numorder[$i]], $text_color); 
  50.   } 
  51.  } 
  52.  $linenums = mt_rand(10, 32); 
  53.  for($i=0; $i <= $linenums$i++) { 
  54.   $linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); 
  55.   $linex = mt_rand(0, 62); 
  56.   $liney = mt_rand(0, 25); 
  57.   imageline($im$linex$liney$linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor); 
  58.  } 
  59.  for($i=0; $i <= 64; $i++) { 
  60.   $pointcolor = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 255), mt_rand(50, 255)); 
  61.   imagesetpixel($im, mt_rand(0, 62), mt_rand(0, 25), $pointcolor); 
  62.  } 
  63.  $bordercolor = imagecolorallocate($im , 150, 150, 150); 
  64.  imagerectangle($im, 0, 0, 61, 24, $bordercolor); 
  65.  header('Content-type: image/png'); 
  66.  imagepng($im); 
  67.  imagedestroy($im); 
  68. else { 
  69.  $numbers = array 
  70.   ( 
  71.   0 => array('3c','66','66','66','66','66','66','66','66','3c'), 
  72.   1 => array('1c','0c','0c','0c','0c','0c','0c','0c','1c','0c'), 
  73.   2 => array('7e','60','60','30','18','0c','06','06','66','3c'), 
  74.   3 => array('3c','66','06','06','06','1c','06','06','66','3c'), 
  75.   4 => array('1e','0c','7e','4c','2c','2c','1c','1c','0c','0c'), 
  76.   5 => array('3c','66','06','06','06','7c','60','60','60','7e'), 
  77.   6 => array('3c','66','66','66','66','7c','60','60','30','1c'), 
  78.   7 => array('30','30','18','18','0c','0c','06','06','66','7e'), 
  79.   8 => array('3c','66','66','66','66','3c','66','66','66','3c'), 
  80.   9 => array('38','0c','06','06','3e','66','66','66','66','3c'
  81.   ); 
  82.  for($i = 0; $i < 10; $i++) { 
  83.   for($j = 0; $j < 6; $j++) { 
  84.    $a1 = substr('012', mt_rand(0, 2), 1).substr('012345', mt_rand(0, 5), 1); 
  85.    $a2 = substr('012345', mt_rand(0, 5), 1).substr('0123', mt_rand(0, 3), 1); 
  86.    mt_rand(0, 1) == 1 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a1); 
  87.    mt_rand(0, 1) == 0 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a2); 
  88.   } 
  89.  } 
  90.  $bitmap = array(); 
  91.  for($i = 0; $i < 20; $i++) { 
  92.   for($j = 0; $j < 4; $j++) { 
  93.    $n = substr($seccode$j, 1); 
  94.    $bytes = $numbers[$n][$i]; 
  95.    $a = mt_rand(0, 14); 
  96.    switch($a) { 
  97.     case 1: str_replace('9''8'$bytes); break
  98.     case 3: str_replace('c''e'$bytes); break
  99.     case 6: str_replace('3''b'$bytes); break
  100.     case 8: str_replace('8''9'$bytes); break
  101.     case 0: str_replace('e''f'$bytes); break
  102.    } 
  103.    array_push($bitmap$bytes); 
  104.   } 
  105.  } 
  106.  for($i = 0; $i < 8; $i++) { 
  107.   $a = substr('012', mt_rand(0, 2), 1) . substr('012345', mt_rand(0, 5), 1); 
  108.   array_unshift($bitmap$a); 
  109.   array_push($bitmap$a); 
  110.  } 
  111.  $image = pack('H*''424d9e000000000000003e000000280000002000000018000000010001000000'
  112.    '0000600000000000000000000000000000000000000000000000FFFFFF00'.implode(''$bitmap)); 
  113.  header('Content-Type: image/bmp'); 
  114.  echo $image
  115. ?> 

Tags: PHP验证码 PHP机器注册

分享到: