Search code examples
phprandomnumberscaptchaletters

Combine random letters and numbers


Does any of you know how to add random letters aswel as random numbers. thanks for you help.

<?php 
session_start(); 
$image = imagecreate(, ); 
$bgcolor = imagecolorallocate($image, , ,);
$textcolor = imagecolorallocate($image, , , ); 
$code = rand(00, 99); 




$_SESSION['code'] = ($code);
imagestring($image, , , , $code, $textcolor); 
header ("Content-type: image/png"); 
imagepng($image);
?>

Solution

  • i have a function for it

    function createRandomString() {
        $chars = "abcdefghijkmnopqrstuvwxyz023456789";
        srand((double)microtime()*1000000);
        $i = 0;
        $randomstring = '' ;
    
        while ($i <= 7) {
            $num = rand() % 33;
            $tmp = substr($chars, $num, 1);
            $randomstring = $randomstring . $tmp;
            $i++;
        }
    
        return $randomstring;
    
    }
    
    echo createRandomString();