Jump to content


Generate Random Strings with PHP


3 replies to this topic

#1 Joe

    Root Administrator

  • EC-Owner
  • PipPipPipPipPipPipPipPip
  • 393 posts

Posted 06 January 2010 - 10:45 AM

Here is a little code that shows you how to create a random string in php, very handy for creating random passwords, etc.

$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$password = "";
for($i=0; $i<7; $i++){
     $randNum = mt_rand(0, 61);
     $password .= $chars[$randNum];
}
echo $password;


To change the length of the string simply edit the second number in the for loop.
joemeyer@exchangecore.com
Any requests for designs,scripts or web-hosting please visit our Client Center.
Thank you for supporting ExchangeCore!

#2 Tim-7967

    EC Newbie

  • Members
  • Pip
  • 8 posts

Posted 03 August 2010 - 03:57 PM

Awesome... I've been looking for a way to do that in PHP correctly but don't understand how to append and mush things together in PHP enough to be able to do it alone.

Thanks!

#3 Joe

    Root Administrator

  • EC-Owner
  • PipPipPipPipPipPipPipPip
  • 393 posts

Posted 07 August 2010 - 06:58 PM

View PostTim-7967, on 03 August 2010 - 03:57 PM, said:

Awesome... I've been looking for a way to do that in PHP correctly but don't understand how to append and mush things together in PHP enough to be able to do it alone.

Thanks!


In php the equivalent of what in most languages is += for strings, php uses .=

Sample:
<?php
    $myString = "";
    for($i = 0; $i < 10; $i++);
        $myString .= $i;
?>


Sample Result:

Quote

0123456789


Hope that clarifies that for you.
joemeyer@exchangecore.com
Any requests for designs,scripts or web-hosting please visit our Client Center.
Thank you for supporting ExchangeCore!

#4 Tim-7967

    EC Newbie

  • Members
  • Pip
  • 8 posts

Posted 07 August 2010 - 10:07 PM

Cool, Thanks!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users