1

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);
?>
user1
  • 39
  • 1
  • 8

3 Answers3

1

I always like to do this for random letters and numbers using md5. its not completely random, but it still returns a random string with both numbers and letters.

$i = rand(100000000, 9999999999)
$i = md5($i);           //creates a hashed string with 32 characters
$i = str_split($i, 10); //10 is the amount of characters of your string max 32
$i = $i[0];
Sjoerd de Wit
  • 2,353
  • 5
  • 26
  • 45
kpp
  • 800
  • 2
  • 11
  • 27
0

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();
TMH
  • 6,096
  • 7
  • 51
  • 88
Sjoerd de Wit
  • 2,353
  • 5
  • 26
  • 45
0

You could do something like this.

<?php
$code = substr(md5(microtime()),rand(0,26),10);
$im = imagecreate(100, 30);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 5, 0, 0, $code, $textcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

Sources used:

https://stackoverflow.com/a/5438778/1978142
http://www.php.net//manual/en/function.imagestring.php

Community
  • 1
  • 1
user1978142
  • 7,946
  • 3
  • 17
  • 20