B
B
bohdaniel2014-06-21 22:42:40
PHP
bohdaniel, 2014-06-21 22:42:40

How to generate a discount coupon in PHP?

Good day!
It is necessary to generate a discount coupon in the format 123456-ABC or similar to PHP so that later it can be verified by mathematical transformations and / or various substitutions.
Using a database with keys stored there is not an option, since the project is small and the database is not used.
Super complex algorithms are not needed, something simple will be enough. Keys will be generated several times a day, the coupon is valid for 1 year.
Probably a question for those who understand cryptography.
I will be grateful for the answer!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
Z
zdiii, 2014-06-22
@bohdaniel

I did it as they wanted)
Coupon type: JFCA-4304790701739
And so, generate.php creates a coupon.

<!doctype html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>Проверка купона</title>
</head>
<body>
    <form action="generate.php" method="post">
        Купон до:
        <input type="number" max="31" style="width:5%;" placeholder="31" name="d">        <input type="number" max="12" style="width:5%;" placeholder="06" name="m">
        <input type="submit" value="Создать">
    </form>
    <br><br><br>
    <?php
if (!isset($_POST['m'])){
    } else {
                $kupon = $_POST['m'].$_POST['d'];
                $dr = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
                $drr   = array( "A", "B", "C", "D", "E", "F", "G", "H", "I", "J");
                $data = str_replace($dr, $drr, $kupon);
                $kupon2 = $kupon * 79 * 89 * 99 * 99 * 99;
                $kupon2 = "$data-$kupon2";
                echo "Ваш купон: \"<b>$kupon2</b>\"";
};
?>
</body>
</html>

save the code as generate.php .
Next, proverka.php checks the coupon for validity.
<!doctype html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>Проверка купона</title>
</head>
<body>
    <form action="proverka.php" method="post">
        Введите купон:
        <input type="text" style="width:50%;" placeholder="88e3e18656d274a70518ecdcaf01aec5" name="kupon">
        <input type="submit" value="Проверить">
    </form>
    <br><br><br>
    <?php;
if (!isset($_POST['kupon'])){
    } else {
              $kupon = substr($_POST['kupon'], 0, 4);
              $date = date("md");
              $dr = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
                $drr   = array( "A", "B", "C", "D", "E", "F", "G", "H", "I", "J");
                $data = str_replace($drr, $dr, $kupon);
                
              $kupon2 = $data * 79 * 89 * 99 * 99 * 99;
              $kupon2 = "$kupon-$kupon2";
              if ($_POST['kupon'] == $kupon2) {
                        if($data>=$date){
                                              echo "<h1>Действительный купон!</h1>";
                                              } else {
                                              echo "<h1>Недействительный купон!</h1>";
                        };
              } else {
              echo "<h1>Недействительный купон!</h1>";
              }
};
?>
</body>
</html>

save the code as proverka.php .
Upload two files to the server.
Use it) Ask questions, I will answer)))
I can modify as you wish))) Of course, for a small Yandex.money)
mail: marketing-kg @yandex .com

M
miki131, 2014-06-22
@miki131

If SQLite is not suitable, just make a JSON file with a list of codes. Something like this

[
{
  "code": "123456-ABC",
  "created_at": "22-06-2014",
  "valid": true
}
]

D
Denis Morozov, 2014-06-21
@morozovdenis

what's the difference small or big project
if you don't want a database server, then use SQLite and create a database file right there on the file where the php scripts are

B
bohdaniel, 2014-06-24
@bohdaniel

Before I realized that the option I proposed did not suit me a little, I did this ...
Maybe it will come in handy for someone ...
1) Let's say you need to generate a coupon like 314420-ABG
2) We randomly generate a number from 65 to 90 (= 65)
3) Generate the first letter from ascii (=A)
4) Subtract the letters 64 from the code (65-64=1)
5) Multiply 1x2 = 2
6) Convert this number to radians: 2*pi/180 = 0.03 (s rounding)
7) Find out the remainder of this number after the coma (=03)
8) Divide the number into two parts: 0 and 3
9) Get the first (3) and last (0) number from the numerical set
10) Multiply these numbers and add 14 ( result will always be double digit = 14)
11) We decompose this number into separate numbers 1 and 4. This will be the second (1) and fourth (4) number from the numerical set
12) Multiply the 1st, 2nd and 4th number (=12). We break into separate characters (1, 2). Let's find out which of them is more (=2). This will be our 5th number from the numerical set.
13) We randomly generate the second letter (66=B)
14) Subtract 64 = 2
15) abs(cos(2)) = 0.41
16) find out the first number after the coma (4)
17) This will be our last, 3rd number from the numerical row.
18) The last letter is also randomly generated. But it's for beauty :)
We check the code, in the same order as it was generated. If at some stage an error occurs, then the code is invalid.
The disadvantage of such coupon generation is that one coupon can be used an infinite number of times. It's like a license key.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question