Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

Unique number

How I get in php unique 5 character number sequence  example 12093, 48923, 83492 and so on... Everytime when user come in to the page there is unigue five  number sequence?
0
mattimeikalainen
Asked:
mattimeikalainen
  • 2
  • 2
1 Solution
 
Jason_LotitoCommented:
Depends on what you mean by unique.  If you mean each time a page shows up, you want a random number that can't be guessed, you can use:

<?php
mt_srand(time());
$random = mt_rand(10000,99999);
echo $random;

Open in new window


At the same time, if you want make that number unique to the user, you'd need to insert the number into a database and then store a reference to that number with the user.  However, if you do that, you limit yourself and the number of numbers.  Should numbers expire?  In this case, could you explain a bit more what you are trying to do?
0
 
Beverley PortlockCommented:
@Jason - mt_rand doesn't require seeding

@ O/P - the problem with a 5 digit number is that they are not very random since there are only 90,000 of them. A 5 character string has a lot more choice and it is not that hard to build a small function to do what you want

     // 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 = 5, $codeChars= "" ) {
          $code  = "";
          $chars = ($codeChars != "" ) ? $codeChars : "ABCDEFGHJKLMNPQRTUVWXYZ0123456789";
          $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
 
mattimeikalainenAuthor Commented:
How I put that bportlock solutions on my page? echo something?
0
 
mattimeikalainenAuthor Commented:
That number series goes in forms hidden field <input type="hidden" name="number" value="<?php echo something">
0
 
Beverley PortlockCommented:
Assuming you have included the function code I posted above, then it could be used lke so

<input type="hidden" name="number" value="<?php echo nearUniqueCode(); >"  />
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now