Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Generate Keys on from a list when refreshed

  1. #1
    Join Date
    Sep 2008
    Posts
    44
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Generate Keys on from a list when refreshed

    To understand this I will explain. On some sites they give away beta codes etc. They have it so that it gets emailed to you or it gets displayed. I am looking for that kind of a code that displays a different code once a code has been used(refreshed).

    Thanks for your time

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Do you want the code to be random? Or be generated from a database.

    If a database, you could have something like this:
    Code:
    CODE | USED|
    --------------
    94nD9|  0    |
    --------------
    94nD9|  1    |
    --------------
    64y2u|  1    |
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    dkblackhawk (01-17-2010)

  4. #3
    Join Date
    Sep 2008
    Posts
    44
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    Do you want the code to be random? Or be generated from a database.

    If a database, you could have something like this:
    Code:
    CODE | USED|
    --------------
    94nD9|  0    |
    --------------
    94nD9|  1    |
    --------------
    64y2u|  1    |
    Yes be generated from a database thanks for pointing that out. So how exactly would I use the above code? and how would i make it so that it wont show if it is the same ip or something along those lines. I have the php code to get the IP.

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Wait - don't you want it so that each code can only be used once?

    If yes - then every time the code is used, the USED BOOL gets set to 1.
    Jeremy | jfein.net

  6. #5
    Join Date
    Sep 2008
    Posts
    44
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    Wait - don't you want it so that each code can only be used once?

    If yes - then every time the code is used, the USED BOOL gets set to 1.
    I only want each code to be used once. How would I set this up?

  7. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Ok, let me just go over this.
    You're database structure would start off like this:

    Code:
    CREATE TABLE IF NOT EXISTS `code` (
      `codes` text NOT NULL,
      `used` tinyint(1) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    Notice how all of the USED are set to 0? That means that they haven't been used yet.

    Then - to validate codes you'd do:
    PHP Code:
    <?php
    if(!isset($_GET['code'])){ die("No code"); }
    include(
    'conn.php');

    $query "SELECT * FROM  `code` WHERE `codes` = '{$_GET['code']}' AND `used` = '0'";
    $result mysql_query($query) or die(mysql_error());
    if(
    mysql_num_rows($result) > 0){
      
    $set_query "UPDATE `code` SET `used` = '1' WHERE `codes` = '{$_GET['code']}' AND `used` = '0' ";
      
    mysql_query($set_query) or die(mysql_error());
      echo 
    "You used code: ".$_GET['code'];
    } else {
      die(
    "This code has already been used!");
    }
    ?>
    Jeremy | jfein.net

  8. The Following User Says Thank You to Nile For This Useful Post:

    dkblackhawk (01-17-2010)

  9. #7
    Join Date
    Sep 2008
    Posts
    44
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks just need to figure out how to edit a database on my host ^_^"

  10. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Sorry, I can't help with that! Haha.

    Also - if you need to generate tons of codes and put them in a database, the code to get the code is:
    PHP Code:
    <?php
    function generate_code(){
      
    $abc range("A""Z");
      
    $abc_i range("a""z");
      
    $num range("1""9");
      
    $r = array();
      for(
    $i 0$i 5$i++){
        
    $random rand(03);
        switch(
    $random){
          case 
    0:
            
    $r[] = $abc[rand(0count($abc)-1)];
          break;
          case 
    1:
            
    $r[] = $abc_i[rand(0count($abc_i)-1)];
          break;
          case 
    2:
            
    $r[] = $num[rand(0count($num)-1)];
          break;
          case 
    3:
            
    $r[] = $num[rand(0count($num)-1)];
          break;
        }
      }
      return 
    implode($r);
    }
    echo 
    generate_code();
    ?>
    While it might return the same code twice - it is very unlikely.
    Jeremy | jfein.net

  11. #9
    Join Date
    Sep 2008
    Posts
    44
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    I found out I can't edit the database tables so is there a way that I can just use a standard file (php?) and do it from there?

  12. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Aww man! What kind of host are you using?
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •