Syntax Desktop PHP Cross Reference Web Portal Systems

Source: /public/CaptchaSecurityImages.php - 114 lines - 3873 bytes - Summary - Text - Print

   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  * Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
  10  * 
  11  * This program is free software; you can redistribute it and/or 
  12  * modify it under the terms of the GNU General Public License 
  13  * as published by the Free Software Foundation; either version 2 
  14  * of the License, or (at your option) any later version.
  15  * 
  16  * This program is distributed in the hope that it will be useful, 
  17  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  19  * GNU General Public License for more details: 
  20  * http://www.gnu.org/licenses/gpl.html
  21  *
  22  
  23  ////////////////////////////////////////////////////////////////////////////////
  24  /////////////////////////               EXAMPLE            /////////////////////
  25  ////////////////////////////////////////////////////////////////////////////////
  26  
  27  
  28  <?php 
  29  session_start();
  30  
  31  if( isset($_POST['submit'])) {
  32     if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
  33        // Insert you code for processing the form here
  34        echo 'Thank you. Your message said "'.$_POST['message'].'"';
  35     } else {
  36        // Insert your code for showing an error message here
  37        echo 'Sorry, you have provided an invalid security code';
  38     }
  39  } else {
  40  ? >
  41  
  42      <form action="form.php" method="post">
  43          Message: <input type="text" name="message" /><br />
  44          <img src="/admin/public/CaptchaSecurityImages.php?width=100&height=40&character=5" /><br />
  45          Security Code: <input id="security_code" name="security_code" type="text" /><br />
  46          <input type="submit" name="submit" value="Submit" />
  47      </form>
  48  
  49  
  50  < ?php
  51      }
  52  ? >
  53  
  54  */
  55  
  56  class CaptchaSecurityImages {
  57  
  58    //relative path to the font
  59    var $path = "/public/";
  60  
  61    //font name
  62      var $font = "monofont.ttf";
  63  
  64  	function generateCode($characters) {
  65          /* list all possible characters, similar looking characters and vowels have been removed */
  66          $possible = '23456789bcdfghjkmnpqrstvwxyz'; 
  67          $code = '';
  68          $i = 0;
  69          while ($i < $characters) { 
  70              $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
  71              $i++;
  72          }
  73          return $code;
  74      }
  75  
  76  	function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
  77        $this->font = $_SERVER["DOCUMENT_ROOT"].$this->path.$this->font;
  78          $code = $this->generateCode($characters);
  79          /* font size will be 75% of the image height */
  80          $font_size = $height * 0.75;
  81          $image = @imagecreate($width, $height) or die('Cannot Initialize new GD image stream');
  82          /* set the colours */
  83          $background_color = imagecolorallocate($image, 255, 255, 255);
  84          $text_color = imagecolorallocate($image, 20, 40, 100);
  85          $noise_color = imagecolorallocate($image, 100, 120, 180);
  86          /* generate random dots in background */
  87          for( $i=0; $i<($width*$height)/3; $i++ ) {
  88              imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  89          }
  90          /* generate random lines in background */
  91          for( $i=0; $i<($width*$height)/150; $i++ ) {
  92              imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  93          }
  94          /* create textbox and add text */
  95          $textbox = imagettfbbox($font_size, 0, $this->font, $code);
  96          $x = ($width - $textbox[4])/2;
  97          $y = ($height - $textbox[5])/2;
  98          imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code);
  99          /* output captcha image to browser */
 100          imagejpeg($image);
 101          imagedestroy($image);
 102          $_SESSION['security_code'] = $code;
 103      }
 104  
 105  }
 106  
 107  $width = isset($_GET['width']) ? $_GET['width'] : '120';
 108  $height = isset($_GET['height']) ? $_GET['height'] : '40';
 109  $characters = isset($_GET['characters']) ? $_GET['characters'] : '6';
 110  
 111  header('Content-Type: image/jpeg');
 112  $captcha = new CaptchaSecurityImages($width,$height,$characters);
 113  
 114  ?>

title

Description

title

Description

title

Description

title

title

Body