pin code generation in php

  • stevewise
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Mar 16, 2009
  • Posts: 7
  • Status: Offline

Post June 25th, 2010, 9:31 am

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 25th, 2010, 9:31 am

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 3415
  • Loc: Richland, WA
  • Status: Offline

Post June 25th, 2010, 10:00 am

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.
#define NULL (::rand() % 2)
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Joined: Mar 12, 2007
  • Posts: 6228
  • Loc: South-Africa
  • Status: Offline

Post June 28th, 2010, 7:09 am

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. }
Let's leave all our *plum* where it is and go live in the jungle ...

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 138 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.