Sunday 16 October 2011

Random Number And Alphabet Captcha Code With PHP

For Developing Random captcha code it is important to know Array , loop ,Array Rand and push function.

<? function randamString()
{
 $arr=array();
 for($i=65;$i<=91-1;$i++)
 { 
  array_push($arr,chr($i));
 }
 for($i=0;$i<=9;$i++)
 {
  array_push($arr,$i);
 } 
 $num=array_rand($arr, 6);
 $str.=$arr[$num[0]];
 $str.=$arr[$num[1]];
 $str.=$arr[$num[2]];
 $str.=$arr[$num[3]];
 $str.=$arr[$num[4]];
 $str.=$arr[$num[5]]; 
 return $str;
}
?>

Code Explanation 

 randamString() function is a user define function .
$arr is a variable that store array.
chr() — Return a specific character 
string chr ( int $ascii )
Returns a one-character string containing the character specified by ascii.

array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. 

Array random function 
               array_rand — Pick one or more random entries out of an array.

$num=array_rand($arr, 6); function randomly select 6 value. $str. concatenate all the strings. now echo the function  <?php echo $str=randamString(); ?>




                                          
Download Folder



No comments:

Post a Comment