Solved

PHP code that randomize number or letters combination

Posted on 2010-08-24
5
289 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thank you
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

762 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

6 Experts available now in Live!

Get 1:1 Help Now