Solved

Generate unique 4 char values

Posted on 2007-03-28
8
236 Views
Last Modified: 2008-02-01
I need to generate some PINs for a login system. I only have 39 values but I would like them to be unique,
how would I do that in PHP?
0
Comment
Question by:EddieShipman
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Expert Comment

by:richswyatt
ID: 18808895
There are several ways.

Are you saving the pins somewhere to make sure that they are not reused?
If so - we could do something like this:

$uniqueval = rand(0,9999);
/// do some code to get a list of previously assigned pins (not shown)
// (now do a while loop to go through the previously assigned vals)
while($x = 0)
{
  if(in_array($uniqueval,$something)
 {

 }


}
0
 
LVL 5

Expert Comment

by:richswyatt
ID: 18808921
oops hit a button on the keyboard and it submitted - try again

if(in_array($uniqueval,$something) {   $uniqueval=rand(0,9999); $x =0; }
else { $x = 1; }

At the conclusion of the loop $uniqueval should be a unique 4 digit value not previously assigned.
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 18809051
I have 39 records in a mySQL table named Teams, the field is PIN.
0
 
LVL 29

Accepted Solution

by:
rdivilbiss earned 500 total points
ID: 18809082
If you want a 4 digit pin, then rand(0,9999); needs to be rand(1111,9999);

<?PHP
$pins = array();
$maxPins = 39;

for ($idx=0; $idx<$maxPins; $idx++) {
      $unique = false;
      while (!$unique) {
            $tmpPin = rand(1111,9999);
            if (!array_search($tmpPin, $pins)) {
                  $unique=true;
            }
      }
      array_push($pins,$tmpPin);
       // you can update a table row $idx here
}

print_r($pins);
?>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:yodercm
ID: 18809085
do{
                $newpin=rand()%1000000000;
                $result=mysql_query("Select * from Teams where PIN=$newpin;");
}while(mysql_num_rows($result)>0);

At the end of this loop, you will have $newpin equal to a random value that is not in the table.
0
 
LVL 29

Expert Comment

by:rdivilbiss
ID: 18809207
Why do multiple database lookups, when you can determine the uniqueness in code alone, and only hit the database when you update a record with the new PIN.

Also, (although not explicitly stated by the asker,) a PIN is normally a 4 digit integer, where rand()%1000000000; will return many different lengths.
0
 
LVL 27

Expert Comment

by:yodercm
ID: 18809271
OK, so just change the rand statement to generate whatever size number you want for the pin.

However, unless the pins are all generated at the same time and only once, you cannot save them in code.  You will have to do database lookups each time you generate a new one for a new user.
0
 
LVL 29

Expert Comment

by:rdivilbiss
ID: 18809329
He says he already has 39 rows, so only hit each row one time, e.g. to update it with the unique PIN.

If he was adding a new row subsequent, then your query until unique would be a good choice.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 …

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now