spiritssight
02-24-2008, 07:40 PM
Hello All,
I am looking for any feed back on improving the code below, I am trying to design this that its good for a few different uses, one being to create user_ids, record_ids, and activation codes / verifie codes, I have not come up with any other reasons yet but if I do I want to be able to use this same function and just add any speical part like I did for the record Id and user id part as you will see, is there a better way to write this and make it have a high change of being uniqe for the first try. I notice alot of the output has letters and numbers repeat in the same string like one had four f in it, is this good or a bad thing?
any how I would like any ideas to make this a better function.
Thanks for your insight into this.
Sincerely,
Christopher
<?php
function random( $uses = NULL, $type = NULL, $length = 5 )
{
switch( $type )
{
default:
$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
break;
case 'u_letters':
$pattern = "ABCDEFGHIJKLMONPQRSTUVWXYZABCDEFGHIJKLMONPQRSTUVWXYZ";
break;
case 'l_letters':
$pattern = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
break;
case 'm_letters':
$pattern = "aBcDeFgHiJkLmNoPqRsTuVwXyZAbCdEfGhIjKlMnOpQrStUvWxYz";
break;
case 'numbers':
$pattern = "12345678901234567890123456789012345678901234567890";
break;
}
switch($uses)
{
default:
$default = '';
for($i=0; $i < $length; $i++)
{
$default .= $pattern{mt_rand(0,35)};
}
return( $default );
break;
case 'user_id':
include '/home/dev/www/lib/db_config_ro-dev.php';
include '/home/dev/www/lib/db_conn-select.php';
$user_id = 'U-';
do
{
// $user_id = 'U-' . sprintf("%'09d", mt_rand(1, 999999999));
for($i=0; $i < $length; $i++)
{
$user_id .= $pattern{mt_rand(1, 35)};
}
$query_s = "SELECT user_id FROM access_credentials WHERE user_id = '$user_id'";
$result_s = mysql_query($query_s) OR die("Sorry, unable to select record: " . mysql_error());
}
while(mysql_num_rows($result_s) != 0); // don't do again if = 0
return( $user_id );
break;
case 'record_id':
include '/home/dev/www/lib/db_config_ro-dev.php';
include '/home/dev/www/lib/db_conn-select.php';
$record_id = 'R-';
do
{
// $record_id = 'R-' . sprintf("%'09d", mt_rand(1, 999999999));
for($i=0; $i < $length; $i++)
{
$record_id .= $pattern{mt_rand(1,35)};
}
$query_s = "SELECT record_id FROM record_status WHERE record_id = '$record_id'";
$result_s = mysql_query($query_s) OR die("Sorry, unable to select record: " . mysql_error());
}
while(mysql_num_rows($result_s) != 0); // don't do again if = 0
return( $record_id );
break;
}
// }
}
?>
I am looking for any feed back on improving the code below, I am trying to design this that its good for a few different uses, one being to create user_ids, record_ids, and activation codes / verifie codes, I have not come up with any other reasons yet but if I do I want to be able to use this same function and just add any speical part like I did for the record Id and user id part as you will see, is there a better way to write this and make it have a high change of being uniqe for the first try. I notice alot of the output has letters and numbers repeat in the same string like one had four f in it, is this good or a bad thing?
any how I would like any ideas to make this a better function.
Thanks for your insight into this.
Sincerely,
Christopher
<?php
function random( $uses = NULL, $type = NULL, $length = 5 )
{
switch( $type )
{
default:
$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
break;
case 'u_letters':
$pattern = "ABCDEFGHIJKLMONPQRSTUVWXYZABCDEFGHIJKLMONPQRSTUVWXYZ";
break;
case 'l_letters':
$pattern = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
break;
case 'm_letters':
$pattern = "aBcDeFgHiJkLmNoPqRsTuVwXyZAbCdEfGhIjKlMnOpQrStUvWxYz";
break;
case 'numbers':
$pattern = "12345678901234567890123456789012345678901234567890";
break;
}
switch($uses)
{
default:
$default = '';
for($i=0; $i < $length; $i++)
{
$default .= $pattern{mt_rand(0,35)};
}
return( $default );
break;
case 'user_id':
include '/home/dev/www/lib/db_config_ro-dev.php';
include '/home/dev/www/lib/db_conn-select.php';
$user_id = 'U-';
do
{
// $user_id = 'U-' . sprintf("%'09d", mt_rand(1, 999999999));
for($i=0; $i < $length; $i++)
{
$user_id .= $pattern{mt_rand(1, 35)};
}
$query_s = "SELECT user_id FROM access_credentials WHERE user_id = '$user_id'";
$result_s = mysql_query($query_s) OR die("Sorry, unable to select record: " . mysql_error());
}
while(mysql_num_rows($result_s) != 0); // don't do again if = 0
return( $user_id );
break;
case 'record_id':
include '/home/dev/www/lib/db_config_ro-dev.php';
include '/home/dev/www/lib/db_conn-select.php';
$record_id = 'R-';
do
{
// $record_id = 'R-' . sprintf("%'09d", mt_rand(1, 999999999));
for($i=0; $i < $length; $i++)
{
$record_id .= $pattern{mt_rand(1,35)};
}
$query_s = "SELECT record_id FROM record_status WHERE record_id = '$record_id'";
$result_s = mysql_query($query_s) OR die("Sorry, unable to select record: " . mysql_error());
}
while(mysql_num_rows($result_s) != 0); // don't do again if = 0
return( $record_id );
break;
}
// }
}
?>