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

首頁 > 語言 > PHP > 正文

php生成驗證碼圖片程序

2024-09-04 11:44:28
字體:
供稿:網(wǎng)友

一款國外網(wǎng)站下載的php生成驗證碼圖片代碼,這個比較實(shí)用我們還舉例說明了,有需要的朋友按上面保存成php就可以用了.

php生成驗證碼圖片程序代碼如下:

  1. <?php  
  2. session_start(); 
  3.  
  4. if( isset($_POST['submit'])) { 
  5.    if$_SESSION['security_code'] == $_POST['security_code'] && !emptyempty($_SESSION['security_code'] ) ) { 
  6.   // Insert you code for processing the form here, e.g emailing the submission, entering it into a database.  
  7.   echo 'Thank you. Your message said "'.$_POST['message'].'"'
  8.   unset($_SESSION['security_code']); 
  9.    } else { 
  10.   // Insert your code for showing an error message here 
  11.   echo 'Sorry, you have provided an invalid security code'
  12.    } 
  13. else { 
  14. ?> 
  15.  
  16.  <form action="form.php" method="post"
  17.   <label for="name">Name: </label><input type="text" name="name" id="name" /><br /> 
  18.   <label for="email">Email: </label><input type="text" name="email" id="email" /><br /> 
  19.   <label for="message">Message: </label><textarea rows="5" cols="30" name="message" id="message"></textarea><br /> 
  20.   <img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /><br />//開源代碼Vevb.com 
  21.   <label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" /><br /> 
  22.   <input type="submit" name="submit" value="Submit" /> 
  23.  </form> 
  24.  
  25. <?php 
  26.  } 
  27. ?> 

驗證程序 CaptchaSecurityImages.php,代碼如下:

  1. <?php 
  2. session_start(); 
  3.  
  4. /* 
  5. * File: CaptchaSecurityImages.php 
  6. * Author: Simon Jarvis 
  7. * Copyright: 2006 Simon Jarvis 
  8. * Date: 03/08/06 
  9. * Updated: 07/02/07 
  10. * Requirements: PHP 4/5 with GD and FreeType libraries 
  11. * Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php 
  12.  
  13. * This program is free software; you can redistribute it and/or  
  14. * modify it under the terms of the GNU General Public License  
  15. * as published by the Free Software Foundation; either version 2  
  16. * of the License, or (at your option) any later version. 
  17.  
  18. * This program is distributed in the hope that it will be useful,  
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of  
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  
  21. * GNU General Public License for more details:  
  22. * http://www.gnu.org/licenses/gpl.html 
  23. * 
  24. */ 
  25.  
  26. class CaptchaSecurityImages { 
  27.  
  28.  var $font = 'monofont.ttf'
  29.  
  30.  function generateCode($characters) { 
  31.   /* list all possible characters, similar looking characters and vowels have been removed */ 
  32.   $possible = '23456789bcdfghjkmnpqrstvwxyz'
  33.   $code = ''
  34.   $i = 0; 
  35.   while ($i < $characters) {  
  36.    $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1); 
  37.    $i++; 
  38.   } 
  39.   return $code
  40.  } 
  41.  
  42.  function CaptchaSecurityImages($width='120',$height='40',$characters='6') { 
  43.   $code = $this->generateCode($characters); 
  44.   /* font size will be 75% of the image height */ 
  45.   $font_size = $height * 0.75; 
  46.   $image = @imagecreate($width$heightor die('Cannot initialize new GD image stream'); 
  47.   /* set the colours */ 
  48.   $background_color = imagecolorallocate($image, 255, 255, 255); 
  49.   $text_color = imagecolorallocate($image, 20, 40, 100); 
  50.   $noise_color = imagecolorallocate($image, 100, 120, 180); 
  51.   /* generate random dots in background */ 
  52.   for$i=0; $i<($width*$height)/3; $i++ ) { 
  53.    imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color); 
  54.   } 
  55.   /* generate random lines in background */ 
  56.   for$i=0; $i<($width*$height)/150; $i++ ) { 
  57.    imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); 
  58.   } 
  59.   /* create textbox and add text */ 
  60.   $textbox = imagettfbbox($font_size, 0, $this->font, $codeor die('Error in imagettfbbox function'); 
  61.   $x = ($width - $textbox[4])/2; 
  62.   $y = ($height - $textbox[5])/2; 
  63.   imagettftext($image$font_size, 0, $x$y$text_color$this->font , $codeor die('Error in imagettftext function'); 
  64.   /* output captcha image to browser */ 
  65.   header('Content-Type: image/jpeg'); 
  66.   imagejpeg($image); 
  67.   imagedestroy($image); 
  68.   $_SESSION['security_code'] = $code
  69.  } 
  70.  
  71.  
  72. $width = isset($_GET['width']) ? $_GET['width'] : '120'
  73. $height = isset($_GET['height']) ? $_GET['height'] : '40'
  74. $characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6'
  75. //開源代碼Vevb.com 
  76. $captcha = new CaptchaSecurityImages($width,$height,$characters); 
  77.  
  78. ?> 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 益阳市| 巴中市| 赤水市| 依兰县| 信阳市| 开鲁县| 平湖市| 吴川市| 吴旗县| 抚宁县| 长丰县| 搜索| 遵化市| 穆棱市| 万宁市| 龙南县| 甘泉县| 正镶白旗| 离岛区| 宣城市| 松滋市| 台北县| 沈阳市| 潮安县| 巍山| 兴海县| 松桃| 滁州市| 沁源县| 临汾市| 盱眙县| 留坝县| 南陵县| 井冈山市| 太保市| 临安市| 安仁县| 淮阳县| 嘉荫县| 邓州市| 诏安县|