1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Generating Serial Number or Code in PHP/SQL

Discussion in 'PHP' started by richbate, Jun 25, 2007.

  1. #1
    I'm in the basic stages of building a new site where users can only register using a unique serial number that has been printed on a promotional flyer or leaflet.
    I'm thinking of having about 1000 serial numbers. Now for the registration, i have the syntax sorted out to be able to verify and remove the used serial number.

    OK, what i need help is with generating these serial numbers, i've had several ideas on what to do for this.

    First idea is, i should use the rand() function for each number on the 15 digit serial, then checking it against database to see if its already on there.

    Second idea was to use the rand() and MD5() function, and generate a number between 1 and 10000, then MD5 Hash it to randomize it.

    Then, with the script to generate a serial, i just loop it till i feel we have enough numbers.

    If anyone else has a theory or even a script that could do this, let me know!

    Rich
     
    richbate, Jun 25, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    MD5 only contains hexadecimal values, which are characters from a-f, and numbers from 0-9. So all serials will look pretty equal.

    I made my own random string generator, so it'll be more random.

    
    function swd_rand($length = 10, $letters = true, $numbers = true, $case = 'i')
    {
    	$chars = array();
    	
    	if ($numbers)
    	{
    		$chars = array_merge($chars, range(48, 57));
    	}
    
    	if ($letters OR !$numbers)
    	{
    		$chars = array_merge($chars, range(65, 90), range(97, 122));
    	}
    	
    	for ($string = ''; strlen($string) < $length; $string .= chr($chars[array_rand($chars)]));
    	
    	switch ($case)
    	{
    		case 'i': default: return $string;
    		case 'u': return strtoupper($string);
    		case 'l': return strtolower($string);
    	}
    }
    
    
    PHP:
    
    $string = swd_rand(25);
    
    PHP:

    Other than that, I don't see issues with your idea. To verify and remove just do:

    
    
    mysql_query("
        DELETE FROM codes
        WHERE code = '". mysql_real_escape_string($code) ."'
    ") OR die(mysql_error());
    
    if (mysql_affected_rows())
    {
        // Code is valid, and has been removed. Process registration...
    }
    else
    {
        // Invalid code.
    }
    
    PHP:
     
    nico_swd, Jun 25, 2007 IP
  3. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    you could use the following code:


    <?php
    function assign_rand_value($num)
    {
    // accepts 1 - 36
    switch($num)
    {
    case "1":
    $rand_value = "a";
    break;
    case "2":
    $rand_value = "b";
    break;
    case "3":
    $rand_value = "c";
    break;
    case "4":
    $rand_value = "d";
    break;
    case "5":
    $rand_value = "e";
    break;
    case "6":
    $rand_value = "f";
    break;
    case "7":
    $rand_value = "g";
    break;
    case "8":
    $rand_value = "h";
    break;
    case "9":
    $rand_value = "i";
    break;
    case "10":
    $rand_value = "j";
    break;
    case "11":
    $rand_value = "k";
    break;
    case "12":
    $rand_value = "l";
    break;
    case "13":
    $rand_value = "m";
    break;
    case "14":
    $rand_value = "n";
    break;
    case "15":
    $rand_value = "o";
    break;
    case "16":
    $rand_value = "p";
    break;
    case "17":
    $rand_value = "q";
    break;
    case "18":
    $rand_value = "r";
    break;
    case "19":
    $rand_value = "s";
    break;
    case "20":
    $rand_value = "t";
    break;
    case "21":
    $rand_value = "u";
    break;
    case "22":
    $rand_value = "v";
    break;
    case "23":
    $rand_value = "w";
    break;
    case "24":
    $rand_value = "x";
    break;
    case "25":
    $rand_value = "y";
    break;
    case "26":
    $rand_value = "z";
    break;
    case "27":
    $rand_value = "0";
    break;
    case "28":
    $rand_value = "1";
    break;
    case "29":
    $rand_value = "2";
    break;
    case "30":
    $rand_value = "3";
    break;
    case "31":
    $rand_value = "4";
    break;
    case "32":
    $rand_value = "5";
    break;
    case "33":
    $rand_value = "6";
    break;
    case "34":
    $rand_value = "7";
    break;
    case "35":
    $rand_value = "8";
    break;
    case "36":
    $rand_value = "9";
    break;
    }
    return $rand_value;
    }


    function get_rand_id($length)
    {
    if($length>0)
    {
    $rand_id="";
    for($i=1; $i<=$length; $i++)
    {
    mt_srand((double)microtime() * 1000000);
    $num = mt_rand(1,36);
    $rand_id .= assign_rand_value($num);
    }
    }
    return $rand_id;
    }




    function UniqueSerial($s){
    require_once("OpenDB.php");
    // make sure this salt value is unique
    $sql = "select * from `your-table` where [serial] = \"$s\"";
    $result = mysql_query($sql);
    if(!$result) die("mysql error: " . mysql_error());
    $num = mysql_num_rows($result);
    require_once("CloseDB.php");
    if($num>0) UniqueSerial(get_rand_id(50));
    else return $s;
    }


    ?>

    all you need to do is save this code and include it into your program. Then simply call UniqueSerial as follows:

    UniqueSerial(get_rand_id(50)); (50 is the length of the serial number)

    I hope this helps.
     
    NoamBarz, Jun 25, 2007 IP
  4. e39m5

    e39m5 Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey Rich,

    People have already posted a way to generate a random ID, my sugggestion is for inserting into the db. Currently, checking and inserting for each serial will take 2000 queries. With an array you can reduce the number of queries to 1.

    
    $serials = Array();
      //Loop until the array has 1000 values
    while(count($serials)<1000){
          //Generate a random string and add it to the array
        $serials[] = randomString(15);
          //Remove duplicates in the array
        $serials = array_unique($serials);
    }
    foreach($serials as $value){
        $values .= "('".$value."'),";
    }
    $values = rtrim($values,',');
    $sql = "INSERT INTO serials (serial) VALUES $values";
    mysql_query($sql);
    
    PHP:
    e39m5
     
    e39m5, Jun 25, 2007 IP