Moderators: macek, egami, gesf

function RandomString($length=8)
{
//Generate alphabet array
$alphabet = "";
for ($i = 0; $i < 26; $i++) {
@$alphabet .= chr(97+$i).",";
}
$alphabet = explode(",", $alphabet);
$RandomString = "";
//Check that length is between 1 and 40
if ($length > 40 || $length < 1) {
$length = 8;
}
//Generate Random string
for ($i = 1; $i <= $length; $i++) {
$rand = rand(0, 25);
$RandomString .= $alphabet[$rand];
}
return $RandomString;
}
//Connect to database
//Select database
$sql = mysql_query("SELECT * FROM table WHERE field = '".RandomString()."'");
if (mysql_num_rows($sql) > 0) {
echo "RandomString is already taken";
}
else {
echo "Not taken";
}

Joel wrote:
- Code: Select all
function RandomString($length=8)
{
//Generate alphabet array
$alphabet = "";
for ($i = 0; $i < 26; $i++) {
@$alphabet .= chr(97+$i).",";
}
$alphabet = explode(",", $alphabet);
$RandomString = "";
//Check that length is between 1 and 40
if ($length > 40 || $length < 1) {
$length = 8;
}
//Generate Random string
for ($i = 1; $i <= $length; $i++) {
$rand = rand(0, 25);
$RandomString .= $alphabet[$rand];
}
return $RandomString;
}
<?
function rand_string($length = 16) {
$rand_string = '';
$choice = 0;
for($i = 0, $i < $length; $i++) {
// pick lowercase, uppercase, or number
$choice = rand(0,2);
switch($choice) {
case 0: // lowercase
$num = rand(0, 25);
$rand_string .= chr(97 + $num);
break;
case 1: // uppercase
$num = rand(0, 25);
$rand_string .= chr(65 + $num);
break;
case 2: // number
$rand_string .= rand(0, 9);
break;
}
}
return $rand_string;
}
?><?
function rand_string($length = 16) {
// list all valid characters (faster than generating them on-the-fly)
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
'abcdefghijklmnopqrstuvwxyz' .
'0123456789';
$chars_len = strlen($chars);
$rand_string = '';
for($i = 0, $i < $length, $i++) {
$rand_string .= $chars{rand(0, $chars_len - 1)};
}
return $rand_string;
}
?>

Joel wrote:Or in other words, Swirlee, I love you.
<?
function rand_string($length = 16) {
// each set of characters is in a different array element
$chars = array(
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', // uppercase
'abcdefghijklmnopqrstuvwxyz', // lowercase
'0123456789' // numerals
);
$rand_string = '';
for($i = 0, $i < $length, $i++) {
// choose a set of characters at random
$which_list = rand(0, count($chars));
// choose a character (index) from that set
$which_char = rand(0, strlen($chars[$which_list]));
// append the character to $rand_string
$rand_string .= $chars[$which_list]{$which_char};
}
return $rand_string;
}
?>

do {
$randomstring = RandomString();
$sql = mysql_query("SELECT * FROM table WHERE field = '".$randomstring."'");
} while (mysql_num_rows($sql) > 0;



<?
function rand_string($length = 16) {
// each set of characters is in a different array element
$chars = array(
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', // uppercase
'abcdefghijklmnopqrstuvwxyz', // lowercase
'0123456789' // numerals
);
$rand_string = '';
for($i = 0; $i < $length; $i++) {
// choose a set of characters at random
$which_list = rand(0, count($chars) - 1);
// choose a character (index) from that set
$which_char = rand(0, strlen($chars[$which_list]));
// append the character to $rand_string
$rand_string .= $chars[$which_list]{$which_char};
}
return $rand_string;
}
?>
sorry to break your heart but it's still not 100% -- the second output of your code generated 15 characters. and once i tried it out it came all the way down to 13 on an occasion. It definetly hits 16 more often though if that makes you feel better. I can't find anything wrong with it though.
function rand_string($length = 16) {
// each set of characters is in a different array element
$chars = array(
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', // uppercase
'abcdefghijklmnopqrstuvwxyz', // lowercase
'0123456789' // numerals
);
$rand_string = '';
for($i = 0; $i < $length; $i++) {
// choose a set of characters at random
$which_list = rand(0, count($chars) - 1);
// choose a character (index) from that set
$which_char = rand(0, strlen($chars[$which_list]) - 1);
// append the character to $rand_string
$rand_string .= $chars[$which_list]{($which_char)};
}
return $rand_string;
}

function rand_string($length = 16) {
// each set of characters is in a different array element
$chars = array(
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', // uppercase
'abcdefghijklmnopqrstuvwxyz', // lowercase
'0123456789' // numerals
);
$rand_string = '';
for($i = 0; $i < $length; $i++) {
// choose a set of characters at random
$which_list = rand(0, count($chars) - 1);
// choose a character (index) from that set
$which_char = rand(0, strlen(($chars[$which_list])) - 1);
// append the character to $rand_string
$rand_string .= $chars[$which_list]{($which_char)};
}
return $rand_string;
}



Users browsing this forum: No registered users and 1 guest