国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP對驗證碼的認證過程防止機器注冊

2024-05-04 21:49:56
字體:
來源:轉載
供稿:網友

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. //Vevb.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. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丘北县| 金阳县| 裕民县| 个旧市| 西藏| 太仆寺旗| 新野县| 炎陵县| 南丰县| 云霄县| 丹东市| 潞西市| 菏泽市| 成安县| 兴文县| 苏州市| 刚察县| 苍溪县| 汤原县| 庆阳市| 普宁市| 承德县| 鹤壁市| 仲巴县| 安国市| 乌拉特后旗| 怀远县| 沅陵县| 盐边县| 武威市| 特克斯县| 泰州市| 满洲里市| 玉山县| 二手房| 碌曲县| 涟水县| 曲松县| 正定县| 泸定县| 苏尼特左旗|