Use this
Call it create.phpPHP Code:<?php
$hostname = ("localhost");
$database = ("database");
$username = ("username");
$password = ("password");
mysql_connect($hostname, $username, $password);
mysql_selectdb($database);
function clean($string)
{
// This is from KP2 - Thanks!
$string = str_replace("\"", "", $string);
$string = nl2br($string);
$string = htmlentities($string);
$words = array("UNION",
"SELECT FROM",
"ORDER BY",
"INSERT INTO",
"TRUNCATE",
"DROP TABLE",
"CREATE TABLE",
"DROP DATABASE"); // All the queries we want to stop
$string = preg_replace("/$words/i", "", $string);
return $string;
}
switch($_GET["act"])
{
default:
if(isset($_POST["credits"]))
{
if(is_numeric($_POST["credits"]))
{
for($i=0; $i<"8"; $i++)
{
$randnum = mt_rand(0,61);
if($randnum < 10)
{
$randstr .= chr($randnum+48);
}
else if($randnum < 36)
{
$randstr .= chr($randnum+55);
}
else
{
$randstr .= chr($randnum+61);
}
}
$string = strtolower($randstr);
$value = clean($_POST["credits"]);
mysql_query("INSERT INTO `credits` (`value`, `string`) VALUES ('$value', '$string')");
echo 'You have created ' . $_POST["credits"] . ' Credits! - Your code is ' . $string . ' Have fun!';
} else {
echo 'Your amount of credits is not a number';
}
}
else {
?>
Add credits (ammount)
<br />
<form action="check.php" method="POST">
<input name="credits" type="text" />
<input type="submit" value="Submit!" />
</form>
<?php
}
break;
case submit:
{
if(isset($_POST["credit"]))
{
$credits = clean($_POST["credit"]);
$sql = mysql_query("SELECT * FROM `credits` WHERE `string` = '$credits'");
if(mysql_num_rows($sql) == "1")
{
$fetch = mysql_fetch_array($sql);
$value = $fetch["value"];
echo "Correct voucher code! Also, if you wanted it to add the the users credits, add a mysql insert for the user to earn some credits, with the credit value as $value";
mysql_query("DELETE FROM `credits` WHERE `credits`.`value` = '$value' AND CONVERT(`credits`.`string` USING utf8) = '$credits'");
} else {
echo "Invalid code";
}
} else
{
echo 'Redeem credits<br />
<form action="check.php?act=submit" method="POST">
<input name="credit" type="text" />
<input type="submit" value="Submit!" />
</form>';
}
}
break;
}
?>
And for the database:
CREATE TABLE `credits` (
`value` VARCHAR( 225 ) NOT NULL ,
`string` VARCHAR( 225 ) NOT NULL
) ENGINE = MYISAM ;
It creates a credit code, then a user types in the code at create.php?act=submit then you need to add a thing which updates their credits by the ammount, then it deletes it.
Have fun (edit mysql details aswell!)
Edit: ignore my crap coding

