Click to See Complete Forum and Search --> : Generating a random string help


HellgY
01-18-2006, 09:42 AM
Hello,
When a new user registers to my site he receives a message to the email address which he indicated. The link to the account activation is a page that received two values to a GET array, the user ID, which actually points which on the account that I'm about to activate, and a random string that was inserted to the DB at the time of the registration, any idea how to generate such a string?.
Thanks in advance!.

chazzy
01-18-2006, 09:53 AM
<?php
// function to generate random strings
function RandomString($length=32)
{
$randstr='';
srand((double)microtime()*1000000);
//our array add all letters and numbers if you wish
$chars = array ( 'a','b','c','d','e','f');
for ($rand = 0; $rand <= $length; $rand++)
{
$random = rand(0, count($chars) -1);
$randstr .= $chars[$random];
}
return $randstr;
}
?>


Just change the chars array to the possible characters you want to use.

HellgY
01-18-2006, 10:33 AM
OK, solved, Thanks Chazzy!,
Another question, i have the following text obtained in a variable, what will be the best way for easily replacing the variables names with their values without using the str_replace() function?.
Thanks in advance!.

"Hello!,".$_POST['name'].". Thanks for singing on to our site!, Please follow the link bellow within 24 hours since this notification was issued, otherwise it will void, and you will have to register all over again. regards, Total NBA Team team. Active registration: http://www.opendD4U.net?ID=".$userid."&code=".$activekey.""success"Hello!,".$_POST['name'].". Thanks for singing on to our site!, Please follow the link bellow within 24 hours since this notification was issued, otherwise it will void, and you will have to register all over again. regards, Total NBA Team team. Active registration: http://www.opendD4U.net?ID=".$userid."&code=".$activekey.""


I used the following syntax for my goal, It seemed more compact, but it hangs on submition, everything else works just fine without it.

$activekey="";
$i=0;
while(i<8)
{
$possible = "0123456789abcdfghijklmnopqrstvwxyz";
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1
);
if(!strstr($activekey, $char))
{
$activekey=$activekey."".$cahr;
$i++;
}

chazzy
01-18-2006, 02:17 PM
I'm not really sure what you mean.

I see this should be changed in your code though...


if(!strstr($activekey, $char))
{
$activekey=$activekey."".$char;
$i++;
}


you can typically use {$varname} to get the value as well, but I'm not really sure what the problem is.

bokeh
01-18-2006, 02:55 PM
No need for loops to generate a random alphanumeric string.function rand_string($len = 20){
$e = base64_encode(pack("h*", sha1(mt_rand())));
return substr(strtr($e, "+/=", "xyz"), 0, $len);
} As for the string with the variable you could just eval() (http://www.php.net/eval) it.

HellgY
01-18-2006, 03:13 PM
Thanks both of you Chazzy and Bokeh!, the eval function really helped!, tho the following syntax, even after the modification that Chazzy offered is hanging:
Code:

while(i<8) { $possible = "0123456789abcdfghijklmnopqrstvwxyz";
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1
);
if(!strstr($activekey, $char))
{
$activekey=$activekey."".$char;
$i++;
}

}

I'll go with wither Bokeh's or Chazzy's code then, i guess.
The 'XYZ' is the pool for the string generator?.

bokeh
01-18-2006, 03:30 PM
while(i<8)Variables in PHP are preceeded with a $.

HellgY
01-18-2006, 03:41 PM
LOL such an stupid mistake:\, thanks!.
I tried applying eval() for my code.
and it returns the following error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/nitay/domains/opend4u.net/public_html/Oleg/Register.php on line 90

For the code below with a reference to the line marked with '-':

$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,6})$";
if (eregi($regexp, $_POST['email']))
{
list (, $domain) = split ("@", $_POST['email']);
if(getmxrr($domain, $mxhost) or gethostbyname($domain))
{
$query="SELECT * FROM messages WHERE shortcut='registration'";
$result=mysql_query($query, $link);
$row=mysql_fetch_array($result, MYSQL_ASSOC);
$activekey=rand_string();
eval("\$row['body']=\"$row['body']\";");
print($row['body']);
mail($_POST['email'], $row['title'], $row['body']);
mysql_free_result($result);
}

NogDog
01-18-2006, 04:40 PM
No real need for new functions to create random strings: uniqid() (http://www.php.net/uniqid)

bokeh
01-18-2006, 08:21 PM
No real need for new functions to create random strings: uniqid() (http://www.php.net/uniqid)I knew there was a function for that I just couldn't remember what it was called. I tried searching the manual but couldn't find it.

chazzy
01-18-2006, 08:34 PM
No real need for new functions to create random strings: uniqid() (http://www.php.net/uniqid)

It's too bad that you can't limit the character set used, or else that would have been perfect for what i needed my original function for.