Solved

PHP code that randomize number or letters combination

Posted on 2010-08-24
5
293 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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 …

710 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