View Full Version : Generate Keys on from a list when refreshed
dkblackhawk
01-17-2010, 03:54 PM
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 :D
Do you want the code to be random? Or be generated from a database.
If a database, you could have something like this:
CODE | USED|
--------------
94nD9| 0 |
--------------
94nD9| 1 |
--------------
64y2u| 1 |
dkblackhawk
01-17-2010, 03:59 PM
Do you want the code to be random? Or be generated from a database.
If a database, you could have something like this:
CODE | USED|
--------------
94nD9| 0 |
--------------
94nD9| 1 |
--------------
64y2u| 1 |
Yes be generated from a database :D 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.
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.
dkblackhawk
01-17-2010, 04:08 PM
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?
Ok, let me just go over this.
You're database structure would start off like this:
http://localhostr.com/files/9a8f4b/capture.png
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
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!");
}
?>
dkblackhawk
01-17-2010, 04:48 PM
Thanks just need to figure out how to edit a database on my host ^_^"
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
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(0, 3);
switch($random){
case 0:
$r[] = $abc[rand(0, count($abc)-1)];
break;
case 1:
$r[] = $abc_i[rand(0, count($abc_i)-1)];
break;
case 2:
$r[] = $num[rand(0, count($num)-1)];
break;
case 3:
$r[] = $num[rand(0, count($num)-1)];
break;
}
}
return implode($r);
}
echo generate_code();
?>
While it might return the same code twice - it is very unlikely.
dkblackhawk
01-18-2010, 12:41 AM
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?
Aww man! What kind of host are you using?
dkblackhawk
01-18-2010, 02:14 AM
Aww man! What kind of host are you using?
Dreamhost, they have their own custom control panel so it makes it hard to do things.
Dreamhost has mysql databases...
dkblackhawk
01-18-2010, 02:34 AM
Dreamhost has mysql databases...
Yes but you can't physically edit them. At least from what I found out.
djr33
01-18-2010, 02:39 AM
Permissions can be limited. For example, my host does not allow adding new databases outside of their own control panel.
However, no host limits mysql usage to the point where you cannot modify tables-- it would be pointless.
There may be no default editor setup, but you could try to install one (like phpmyadmin), or just use text commands.
In your PHP, you can use commands like mysql_query("CREATE TABLE ...") and go from there.
mysql_connect("host", "user", "pass") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
mysql_query("CREATE TABLE IF NOT EXISTS `code` (
`codes` text NOT NULL,
`used` tinyint(1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1") or die(mysql_error());
dkblackhawk
01-18-2010, 03:08 AM
mysql_connect("host", "user", "pass") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
mysql_query("CREATE TABLE IF NOT EXISTS `code` (
`codes` text NOT NULL,
`used` tinyint(1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1") or die(mysql_error());
Wow...I really need some sleep ^_^" thanks for your help :) , will try this tomorrow.
Did it happen to work out for you?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.