Solved

Unique number

Posted on 2011-02-22
5
342 Views
Last Modified: 2012-05-11
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
Comment
Question by:mattimeikalainen
[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
  • 2
  • 2
5 Comments
 

Expert Comment

by:Jason_Lotito
ID: 34952642
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
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 34952964
@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
 

Author Comment

by:mattimeikalainen
ID: 34953261
How I put that bportlock solutions on my page? echo something?
0
 

Author Comment

by:mattimeikalainen
ID: 34953314
That number series goes in forms hidden field <input type="hidden" name="number" value="<?php echo something">
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34953626
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check mysql insert 12 39
php error 27 55
PHP Underscores vs. Directory separators 5 26
How do I speed up this PDO query 4 13
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 …

756 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