Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP code that randomize number or letters combination

Posted on 2010-08-24
5
Medium Priority
?
299 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 1200 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 400 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 200 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

916 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