Jump to content

captcha


jalaladdin

Recommended Posts

i use captcha for contact form ie show me this errorWarning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\my\index.php:1) in C:\wamp\www\my\index.php on line 3i wrote this code before html codes

<?php ob_start() ; session_start(); require('php-captcha.inc.php'); if (empty($_POST['user_code'])) { $valid="PLEASE ENTER CODE"; // echo 'Valid code entered'; } else if (PhpCaptcha::Validate($_POST['user_code'])) { $valid="VALID CODE"; // echo 'Valid code entered'; } else { $valid="INVALID CODE"; // echo 'Invalid code entered';}?>
Link to comment
Share on other sites

I WROTE THIS CODE FOE COMMENT FORM WHEN I FILL OUT FORM AND SEND MESSAGE IF CODE SECURE IS NOT VALID ,MESSAGE SAVE IN DATABASE BUT I WANNA AFTER CHECK CODE SECURE(THE CODE SECURE IS VALID) MESSAGE STORE IN MYSQLHOW CAN I CHANGE THIS CODE

<?php session_start(); require('php-captcha.inc.php'); if (empty($_POST['user_code'])) { echo 'please code entered'; } else if (PhpCaptcha::Validate($_POST['user_code'])) { echo 'Valid code entered'; } else { echo 'Invalid code entered';}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" ;Dir:rtl /><title>Untitled Document</title><link href="my.css" rel="stylesheet" type="text/css" /><link href="cs/default.css" rel="stylesheet" type="text/css" /><link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" /><link href="css/complete_form.css" rel="stylesheet" type="text/css"><script type="text/javascript" src="SpryAssets/contact.js"></script><script type="text/javascript" src="jquery.js"></script><script type="text/javascript"> function refresh(){ img = document.getElementById('Captcha'); //Change the image img.src = 'visual-captcha.php?' + Math.random(); } </script><script type="text/javascript"><!--var timeout = 500;var closetimer = 0;var ddmenuitem = 0;// open hidden layerfunction mopen(id){ // cancel close timer mcancelclosetime(); // close old layer if(ddmenuitem) ddmenuitem.style.visibility = 'hidden'; // get new layer and show it ddmenuitem = document.getElementById(id); ddmenuitem.style.visibility = 'visible';}// close showed layerfunction mclose(){ if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';}// go close timerfunction mclosetime(){ closetimer = window.setTimeout(mclose, timeout);}// cancel close timerfunction mcancelclosetime(){ if(closetimer) { window.clearTimeout(closetimer); closetimer = null; }}// close layer when click-outdocument.onclick = mclose; // --></script></head><center><body background="" ><table width="917" height="800" border="0"> <tr> <td height="103" class="header" > </td> </tr> <tr> <td height="10"><ul id="sddm" > <div id="m3" onmouseover="mcancelclosetime()" onmouseout="mclosetime()"> <a href="#">ASP Dropdown</a> <a href="#">Pulldown menu</a> <a href="#">AJAX dropdown</a> <a href="#">DIV dropdown</a> <a href="#">Pulldown menu</a> <a href="#">AJAX dropdown</a> <a href="#">DIV dropdown</a> </div> </li> </ul></td> </tr> <tr> <td align="left" valign="top"> <div class="sidebarbox-border bg-blue02"> </div> <div class="container-content-sidebar"> <div class="content-pagetitle dircss "> </div> <div class="contentbox-container"> <div class=" dir " > <p> <form action="" name="contact" method="post" class=" dir "name="signup" id="signup" ><center><table summary="Demonstration form"> <tbody> <tr> <td><label for="name">نام :</label></td> <td><input name="name" size="35" maxlength="34" type="text"></td> </tr> <tr> <td><label for="email">ایمیل:</label></td> <td ><input name="email" size="35" maxlength="25" type="text"></td> </tr> <tr> <td><label for="comment">پیام:</label></td> <td><textarea style="width:250px;height:150px" name="comment" type="text"rows="5" cols"100" maxlength=""></textarea></td> </tr> <tr> <td></td>'); <td><img src="visual-captcha.php" width="200" height="60" alt="Visual CAPTCHA" id="Captcha" /><br><input type="button" onClick="refresh(); " class="button" value="ریست کد"></input><p/><label for="user_code">code secure:</label><input name="user_code" size="20" type="text"> <font color="#CC0033"><?php echo $valid;?></font> </td> </tr> <tr> <td> </td> <td><input name="Submit" value="ارسال" type="submit" class="button" ></td> <td><input type="reset" value="پاک کردن" name="B2" class="button"> </td> </tr></table></body></center></html>
captcha.inc.php code
<?phpsession_start();/**************************************************  *************/   /* PhpCaptcha - A visual and audio CAPTCHA generation library		  Software License Agreement (BSD License)		  Copyright (C) 2005-2006, Edward Eliot.	  All rights reserved.	   	  Redistribution and use in source and binary forms, with or without	  modification, are permitted provided that the following conditions are met: 		 * Redistributions of source code must retain the above copyright		   notice, this list of conditions and the following disclaimer.		 * Redistributions in binary form must reproduce the above copyright		   notice, this list of conditions and the following disclaimer in the		   documentation and/or other materials provided with the distribution.		 * Neither the name of Edward Eliot nor the names of its contributors		   may be used to endorse or promote products derived from this software		   without specific prior written permission of Edward Eliot. 	  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND ANY	  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED	  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE	  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY	  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES	  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;	  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND	  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT	  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS	  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.		  Last Updated:  18th April 2006							   */   /**************************************************  *************/	   /************************ Documentation ************************/   /*	   Documentation is available at http://www.ejeliot.com/pages/2	   */   /************************ Default Options **********************/	   // start a PHP session - this class uses sessions to store the generated   // code. Comment out if you are calling already from your application   	   // class defaults - change to effect globally	   define('"CAPTCHA_SESSION_ID"', 'php_captcha');   define('CAPTCHA_WIDTH', 200); // max 500   define('CAPTCHA_HEIGHT', 50); // max 200   define('CAPTCHA_NUM_CHARS', 5);   define('CAPTCHA_NUM_LINES', 70);   define('CAPTCHA_CHAR_SHADOW', false);   define('CAPTCHA_OWNER_TEXT', '');   define('CAPTCHA_CHAR_SET', ''); // defaults to A-Z   define('CAPTCHA_CASE_INSENSITIVE', true);   define('CAPTCHA_BACKGROUND_IMAGES', '');   define('CAPTCHA_MIN_FONT_SIZE', 16);   define('CAPTCHA_MAX_FONT_SIZE', 25);   define('CAPTCHA_USE_COLOUR', false);   define('CAPTCHA_FILE_TYPE', 'jpeg');   define('CAPTCHA_FLITE_PATH', '/usr/bin/flite');   define('CAPTCHA_AUDIO_PATH', '/tmp/'); // must be writeable by PHP process	   /************************ End Default Options **********************/	   // don't edit below this line (unless you want to change the class!)	   class PhpCaptcha {	  var $oImage;	  var $aFonts;	  var $iWidth;	  var $iHeight;	  var $iNumChars;	  var $iNumLines;	  var $iSpacing;	  var $bCharShadow;	  var $sOwnerText;	  var $aCharSet;	  var $bCaseInsensitive;	  var $vBackgroundImages;	  var $iMinFontSize;	  var $iMaxFontSize;	  var $bUseColour;	  var $sFileType;	  var $sCode = '';	   	  function PhpCaptcha(		 $aFonts, // array of TrueType fonts to use - specify full path		 $iWidth = CAPTCHA_WIDTH, // width of image		 $iHeight = CAPTCHA_HEIGHT // height of image	  ) {		 // get parameters		 $this->aFonts = $aFonts;		 $this->SetNumChars(CAPTCHA_NUM_CHARS);		 $this->SetNumLines(CAPTCHA_NUM_LINES);		 $this->DisplayShadow(CAPTCHA_CHAR_SHADOW);		 $this->SetOwnerText(CAPTCHA_OWNER_TEXT);		 $this->SetCharSet(CAPTCHA_CHAR_SET);		 $this->CaseInsensitive(CAPTCHA_CASE_INSENSITIVE);		 $this->SetBackgroundImages(CAPTCHA_BACKGROUND_IMAGES);		 $this->SetMinFontSize(CAPTCHA_MIN_FONT_SIZE);		 $this->SetMaxFontSize(CAPTCHA_MAX_FONT_SIZE);		 $this->UseColour(CAPTCHA_USE_COLOUR);		 $this->SetFileType(CAPTCHA_FILE_TYPE);  		 $this->SetWidth($iWidth);		 $this->SetHeight($iHeight);	  }	   	  function CalculateSpacing() {		 $this->iSpacing = (int)($this->iWidth / $this->iNumChars);	  }	   	  function SetWidth($iWidth) {		 $this->iWidth = $iWidth;		 if ($this->iWidth > 500) $this->iWidth = 500; // to prevent perfomance impact		 $this->CalculateSpacing();	  }	   	  function SetHeight($iHeight) {		 $this->iHeight = $iHeight;		 if ($this->iHeight > 200) $this->iHeight = 200; // to prevent performance impact	  }	   	  function SetNumChars($iNumChars) {		 $this->iNumChars = $iNumChars;		 $this->CalculateSpacing();	  }	   	  function SetNumLines($iNumLines) {		 $this->iNumLines = $iNumLines;	  }	   	  function DisplayShadow($bCharShadow) {		 $this->bCharShadow = $bCharShadow;	  }	   	  function SetOwnerText($sOwnerText) {		 $this->sOwnerText = $sOwnerText;	  }	   	  function SetCharSet($vCharSet) {		 // check for input type		 if (is_array($vCharSet)) {			$this->aCharSet = $vCharSet;		 } else {			if ($vCharSet != '') {			   // split items on commas			   $aCharSet = explode(',', $vCharSet);			 			   // initialise array			   $this->aCharSet = array();			 			   // loop through items			   foreach ($aCharSet as $sCurrentItem) {				  // a range should have 3 characters, otherwise is normal character				  if (strlen($sCurrentItem) == 3) {					 // split on range character					 $aRange = explode('-', $sCurrentItem);				   					 // check for valid range					 if (count($aRange) == 2 && $aRange[0] < $aRange[1]) {						// create array of characters from range						$aRange = range($aRange[0], $aRange[1]);					  						// add to charset array						$this->aCharSet = array_merge($this->aCharSet, $aRange);					 }				  } else {					 $this->aCharSet[] = $sCurrentItem;				  }			   }			}		 }	  }	   	  function CaseInsensitive($bCaseInsensitive) {		 $this->bCaseInsensitive = $bCaseInsensitive;	  }	   	  function SetBackgroundImages($vBackgroundImages) {		 $this->vBackgroundImages = $vBackgroundImages;	  }	   	  function SetMinFontSize($iMinFontSize) {		 $this->iMinFontSize = $iMinFontSize;	  }	   	  function SetMaxFontSize($iMaxFontSize) {		 $this->iMaxFontSize = $iMaxFontSize;	  }	   	  function UseColour($bUseColour) {		 $this->bUseColour = $bUseColour;	  }	   	  function SetFileType($sFileType) {		 // check for valid file type		 if (in_array($sFileType, array('gif', 'png', 'jpeg'))) {			$this->sFileType = $sFileType;		 } else {			$this->sFileType = 'jpeg';		 }	  }	   	  function DrawLines() {		 for ($i = 0; $i < $this->iNumLines; $i++) {			// allocate colour			if ($this->bUseColour) {			   $iLineColour = imagecolorallocate($this->oImage, rand(100, 250), rand(100, 250), rand(100, 250));			} else {			   $iRandColour = rand(100, 250);			   $iLineColour = imagecolorallocate($this->oImage, $iRandColour, $iRandColour, $iRandColour);			}			 			// draw line			imageline($this->oImage, rand(0, $this->iWidth), rand(0, $this->iHeight), rand(0, $this->iWidth), rand(0, $this->iHeight), $iLineColour);		 }	  }	   	  function DrawOwnerText() {		 // allocate owner text colour		 $iBlack = imagecolorallocate($this->oImage, 0, 0, 0);		 // get height of selected font		 $iOwnerTextHeight = imagefontheight(2);		 // calculate overall height		 $iLineHeight = $this->iHeight - $iOwnerTextHeight - 4;		  		 // draw line above text to separate from CAPTCHA		 imageline($this->oImage, 0, $iLineHeight, $this->iWidth, $iLineHeight, $iBlack);		  		 // write owner text		 imagestring($this->oImage, 2, 3, $this->iHeight - $iOwnerTextHeight - 3, $this->sOwnerText, $iBlack);		  		 // reduce available height for drawing CAPTCHA		 $this->iHeight = $this->iHeight - $iOwnerTextHeight - 5;	  }	   	  function GenerateCode() {		 // reset code		 $this->sCode = '';		  		 // loop through and generate the code letter by letter		 for ($i = 0; $i < $this->iNumChars; $i++) {			if (count($this->aCharSet) > 0) {			   // select random character and add to code string			   $this->sCode .= $this->aCharSet[array_rand($this->aCharSet)];			} else {			   // select random character and add to code string			   $this->sCode .= chr(rand(65, 90));			}		 }		  		 // save code in session variable		 if ($this->bCaseInsensitive) {			$_SESSION["CAPTCHA_SESSION_ID"] = strtoupper($this->sCode);		 } else {			$_SESSION["CAPTCHA_SESSION_ID"] = $this->sCode;		 }	  }	   	  function DrawCharacters() {		 // loop through and write out selected number of characters		 for ($i = 0; $i < strlen($this->sCode); $i++) {			// select random font			$sCurrentFont = $this->aFonts[array_rand($this->aFonts)];			 			// select random colour			if ($this->bUseColour) {			   $iTextColour = imagecolorallocate($this->oImage, rand(0, 100), rand(0, 100), rand(0, 100));			 			   if ($this->bCharShadow) {				  // shadow colour				  $iShadowColour = imagecolorallocate($this->oImage, rand(0, 100), rand(0, 100), rand(0, 100));			   }			} else {			   $iRandColour = rand(0, 100);			   $iTextColour = imagecolorallocate($this->oImage, $iRandColour, $iRandColour, $iRandColour);			 			   if ($this->bCharShadow) {				  // shadow colour				  $iRandColour = rand(0, 100);				  $iShadowColour = imagecolorallocate($this->oImage, $iRandColour, $iRandColour, $iRandColour);			   }			}			 			// select random font size			$iFontSize = rand($this->iMinFontSize, $this->iMaxFontSize);			 			// select random angle			$iAngle = rand(-30, 30);			 			// get dimensions of character in selected font and text size			$aCharDetails = imageftbbox($iFontSize, $iAngle, $sCurrentFont, $this->sCode[$i], array());			 			// calculate character starting coordinates			$iX = $this->iSpacing / 4 + $i * $this->iSpacing;			$iCharHeight = $aCharDetails[2] - $aCharDetails[5];			$iY = $this->iHeight / 2 + $iCharHeight / 4;			 			// write text to image			imagefttext($this->oImage, $iFontSize, $iAngle, $iX, $iY, $iTextColour, $sCurrentFont, $this->sCode[$i], array());			 			if ($this->bCharShadow) {			   $iOffsetAngle = rand(-30, 30);							   $iRandOffsetX = rand(-5, 5);			   $iRandOffsetY = rand(-5, 5);							   imagefttext($this->oImage, $iFontSize, $iOffsetAngle, $iX + $iRandOffsetX, $iY + $iRandOffsetY, $iShadowColour, $sCurrentFont, $this->sCode[$i], array());			}		 }	  }	   	  function WriteFile($sFilename) {		 if ($sFilename == '') {			// tell browser that data is jpeg			header("Content-type: image/$this->sFileType");		 }		  		 switch ($this->sFileType) {			case 'gif':			   $sFilename != '' ? imagegif($this->oImage, $sFilename) : imagegif($this->oImage);			   break;			case 'png':			   $sFilename != '' ? imagepng($this->oImage, $sFilename) : imagepng($this->oImage);			   break;			default:			   $sFilename != '' ? imagejpeg($this->oImage, $sFilename) : imagejpeg($this->oImage);		 }	  }	   	  function Create($sFilename = '') {		 // check for required gd functions		 if (!function_exists('imagecreate') || !function_exists("image$this->sFileType") || ($this->vBackgroundImages != '' && !function_exists('imagecreatetruecolor'))) {			return false;		 }		  		 // get background image if specified and copy to CAPTCHA		 if (is_array($this->vBackgroundImages) || $this->vBackgroundImages != '') {			// create new image			$this->oImage = imagecreatetruecolor($this->iWidth, $this->iHeight);			 			// create background image			if (is_array($this->vBackgroundImages)) {			   $iRandImage = array_rand($this->vBackgroundImages);			   $oBackgroundImage = imagecreatefromjpeg($this->vBackgroundImages[$iRandImage]);			} else {			   $oBackgroundImage = imagecreatefromjpeg($this->vBackgroundImages);			}			 			// copy background image			imagecopy($this->oImage, $oBackgroundImage, 0, 0, 0, 0, $this->iWidth, $this->iHeight);			 			// free memory used to create background image			imagedestroy($oBackgroundImage);		 } else {			// create new image			$this->oImage = imagecreate($this->iWidth, $this->iHeight);		 }		  		 // allocate white background colour		 imagecolorallocate($this->oImage, 255, 255, 255);		  		 // check for owner text		 if ($this->sOwnerText != '') {			$this->DrawOwnerText();		 }		  		 // check for background image before drawing lines		 if (!is_array($this->vBackgroundImages) && $this->vBackgroundImages == '') {			$this->DrawLines();		 }		  		 $this->GenerateCode();		 		 $this->DrawCharacters();		  		 // write out image to file or browser		 $this->WriteFile($sFilename);		  		 // free memory used in creating image		 imagedestroy($this->oImage);		  		 return true;	  }	   	  // call this method statically	  function Validate($sUserCode, $bCaseInsensitive = true) {		 if ($bCaseInsensitive) {			$sUserCode = strtoupper($sUserCode);		 }		  		 if (!empty($_SESSION["CAPTCHA_SESSION_ID"]) && $sUserCode == $_SESSION["CAPTCHA_SESSION_ID"]) {			// clear to prevent re-use			unset($_SESSION["CAPTCHA_SESSION_ID"]);			 			return true;		 }		  		 return false;	  }   }	   // this class will only work correctly if a visual CAPTCHA has been created first using PhpCaptcha   class AudioPhpCaptcha {	  var $sFlitePath;	  var $sAudioPath;	  var $sCode;	   	  function AudioPhpCaptcha(		 $sFlitePath = CAPTCHA_FLITE_PATH, // path to flite binary		 $sAudioPath = CAPTCHA_AUDIO_PATH // the location to temporarily store the generated audio CAPTCHA	  ) {		 $this->SetFlitePath($sFlitePath);		 $this->SetAudioPath($sAudioPath);		  		 // retrieve code if already set by previous instance of visual PhpCaptcha		 if (isset($_SESSION["CAPTCHA_SESSION_ID"])) {			$this->sCode = $_SESSION["CAPTCHA_SESSION_ID"];		 }	  }	   	  function SetFlitePath($sFlitePath) {		 $this->sFlitePath = $sFlitePath;	  }	   	  function SetAudioPath($sAudioPath) {		 $this->sAudioPath = $sAudioPath;	  }	   	  function Mask($sText) {		 $iLength = strlen($sText);		  		 // loop through characters in code and format		 $sFormattedText = '';		 for ($i = 0; $i < $iLength; $i++) {			// comma separate all but first and last characters			if ($i > 0 && $i < $iLength - 1) {			   $sFormattedText .= ', ';			} elseif ($i == $iLength - 1) { // precede last character with "and"			   $sFormattedText .= ' and ';			}			$sFormattedText .= $sText[$i];		 }		  		 $aPhrases = array(			"The %1\$s characters are as follows: %2\$s",			"%2\$s, are the %1\$s letters",			"Here are the %1\$s characters: %2\$s",			"%1\$s characters are: %2\$s",			"%1\$s letters: %2\$s"		 );		  		 $iPhrase = array_rand($aPhrases);		  		 return sprintf($aPhrases[$iPhrase], $iLength, $sFormattedText);	  }	   	  function Create() {		 $sText = $this->Mask($this->sCode);		 $sFile = md5($this->sCode.time());		  		 // create file with flite		 shell_exec("$this->sFlitePath -t \"$sText\" -o $this->sAudioPath$sFile.wav");		  		 // set headers		 header('Content-type: audio/x-wav');		 header("Content-Disposition: attachment;filename=$sFile.wav");		  		 // output to browser		 echo file_get_contents("$this->sAudioPath$sFile.wav");		  		 // delete temporary file		 @unlink("$this->sAudioPath$sFile.wav");	  }   }	   // example sub class   class PhpCaptchaColour extends PhpCaptcha {	  function PhpCaptchaColour($aFonts, $iWidth = CAPTCHA_WIDTH, $iHeight = CAPTCHA_HEIGHT) {		 // call parent constructor		 parent::PhpCaptcha($aFonts, $iWidth, $iHeight);		  		 // set options		 $this->UseColour(true);	  }   }?>

Link to comment
Share on other sites

I WROTE THIS CODE FOE COMMENT FORM WHEN I FILL OUT FORM AND SEND MESSAGE IF CODE SECURE IS NOT VALID ,MESSAGE SAVE IN DATABASE BUT I WANNA AFTER CHECK CODE SECURE(THE CODE SECURE IS VALID) MESSAGE STORE IN MYSQLHOW CAN I CHANGE THIS CODE

<?php session_start(); require('php-captcha.inc.php'); if (empty($_POST['user_code'])) { echo 'please code entered'; } else if (PhpCaptcha::Validate($_POST['user_code'])) { echo 'Valid code entered'; } else { echo 'Invalid code entered';}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" ;Dir:rtl /><title>Untitled Document</title><link href="my.css" rel="stylesheet" type="text/css" /><link href="cs/default.css" rel="stylesheet" type="text/css" /><link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" /><link href="css/complete_form.css" rel="stylesheet" type="text/css"><script type="text/javascript" src="SpryAssets/contact.js"></script><script type="text/javascript" src="jquery.js"></script><script type="text/javascript"> function refresh(){ img = document.getElementById('Captcha'); //Change the image img.src = 'visual-captcha.php?' + Math.random(); } </script><script type="text/javascript"><!--var timeout = 500;var closetimer = 0;var ddmenuitem = 0;// open hidden layerfunction mopen(id){ // cancel close timer mcancelclosetime(); // close old layer if(ddmenuitem) ddmenuitem.style.visibility = 'hidden'; // get new layer and show it ddmenuitem = document.getElementById(id); ddmenuitem.style.visibility = 'visible';}// close showed layerfunction mclose(){ if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';}// go close timerfunction mclosetime(){ closetimer = window.setTimeout(mclose, timeout);}// cancel close timerfunction mcancelclosetime(){ if(closetimer) { window.clearTimeout(closetimer); closetimer = null; }}// close layer when click-outdocument.onclick = mclose; // --></script></head><center><body background="" ><table width="917" height="800" border="0"> <tr> <td height="103" class="header" > </td> </tr> <tr> <td height="10"><ul id="sddm" > <div id="m3" onmouseover="mcancelclosetime()" onmouseout="mclosetime()"> <a href="#">ASP Dropdown</a> <a href="#">Pulldown menu</a> <a href="#">AJAX dropdown</a> <a href="#">DIV dropdown</a> <a href="#">Pulldown menu</a> <a href="#">AJAX dropdown</a> <a href="#">DIV dropdown</a> </div> </li> </ul></td> </tr> <tr> <td align="left" valign="top"> <div class="sidebarbox-border bg-blue02"> </div> <div class="container-content-sidebar"> <div class="content-pagetitle dircss "> </div> <div class="contentbox-container"> <div class=" dir " > <p> <form action="" name="contact" method="post" class=" dir "name="signup" id="signup" ><center><table summary="Demonstration form"> <tbody> <tr> <td><label for="name">نام :</label></td> <td><input name="name" size="35" maxlength="34" type="text"></td> </tr> <tr> <td><label for="email">ایمیل:</label></td> <td ><input name="email" size="35" maxlength="25" type="text"></td> </tr> <tr> <td><label for="comment">پیام:</label></td> <td><textarea style="width:250px;height:150px" name="comment" type="text"rows="5" cols"100" maxlength=""></textarea></td> </tr> <tr> <td></td>'); <td><img src="visual-captcha.php" width="200" height="60" alt="Visual CAPTCHA" id="Captcha" /><br><input type="button" onClick="refresh(); " class="button" value="ریست کد"></input><p/><label for="user_code">code secure:</label><input name="user_code" size="20" type="text"> <font color="#CC0033"><?php echo $valid;?></font> </td> </tr> <tr> <td> </td> <td><input name="Submit" value="ارسال" type="submit" class="button" ></td> <td><input type="reset" value="پاک کردن" name="B2" class="button"> </td> </tr></table></body></center></html>
captcha.inc.php code
<?phpsession_start();/**************************************************  *************/   /* PhpCaptcha - A visual and audio CAPTCHA generation library		  Software License Agreement (BSD License)		  Copyright (C) 2005-2006, Edward Eliot.	  All rights reserved.	   	  Redistribution and use in source and binary forms, with or without	  modification, are permitted provided that the following conditions are met: 		 * Redistributions of source code must retain the above copyright		   notice, this list of conditions and the following disclaimer.		 * Redistributions in binary form must reproduce the above copyright		   notice, this list of conditions and the following disclaimer in the		   documentation and/or other materials provided with the distribution.		 * Neither the name of Edward Eliot nor the names of its contributors		   may be used to endorse or promote products derived from this software		   without specific prior written permission of Edward Eliot. 	  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND ANY	  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED	  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE	  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY	  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES	  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;	  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND	  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT	  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS	  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.		  Last Updated:  18th April 2006							   */   /**************************************************  *************/	   /************************ Documentation ************************/   /*	   Documentation is available at http://www.ejeliot.com/pages/2	   */   /************************ Default Options **********************/	   // start a PHP session - this class uses sessions to store the generated   // code. Comment out if you are calling already from your application   	   // class defaults - change to effect globally	   define('"CAPTCHA_SESSION_ID"', 'php_captcha');   define('CAPTCHA_WIDTH', 200); // max 500   define('CAPTCHA_HEIGHT', 50); // max 200   define('CAPTCHA_NUM_CHARS', 5);   define('CAPTCHA_NUM_LINES', 70);   define('CAPTCHA_CHAR_SHADOW', false);   define('CAPTCHA_OWNER_TEXT', '');   define('CAPTCHA_CHAR_SET', ''); // defaults to A-Z   define('CAPTCHA_CASE_INSENSITIVE', true);   define('CAPTCHA_BACKGROUND_IMAGES', '');   define('CAPTCHA_MIN_FONT_SIZE', 16);   define('CAPTCHA_MAX_FONT_SIZE', 25);   define('CAPTCHA_USE_COLOUR', false);   define('CAPTCHA_FILE_TYPE', 'jpeg');   define('CAPTCHA_FLITE_PATH', '/usr/bin/flite');   define('CAPTCHA_AUDIO_PATH', '/tmp/'); // must be writeable by PHP process	   /************************ End Default Options **********************/	   // don't edit below this line (unless you want to change the class!)	   class PhpCaptcha {	  var $oImage;	  var $aFonts;	  var $iWidth;	  var $iHeight;	  var $iNumChars;	  var $iNumLines;	  var $iSpacing;	  var $bCharShadow;	  var $sOwnerText;	  var $aCharSet;	  var $bCaseInsensitive;	  var $vBackgroundImages;	  var $iMinFontSize;	  var $iMaxFontSize;	  var $bUseColour;	  var $sFileType;	  var $sCode = '';	   	  function PhpCaptcha(		 $aFonts, // array of TrueType fonts to use - specify full path		 $iWidth = CAPTCHA_WIDTH, // width of image		 $iHeight = CAPTCHA_HEIGHT // height of image	  ) {		 // get parameters		 $this->aFonts = $aFonts;		 $this->SetNumChars(CAPTCHA_NUM_CHARS);		 $this->SetNumLines(CAPTCHA_NUM_LINES);		 $this->DisplayShadow(CAPTCHA_CHAR_SHADOW);		 $this->SetOwnerText(CAPTCHA_OWNER_TEXT);		 $this->SetCharSet(CAPTCHA_CHAR_SET);		 $this->CaseInsensitive(CAPTCHA_CASE_INSENSITIVE);		 $this->SetBackgroundImages(CAPTCHA_BACKGROUND_IMAGES);		 $this->SetMinFontSize(CAPTCHA_MIN_FONT_SIZE);		 $this->SetMaxFontSize(CAPTCHA_MAX_FONT_SIZE);		 $this->UseColour(CAPTCHA_USE_COLOUR);		 $this->SetFileType(CAPTCHA_FILE_TYPE);  		 $this->SetWidth($iWidth);		 $this->SetHeight($iHeight);	  }	   	  function CalculateSpacing() {		 $this->iSpacing = (int)($this->iWidth / $this->iNumChars);	  }	   	  function SetWidth($iWidth) {		 $this->iWidth = $iWidth;		 if ($this->iWidth > 500) $this->iWidth = 500; // to prevent perfomance impact		 $this->CalculateSpacing();	  }	   	  function SetHeight($iHeight) {		 $this->iHeight = $iHeight;		 if ($this->iHeight > 200) $this->iHeight = 200; // to prevent performance impact	  }	   	  function SetNumChars($iNumChars) {		 $this->iNumChars = $iNumChars;		 $this->CalculateSpacing();	  }	   	  function SetNumLines($iNumLines) {		 $this->iNumLines = $iNumLines;	  }	   	  function DisplayShadow($bCharShadow) {		 $this->bCharShadow = $bCharShadow;	  }	   	  function SetOwnerText($sOwnerText) {		 $this->sOwnerText = $sOwnerText;	  }	   	  function SetCharSet($vCharSet) {		 // check for input type		 if (is_array($vCharSet)) {			$this->aCharSet = $vCharSet;		 } else {			if ($vCharSet != '') {			   // split items on commas			   $aCharSet = explode(',', $vCharSet);			 			   // initialise array			   $this->aCharSet = array();			 			   // loop through items			   foreach ($aCharSet as $sCurrentItem) {				  // a range should have 3 characters, otherwise is normal character				  if (strlen($sCurrentItem) == 3) {					 // split on range character					 $aRange = explode('-', $sCurrentItem);				   					 // check for valid range					 if (count($aRange) == 2 && $aRange[0] < $aRange[1]) {						// create array of characters from range						$aRange = range($aRange[0], $aRange[1]);					  						// add to charset array						$this->aCharSet = array_merge($this->aCharSet, $aRange);					 }				  } else {					 $this->aCharSet[] = $sCurrentItem;				  }			   }			}		 }	  }	   	  function CaseInsensitive($bCaseInsensitive) {		 $this->bCaseInsensitive = $bCaseInsensitive;	  }	   	  function SetBackgroundImages($vBackgroundImages) {		 $this->vBackgroundImages = $vBackgroundImages;	  }	   	  function SetMinFontSize($iMinFontSize) {		 $this->iMinFontSize = $iMinFontSize;	  }	   	  function SetMaxFontSize($iMaxFontSize) {		 $this->iMaxFontSize = $iMaxFontSize;	  }	   	  function UseColour($bUseColour) {		 $this->bUseColour = $bUseColour;	  }	   	  function SetFileType($sFileType) {		 // check for valid file type		 if (in_array($sFileType, array('gif', 'png', 'jpeg'))) {			$this->sFileType = $sFileType;		 } else {			$this->sFileType = 'jpeg';		 }	  }	   	  function DrawLines() {		 for ($i = 0; $i < $this->iNumLines; $i++) {			// allocate colour			if ($this->bUseColour) {			   $iLineColour = imagecolorallocate($this->oImage, rand(100, 250), rand(100, 250), rand(100, 250));			} else {			   $iRandColour = rand(100, 250);			   $iLineColour = imagecolorallocate($this->oImage, $iRandColour, $iRandColour, $iRandColour);			}			 			// draw line			imageline($this->oImage, rand(0, $this->iWidth), rand(0, $this->iHeight), rand(0, $this->iWidth), rand(0, $this->iHeight), $iLineColour);		 }	  }	   	  function DrawOwnerText() {		 // allocate owner text colour		 $iBlack = imagecolorallocate($this->oImage, 0, 0, 0);		 // get height of selected font		 $iOwnerTextHeight = imagefontheight(2);		 // calculate overall height		 $iLineHeight = $this->iHeight - $iOwnerTextHeight - 4;		  		 // draw line above text to separate from CAPTCHA		 imageline($this->oImage, 0, $iLineHeight, $this->iWidth, $iLineHeight, $iBlack);		  		 // write owner text		 imagestring($this->oImage, 2, 3, $this->iHeight - $iOwnerTextHeight - 3, $this->sOwnerText, $iBlack);		  		 // reduce available height for drawing CAPTCHA		 $this->iHeight = $this->iHeight - $iOwnerTextHeight - 5;	  }	   	  function GenerateCode() {		 // reset code		 $this->sCode = '';		  		 // loop through and generate the code letter by letter		 for ($i = 0; $i < $this->iNumChars; $i++) {			if (count($this->aCharSet) > 0) {			   // select random character and add to code string			   $this->sCode .= $this->aCharSet[array_rand($this->aCharSet)];			} else {			   // select random character and add to code string			   $this->sCode .= chr(rand(65, 90));			}		 }		  		 // save code in session variable		 if ($this->bCaseInsensitive) {			$_SESSION["CAPTCHA_SESSION_ID"] = strtoupper($this->sCode);		 } else {			$_SESSION["CAPTCHA_SESSION_ID"] = $this->sCode;		 }	  }	   	  function DrawCharacters() {		 // loop through and write out selected number of characters		 for ($i = 0; $i < strlen($this->sCode); $i++) {			// select random font			$sCurrentFont = $this->aFonts[array_rand($this->aFonts)];			 			// select random colour			if ($this->bUseColour) {			   $iTextColour = imagecolorallocate($this->oImage, rand(0, 100), rand(0, 100), rand(0, 100));			 			   if ($this->bCharShadow) {				  // shadow colour				  $iShadowColour = imagecolorallocate($this->oImage, rand(0, 100), rand(0, 100), rand(0, 100));			   }			} else {			   $iRandColour = rand(0, 100);			   $iTextColour = imagecolorallocate($this->oImage, $iRandColour, $iRandColour, $iRandColour);			 			   if ($this->bCharShadow) {				  // shadow colour				  $iRandColour = rand(0, 100);				  $iShadowColour = imagecolorallocate($this->oImage, $iRandColour, $iRandColour, $iRandColour);			   }			}			 			// select random font size			$iFontSize = rand($this->iMinFontSize, $this->iMaxFontSize);			 			// select random angle			$iAngle = rand(-30, 30);			 			// get dimensions of character in selected font and text size			$aCharDetails = imageftbbox($iFontSize, $iAngle, $sCurrentFont, $this->sCode[$i], array());			 			// calculate character starting coordinates			$iX = $this->iSpacing / 4 + $i * $this->iSpacing;			$iCharHeight = $aCharDetails[2] - $aCharDetails[5];			$iY = $this->iHeight / 2 + $iCharHeight / 4;			 			// write text to image			imagefttext($this->oImage, $iFontSize, $iAngle, $iX, $iY, $iTextColour, $sCurrentFont, $this->sCode[$i], array());			 			if ($this->bCharShadow) {			   $iOffsetAngle = rand(-30, 30);							   $iRandOffsetX = rand(-5, 5);			   $iRandOffsetY = rand(-5, 5);							   imagefttext($this->oImage, $iFontSize, $iOffsetAngle, $iX + $iRandOffsetX, $iY + $iRandOffsetY, $iShadowColour, $sCurrentFont, $this->sCode[$i], array());			}		 }	  }	   	  function WriteFile($sFilename) {		 if ($sFilename == '') {			// tell browser that data is jpeg			header("Content-type: image/$this->sFileType");		 }		  		 switch ($this->sFileType) {			case 'gif':			   $sFilename != '' ? imagegif($this->oImage, $sFilename) : imagegif($this->oImage);			   break;			case 'png':			   $sFilename != '' ? imagepng($this->oImage, $sFilename) : imagepng($this->oImage);			   break;			default:			   $sFilename != '' ? imagejpeg($this->oImage, $sFilename) : imagejpeg($this->oImage);		 }	  }	   	  function Create($sFilename = '') {		 // check for required gd functions		 if (!function_exists('imagecreate') || !function_exists("image$this->sFileType") || ($this->vBackgroundImages != '' && !function_exists('imagecreatetruecolor'))) {			return false;		 }		  		 // get background image if specified and copy to CAPTCHA		 if (is_array($this->vBackgroundImages) || $this->vBackgroundImages != '') {			// create new image			$this->oImage = imagecreatetruecolor($this->iWidth, $this->iHeight);			 			// create background image			if (is_array($this->vBackgroundImages)) {			   $iRandImage = array_rand($this->vBackgroundImages);			   $oBackgroundImage = imagecreatefromjpeg($this->vBackgroundImages[$iRandImage]);			} else {			   $oBackgroundImage = imagecreatefromjpeg($this->vBackgroundImages);			}			 			// copy background image			imagecopy($this->oImage, $oBackgroundImage, 0, 0, 0, 0, $this->iWidth, $this->iHeight);			 			// free memory used to create background image			imagedestroy($oBackgroundImage);		 } else {			// create new image			$this->oImage = imagecreate($this->iWidth, $this->iHeight);		 }		  		 // allocate white background colour		 imagecolorallocate($this->oImage, 255, 255, 255);		  		 // check for owner text		 if ($this->sOwnerText != '') {			$this->DrawOwnerText();		 }		  		 // check for background image before drawing lines		 if (!is_array($this->vBackgroundImages) && $this->vBackgroundImages == '') {			$this->DrawLines();		 }		  		 $this->GenerateCode();		 		 $this->DrawCharacters();		  		 // write out image to file or browser		 $this->WriteFile($sFilename);		  		 // free memory used in creating image		 imagedestroy($this->oImage);		  		 return true;	  }	   	  // call this method statically	  function Validate($sUserCode, $bCaseInsensitive = true) {		 if ($bCaseInsensitive) {			$sUserCode = strtoupper($sUserCode);		 }		  		 if (!empty($_SESSION["CAPTCHA_SESSION_ID"]) && $sUserCode == $_SESSION["CAPTCHA_SESSION_ID"]) {			// clear to prevent re-use			unset($_SESSION["CAPTCHA_SESSION_ID"]);			 			return true;		 }		  		 return false;	  }   }	   // this class will only work correctly if a visual CAPTCHA has been created first using PhpCaptcha   class AudioPhpCaptcha {	  var $sFlitePath;	  var $sAudioPath;	  var $sCode;	   	  function AudioPhpCaptcha(		 $sFlitePath = CAPTCHA_FLITE_PATH, // path to flite binary		 $sAudioPath = CAPTCHA_AUDIO_PATH // the location to temporarily store the generated audio CAPTCHA	  ) {		 $this->SetFlitePath($sFlitePath);		 $this->SetAudioPath($sAudioPath);		  		 // retrieve code if already set by previous instance of visual PhpCaptcha		 if (isset($_SESSION["CAPTCHA_SESSION_ID"])) {			$this->sCode = $_SESSION["CAPTCHA_SESSION_ID"];		 }	  }	   	  function SetFlitePath($sFlitePath) {		 $this->sFlitePath = $sFlitePath;	  }	   	  function SetAudioPath($sAudioPath) {		 $this->sAudioPath = $sAudioPath;	  }	   	  function Mask($sText) {		 $iLength = strlen($sText);		  		 // loop through characters in code and format		 $sFormattedText = '';		 for ($i = 0; $i < $iLength; $i++) {			// comma separate all but first and last characters			if ($i > 0 && $i < $iLength - 1) {			   $sFormattedText .= ', ';			} elseif ($i == $iLength - 1) { // precede last character with "and"			   $sFormattedText .= ' and ';			}			$sFormattedText .= $sText[$i];		 }		  		 $aPhrases = array(			"The %1\$s characters are as follows: %2\$s",			"%2\$s, are the %1\$s letters",			"Here are the %1\$s characters: %2\$s",			"%1\$s characters are: %2\$s",			"%1\$s letters: %2\$s"		 );		  		 $iPhrase = array_rand($aPhrases);		  		 return sprintf($aPhrases[$iPhrase], $iLength, $sFormattedText);	  }	   	  function Create() {		 $sText = $this->Mask($this->sCode);		 $sFile = md5($this->sCode.time());		  		 // create file with flite		 shell_exec("$this->sFlitePath -t \"$sText\" -o $this->sAudioPath$sFile.wav");		  		 // set headers		 header('Content-type: audio/x-wav');		 header("Content-Disposition: attachment;filename=$sFile.wav");		  		 // output to browser		 echo file_get_contents("$this->sAudioPath$sFile.wav");		  		 // delete temporary file		 @unlink("$this->sAudioPath$sFile.wav");	  }   }	   // example sub class   class PhpCaptchaColour extends PhpCaptcha {	  function PhpCaptchaColour($aFonts, $iWidth = CAPTCHA_WIDTH, $iHeight = CAPTCHA_HEIGHT) {		 // call parent constructor		 parent::PhpCaptcha($aFonts, $iWidth, $iHeight);		  		 // set options		 $this->UseColour(true);	  }   }?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...