Solved

PHP code that randomize number or letters combination

Posted on 2010-08-24
5
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 …

626 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