• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[Znote AAC] Redeem a coupon system

Cornex

Web Developer
Staff member
Global Moderator
Joined
Jun 15, 2008
Messages
3,444
Solutions
5
Reaction score
1,166
Location
Sweden
Hello, this is something that i putted togheter very fast.
The layout ain't the best, but it works.

Admin can generate key with an amount of points that a use can redeem.
Can be funny for ingame events or whatever you find this useful for.

Hope someone find it useful :)

Start of with run this in your database:
How to run a query?

Code:
CREATE TABLE IF NOT EXISTS `__cornex_redeem` (
`id` int(11) NOT NULL,
  `points` int(11) NOT NULL,
  `used_by` int(11) NOT NULL DEFAULT '0',
  `time` int(11) NOT NULL,
  `ip` int(11) NOT NULL,
  `code` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `__cornex_redeem`
ADD PRIMARY KEY (`id`);

ALTER TABLE `__cornex_redeem`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

Now, download the website files.
Your job is to know where to place them, should not be to hard? ;)

Download: https://www.dropbox.com/s/wtwjmj92qavkra1/redeem.rar?dl=0

Warning, i just put some input fields there, therefor it looks just weird. Do whatever u like with it.

RiS6LMwdU.png


nUWA4wWm8.png
 
Last edited by a moderator:
Good job Cornex, can you help me to convert it to gesior acc? i made some changes but wont work xD

