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

	  /*
		-- caMath(Captcha on Mathematic) v.1 --
		Class for preventing robot or non human to posting in a form
		Similiar idea with captcha but it use simple mathematic operation.
		@author	 : coliq <hide@address.com>
		@release : 20/08/2007 22:50	
	
		if you made some improvement please email me ^_^

	  */	

	class camath{
		var $_min;			// min range value to random
		var $_max;			// max range value to random
		var $_resultNumber; // operation result 
		var $_resultText;	// text code to display

		function camath(){
			$this->_resultText = "";
			$this->set_range(0, 10);
		}
		
		function set_range($rmin, $rmax){
			$this->_min = $rmin;
			$this->_max = $rmax;
		}
		
		function get_key(){
			return $this->_resultNumber;
		}
		
		function get_textresult(){
			return $this->_resultText;
		}

		/*
		 * Function to "translate" mathematic operation
		 * @val1: int
		 * @val2: int
		 * @op: int; if you add/remove case section, please update generate(); function
		 */
		function translate($val1, $val2, $op = 0){
			
			switch ($op) {
				case 0:
					$this->_resultText = $val1 . ' + ' . $val2; 
					$this->_resultNumber = $val1 + $val2; 
					break;

				case 1:
					$this->_resultText = $val1 . ' - ' . $val2; 
					$this->_resultNumber = $val1 - $val2; 
					break;
				
				default:
					$this->translate($val1, $val2, 0);
			}
			
		}

		function generate($enc = ""){
			$val1 = rand($this->_min, $this->_max);
			$val2 = rand($this->_min, $this->_max);

			// update this value if you modify(add/remove case) translate(); function
			$opr = rand(0, 1); 

			$this->translate($val1, $val2, $opr);

			print $this->_resultText;// printing code
			
			switch ($enc) {
				case "md5":
					return $this->caMath_md5($this->_resultNumber);
					break;
				case "sha1":
					return $this->caMath_sha1($this->_resultNumber);
					break;
				default:
					return trim($this->_resultNumber);
			}

		}

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

		function caMath_sha1($keyin){
			return sha1($keyin);
		}
		
	}// end caMath class
?>
Return current item: caText n caMath