Location: PHPKode > scripts > caText n caMath > catext-n-camath/catext.class.php
<?php

  /*
	-- caText(Captcha on Text) v.1 --
	Class for preventing robot or non human to posting in a form
	Similiar idea with captcha but it use random text and pseudo text.
	@author	 : coliq <hide@address.com>
	@release : 20/08/2007 22:50	

	if you made some improvement please email me ^_^
  */	

  class catext {
		var $clstrong;		// Key text color
		var $clsoft;		// pseudo text color
		var $key_length;	// max count of key text length
		var $text_length;	// length of catext display
		var $key_result;	// catext result/printed
		var $text_result;	// catext result/printed
		
		// Constructor
		function catext(){
			$this->set_length(3, 5);
			$this->set_color('red', '#808080');
			$this->text_result = ""; 
			$this->key_result = "";
		}
		
		// set color
		function set_color($clstrong, $clsoft){
			$this->clstrong = $clstrong;
			$this->clsoft = $clsoft;
		}

		/* set length
		 * @nkey : int 
		 * @ntext : int 
		*/
		function set_length($nkey, $ntext){
			$this->key_length = (int) $nkey;
			$this->text_length = (int) $ntext;
		}
	
		function get_key(){
			return $this->key_result;
		}

		function get_textresult(){
			return $this->text_result;
		}

		/* Generate code
		 * print captcha text and return function value with key 
		*/
	    function generate($enc = ""){
		  // random char composition	
		  $salt = "abcdefABCDEF1234567890";
		  //$salt = "aBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1234567890";
		  
		  $len = strlen($salt);
		  mt_srand(10000000*(double)microtime());

			  for ($i = 0; $i < $this->text_length; $i++){
				 if(($i == mt_rand($i,$this->text_length - 1)) && $this->key_length > 0){
					 $text = $salt[mt_rand(0,$len - 1)];
					 $this->text_result .= "<font style='bold' color='{$this->clstrong}'>" . $text . "</font>";
					 $this->key_result .= $text;
					 $this->key_length--;
				 } else
					$this->text_result .= "<font style='bold' color='{$this->clsoft}'>" . $salt[mt_rand(0,$len - 1)] . "</font>";
			  }
			
			// direct print code
			print($this->text_result);
			
			switch ($enc) {
				case "md5":
					return $this->catext_md5($this->key_result);
					break;
				case "sha1":
					return $this->catext_sha1($this->key_result);
					break;
				default:
					return trim($this->key_result);
			}
		}

		/************ENCRYPTION FUNCTION*************/
		function catext_md5($keyin){
			return md5($keyin);
		}

		function catext_sha1($keyin){
			return sha1($keyin);
		}		

  }//end catext
?>
Return current item: caText n caMath