Code:
<?php 
    if (isset($_POST['code'])) {

        $code = mysql_real_escape_string($_POST['code']);
        $res = $SQL->query("SELECT * FROM `__cornex_redeem` WHERE `code` = '${code}' LIMIT 1;")->fetchAll();

        // Key exist in database
        if ($res > 0) 
        {
            $res = $res[0];

            if ($res['used_by'] != 0) {
                $error[] = 'Key has already been used';
            }

            if (!empty($error)) {

                foreach ($error as $err) {
                    $main_content .= $err.'<br>';
                }

            }

            if (empty($error)) {

                $used_by = $account_logged->getName();
                $ip = ip2long(Visitor::getIP();
                $time = time();
                $code = $res['code'];
                $points = $res['points'];
                $accid = $account_logged->getAccountID();
                $SQL->query("UPDATE `__cornex_redeem` SET `used_by` = $used_by , `time` = $time , `ip` = $ip WHERE `code` = '$code'") or die mysql_error();
                $SQL->query("UPDATE `accounts` SET `premium_points` = `premium_points` + $points WHERE `account_id` = $accid LIMIT 1") or die mysql_error();
                $main_content .= $points.' points has been added to your account!';

            }

        } else {
            $main_content .= 'Key is not valid';
        }
    }
?>

<form action="" method="POST">
   
    <input type="text" name="code">
    <input type="submit" value="Redeem">

</form>
 
Good job Cornex, can you help me to convert it to gesior acc? i made some changes but wont work xD

Code:
<?php
    if (isset($_POST['code'])) {

        $code = mysql_real_escape_string($_POST['code']);
        $res = $SQL->query("SELECT * FROM `__cornex_redeem` WHERE `code` = '${code}' LIMIT 1;")->fetchAll();

        // Key exist in database
        if ($res > 0)
        {
            $res = $res[0];

            if ($res['used_by'] != 0) {
                $error[] = 'Key has already been used';
            }

            if (!empty($error)) {

                foreach ($error as $err) {
                    $main_content .= $err.'<br>';
                }

            }

            if (empty($error)) {

                $used_by = $account_logged->getName();
                $ip = ip2long(Visitor::getIP();
                $time = time();
                $code = $res['code'];
                $points = $res['points'];
                $accid = $account_logged->getAccountID();
                $SQL->query("UPDATE `__cornex_redeem` SET `used_by` = $used_by , `time` = $time , `ip` = $ip WHERE `code` = '$code'") or die mysql_error();
                $SQL->query("UPDATE `accounts` SET `premium_points` = `premium_points` + $points WHERE `account_id` = $accid LIMIT 1") or die mysql_error();
                $main_content .= $points.' points has been added to your account!';

            }

        } else {
            $main_content .= 'Key is not valid';
        }
    }
?>

<form action="" method="POST">
  
    <input type="text" name="code">
    <input type="submit" value="Redeem">

</form>

I gonna do a gesior version once I find time for it, if no one has convert it before.
 
Good job Cornex, can you help me to convert it to gesior acc? i made some changes but wont work xD

Code:
<?php
    if (isset($_POST['code'])) {

        $code = mysql_real_escape_string($_POST['code']);
        $res = $SQL->query("SELECT * FROM `__cornex_redeem` WHERE `code` = '${code}' LIMIT 1;")->fetchAll();

        // Key exist in database
        if ($res > 0)
        {
            $res = $res[0];

            if ($res['used_by'] != 0) {
                $error[] = 'Key has already been used';
            }

            if (!empty($error)) {

                foreach ($error as $err) {
                    $main_content .= $err.'<br>';
                }

            }

            if (empty($error)) {

                $used_by = $account_logged->getName();
                $ip = ip2long(Visitor::getIP();
                $time = time();
                $code = $res['code'];
                $points = $res['points'];
                $accid = $account_logged->getAccountID();
                $SQL->query("UPDATE `__cornex_redeem` SET `used_by` = $used_by , `time` = $time , `ip` = $ip WHERE `code` = '$code'") or die mysql_error();
                $SQL->query("UPDATE `accounts` SET `premium_points` = `premium_points` + $points WHERE `account_id` = $accid LIMIT 1") or die mysql_error();
                $main_content .= $points.' points has been added to your account!';

            }

        } else {
            $main_content .= 'Key is not valid';
        }
    }
?>

<form action="" method="POST">
  
    <input type="text" name="code">
    <input type="submit" value="Redeem">

</form>
You shouldn't change things you don't understand, like this line
Code:
// Key exist in database
if ($res > 0)

Even if I don't know what $res is, I know it doesn't hold a number.

Since your using an object called SQL to point a method that is self described to return more than one value even if 0 values are return res's value will never be greater than 0, because it is an array and not a variable.

Since it is an array you could compare its size to 0, at least than you would know if it held a value
Code:
if(sizeof($res) > 0)

Curious about this line, is $res a multi-dimensional array?
Code:
$res = $res[0];
 
This would be a better solution
Code:
// Key exist in database
if( isset($res[0]) ){     
    $res = $res[0];
}
 
Code:
string(126) "UPDATE `__cornex_redeem` SET `used_by` = 147501 , `time` = 1427654195 , `ip` = WHERE `code` = 'UN21-FP29-55PS-47TJ-DXQO-45TK'"
(query - SQL error)
Type: voidQuery (voidQuery is used for update, insert or delete from database)

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `code` = 'UN21-FP29-55PS-47TJ-DXQO-45TK'' at line 1

I got this error :/
 
Code:
string(126) "UPDATE `__cornex_redeem` SET `used_by` = 147501 , `time` = 1427654195 , `ip` = WHERE `code` = 'UN21-FP29-55PS-47TJ-DXQO-45TK'"
(query - SQL error)
Type: voidQuery (voidQuery is used for update, insert or delete from database)

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `code` = 'UN21-FP29-55PS-47TJ-DXQO-45TK'' at line 1

I got this error :/
@Cornex
 
Anyone still working on the Gesior version of this script? would be extremly cool to use :3.
 
Tested on Znote ACC 1.5, results:

Everything works fine until i get to redeem my code and then the same error as @up

Code:
string(124) "UPDATE `__cornex_redeem` SET `used_by` = 4300 , `time` = 1428752330 , `ip` = WHERE `code` = 'GP46-QD52-97AT-50AJ-GWDW-46LK'"
(query - SQL error)
Type: voidQuery (voidQuery is used for update, insert or delete from database)

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `code` = 'GP46-QD52-97AT-50AJ-GWDW-46LK'' at line 1
 
Tested on Znote ACC 1.5, results:

Everything works fine until i get to redeem my code and then the same error as @up

Code:
string(124) "UPDATE `__cornex_redeem` SET `used_by` = 4300 , `time` = 1428752330 , `ip` = WHERE `code` = 'GP46-QD52-97AT-50AJ-GWDW-46LK'"
(query - SQL error)
Type: voidQuery (voidQuery is used for update, insert or delete from database)

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `code` = 'GP46-QD52-97AT-50AJ-GWDW-46LK'' at line 1
me too bump!
 
I noticed that the system only works if the account that is redeeming the code only has numbers in its account name.
I simply fixed this by removing 'used_by' from some parts. and changing the check if its been used to check for a IP instead. Here it is:
Code:
<?php
    require_once 'engine/init.php';
    protect_page();
    include 'layout/overall/header.php';

    if (isset($_POST['code'])) {

        $code = mysql_znote_escape_string($_POST['code']);
        $query = "SELECT * FROM `__cornex_redeem` WHERE `code` = '${code}' LIMIT 1;";

        $res = mysql_select_multi($query);
        // Key exist in database
        if ($res !== false) {
            $res = $res[0];

            if ($res['ip'] != 0) {
                $error[] = 'Key has already been used';
            }

            if (!empty($error)) {

                foreach ($error as $err) {
                    echo $err.'<br>';
                }

            }

            if (empty($error)) {

                $used_by = mysql_znote_escape_string($user_data['name']);
                $ip = ip2long(getIP());
                $time = time();
                $code = $res['code'];
                $points = $res['points'];
                $accid = $user_data['id'];
                $query = "UPDATE `__cornex_redeem` SET `time` = $time , `ip` = $ip WHERE `code` = '$code'";
                $query2 = "UPDATE `znote_accounts` SET `points` = `points` + $points WHERE `account_id` = $accid LIMIT 1;";
                mysql_update($query);
                mysql_update($query2);
                echo $points.' points has been added to your account!';

            }

        } else {
            echo 'Key is not valid';
        }

    }

?>

<form action="" method="POST">
   
    <input type="text" name="code">
    <input type="submit" value="Redeem">

</form>
   
<?php include 'layout/overall/footer.php'; ?>

Code:
<?php
    require_once 'engine/init.php'; include 'layout/overall/header.php';
    protect_page();
    admin_only($user_data);

    // From Felony
    function serialKey()
    {
        $template = 'XX99-XX99-99XX-99XX-XXXX-99XX';
        $k = strlen($template);
        $sernum = '';
        for ($i=0; $i<$k; $i++)
        {
            switch($template[$i])
            {
                case 'X': $sernum .= chr(rand(65,90)); break;
                case '9': $sernum .= rand(0,9); break;
                case '-': $sernum .= '-';  break;
            }
        }
        return $sernum;
    }

    if (isset($_POST['points'])) {

        $error = false;
        $points = $_POST['points'];

        if (empty($points)) {
            $error[] = "<font color='red'>Field cannot be empty</font>";
        }

        if (!is_numeric($points)) {
            $error[] = "<font color='red'>Must be a number value</font>";
        }

        if (!empty($error)) {

            foreach ($error as $err) {
                echo $err.'<br>';
            }

        }

        if (empty($error)) {
            // Success
            $key = serialKey();
            $query = "INSERT INTO `__cornex_redeem` (`code`, `points`, `used_by`, `time`, `ip`) VALUES ('".$key."','".$points."', 0, 0, 0)";
            mysql_update($query);
            echo '<p><p> <b>Key generated</b><font color="red">: '.$key.'</font>';
        }

    }

?>

<form action="" method="POST">
   
    <label>Points</label>
    <input type="text" name="points"><br>
    <input type="submit" value="Create code">

</form>

<?php include 'layout/overall/footer.php'; ?>
 
Tested but it still don't works for me, getting the same error, and i tested with an account with only numbers but no changes
 
Back
Top