PHP code that randomize number or letters combination

I need PHP randomize function or code for my newsletter confirmation page..
do you know one?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Beverley PortlockConnect With a Mentor Commented:
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

gilemConnect With a Mentor Commented:
Well simply, you can use

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;

FugasConnect With a Mentor Commented:
I always use something like this
echo md5(time().$username.$mail);

Open in new window

Gatherer_HadeConnect With a Mentor Commented:
Use the uniqid() function.  It is gauranteed to be unique.  A random number may not be unique.

BRMarketingAuthor Commented:
thank you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.