Solved

PHP code that randomize number or letters combination

Posted on 2010-08-24
5
290 Views
Last Modified: 2013-12-13
I need PHP randomize function or code for my newsletter confirmation page..
do you know one?
0
Comment
Question by:Braveheartli
5 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 300 total points
ID: 33510591
Try the attached. You can vary the size of the result string as well as the letters that appear in it
// 009 Generate a near unique, non-sequential code
     //
     // Parameters: $codeSize - The size of the code to generate. Longer is more secure
     //             $codeChars - The set of characters to use to generate the key
     //
     // Returns: A simple string
     //
     function nearUniqueCode( $codeSize = 10, $codeChars= "" ) {
          $code  = "";
          $chars = ($codeChars != "" ) ? $codeChars : "abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNPQRTUVWXYZ0123456789";
          $chLen = strlen( $chars );


          // Build the code
          //
          for ( $i=0; $i < $codeSize; $i++ ) {
               $pos =  mt_rand(0, $chLen-1);
               $code .= substr( $chars, $pos, 1 );
          }

          return $code;
     }

Open in new window

0
 
LVL 2

Assisted Solution

by:gilem
gilem earned 100 total points
ID: 33510595
Well simply, you can use http://us.php.net/manual/en/function.rand.php.

However, if you want something like a confirmation code that is hard to guess,
function genRandomString() {
    $length = 10;
    $characters = ’0123456789abcdefghijklmnopqrstuvwxyz’;
    $string = ”;    

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters)-1)];
    }

    return $string;
}

ref.
http://www.lost-in-code.com/programming/php-code/php-random-string-with-numbers-and-letters/
0
 
LVL 4

Assisted Solution

by:Fugas
Fugas earned 50 total points
ID: 33510760
I always use something like this
<?php 
$username="peter";
$mail="blah@blah.com";
echo md5(time().$username.$mail);
?>

Open in new window

0
 
LVL 2

Assisted Solution

by:Gatherer_Hade
Gatherer_Hade earned 50 total points
ID: 33510963
Use the uniqid() function.  It is gauranteed to be unique.  A random number may not be unique.

0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 33511164
thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now