This is a very basic script that generates a string 25 characters long using the characters listed in $string. You can modify the length of the string by changing the 25 in the for() statement to the length you desire.

<?php
$string = "abcdefghijklmnopqrstuvwxyz0123456789"; //List usable characters
for($i=0;$i<25;$i++){ //Repeat 25 times
$pos = rand(0,36); //We have 37 characters in our $string so we take a random one between 0 and 36
$str .= $string{$pos}; //Add the randomly selected character to our output variable
}
echo $str; //Print our randomly generated string
?>

The code outputs:
2d2c3vceidd1xk1zhqhkjsey2
Refresh the page to see another randomly generated string.

This code can be used to create a random login ticket to ensure the user is authenticated or can be used as a salt to a password when encrypting. The following is an example of this.

<?php
$RawPassword = "password"; //Store the users raw unedited password
$string = "abcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<10;$i++){
$pos = rand(0,36);
$salt .= $string{$pos}; //Store the salt to the $salt variable
}
 
$SaltedPassword = sha1(RawPassword,$salt); //Hash the password using SHA1 encryption and the $salt
 
echo "Salted Password: ".$SaltedPassword; //Save the password to the database, or in this case, print the result.
echo "<br>Salt: ".$salt;
echo "<br>Raw Password: ".$RawPassword;
?>

The code outputs:
Salted Password: P{-^’ {puM
Salt: 9n2yobqs3t
Raw Password: password

Leave a Reply

*