pin code generation in php

  • stevewise
  • Newbie
  • Newbie
  • stevewise
  • Posts: 7

Post 3+ Months Ago

please i need a php script that can automatically generate pin no that are unique which users can use along side with other fields to sign up. Which after first use it should no longer be valid, as they must have registered.
Thanks very much as any contribution is highly welcome.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

There are a few ways you could do this.

Generate a random 4 digit number and check it against a database of currently generated numbers. If it already exists generate a new one and check again.

Create a table of all 4 digit numbers (4^10) run a query to select a single number at random. Then mark that number as used/delete it.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

SpooF wrote:
There are a few ways you could do this.

Generate a random 4 digit number and check it against a database of currently generated numbers. If it already exists generate a new one and check again.

Create a table of all 4 digit numbers (4^10) run a query to select a single number at random. Then mark that number as used/delete it.

Out of those I think the first is the best, this way you don't limit yourself to a certain number of pins and there is less preparation (imo):
PHP Code: [ Select ]
function create_pin($length=5){
  $new = false;
  while($new == false){
    $pin = substr(md5(microtime()),0,$length);
    $sql_select = "SELECT * FROM user WHERE pin='{$pin}'";
    $sql_query = mysql_query($sql_select);
    $new = (mysql_num_rows($sql_query) > 0) ? false : true;
  }
  return $pin;
}
  1. function create_pin($length=5){
  2.   $new = false;
  3.   while($new == false){
  4.     $pin = substr(md5(microtime()),0,$length);
  5.     $sql_select = "SELECT * FROM user WHERE pin='{$pin}'";
  6.     $sql_query = mysql_query($sql_select);
  7.     $new = (mysql_num_rows($sql_query) > 0) ? false : true;
  8.   }
  9.   return $pin;
  10. }

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 97 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.