1function generateRandomString($length = 25) {
2 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
3 $charactersLength = strlen($characters);
4 $randomString = '';
5 for ($i = 0; $i < $length; $i++) {
6 $randomString .= $characters[rand(0, $charactersLength - 1)];
7 }
8 return $randomString;
9}
10//usage
11$myRandomString = generateRandomString(5);
1function generateRandomString($length = 10) {
2 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
3 $charactersLength = strlen($characters);
4 $randomString = '';
5 for ($i = 0; $i < $length; $i++) {
6 $randomString .= $characters[rand(0, $charactersLength - 1)];
7 }
8 return $randomString;
9}
10
11Output the random string with the call below:
12
13// Echo the random string.
14// Optionally, you can give it a desired string length.
15echo generateRandomString();
1phpCopy<?php
2
3echo "Out1: ",substr(md5(time()), 0, 16),"\n";
4
5echo "Out2: ",substr(sha1(time()), 0, 16),"\n";
6
7echo "Out3: ",md5(time()),"\n";
8
9echo "Out4: ",sha1(time()),"\n";
10
11?>
12