Jump to content

Php mysql random link generate for a prize win


PNewCode
Go to solution Solved by Barand,

Recommended Posts

Hello
I am looking to create a game that has a link generated upon winning. The game itself is already done so I wont bother you with all of that.
The road block that I'm facing is looking for the right direction on making it so the winner get a link to a page where the redemption can be claimed.

To give more insight...

There is a page I made where a user can click on a button to add 5 tokens to their account
That part is also already made and works

So when the user wins the game, I want it to be so they can only use the link to that page one time, so that they can't share it with others to give away free tokens, or keep going back to claim more after one win.

Is this possible to do without emailing them a link? The website is very strict about not using email. But there are already user logins and such.

Edit: I should also say that I haven't tried anything yet because all of my searches are for either sending an email or how to generate a new link token manually so I don't know exactly what I need to do. This has to be generated automatically.

Edited by PNewCode
Link to comment
Share on other sites

Perhaps maintain log tables of winners (userID and timestamo) and also log when they claimed their reward.

On the page where they collect the reward you would first check if they are allowed to be there (winner and not collected) and, if not, redirect them elsewhere.

Link to comment
Share on other sites

16 minutes ago, Barand said:

Perhaps maintain log tables of winners (userID and timestamo) and also log when they claimed their reward.

On the page where they collect the reward you would first check if they are allowed to be there (winner and not collected) and, if not, redirect them elsewhere.

I like this approach. I could even have it so when they click the redemption, then it enters a "claimed" in the database, and then the page can check for that.
So that brings me to a different issue then. |
Lets say user "bob" played the game, won the game, and redeemed the prize.
Then Bob wants to play again... 
I would either have to create multiple win pages manually OR make it clear the "claimed" entry to start again.
The latter would make more sense and would be easy enough to do. But that would also make it so multiple people could just get the final link to the prize and redeem it

You did put me in the right direction. Thank you @Barand I have a starting point now

Edit: I think... THINK... also maybe have an entry in the database that matches the users ID to per-say a column names "status" and that can have the value entered in it when the user begins the game that says "Started" and then would allow them to finishe the game, and also a secondary that says "completed". If those values are blank, then the redemption page can check to see for them, and redirect if they are blank..
What are your thoughts on that?

Edited by PNewCode
Link to comment
Share on other sites

  • Solution

I had in mind something like this...

CREATE TABLE `win_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `won` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `collected` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_win_log_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Bob (user_id #123) wins on Saturday. He also wins on Sunday.

Each time he wins, you record the win with this query (inserting $_SESSION['user_id'] into the table)

INSERT INTO win_log (user_id) VALUES (?);

So now you have

TABLE: win_log
+----+---------+---------------------+-----------+
| id | user_id | won                 | collected |
+----+---------+---------------------+-----------+
|  1 |     123 | 2024-01-20 18:53:21 | NULL      |
|  2 |     123 | 2024-01-21 10:33:45 | NULL      |
+----+---------+---------------------+-----------+

On Monday he goes to collect his tokens. You check he is allowed to by...

SELECT COUNT(*) as tot
FROM win_log
WHERE user_id = ?
      AND collected IS NULL; 

If the returned count is > 0 he can go ahead and collect.

You record the collection with...

UPDATE win_log
SET collected = NOW()
WHERE user_id = ?
      AND collected IS NULL
ORDER BY won
LIMIT 1;

You now have

+----+---------+---------------------+---------------------+
| id | user_id | won                 | collected           |
+----+---------+---------------------+---------------------+
|  1 |     123 | 2024-01-20 18:53:21 | 2024-01-22 19:01:25 |
|  2 |     123 | 2024-01-21 10:33:45 | NULL                |
+----+---------+---------------------+---------------------+

 

Link to comment
Share on other sites

@Barand Thats perfect. Thank you very much!!!

So I have an added question to this, I'm not sure if I should have a different thread though so if I should have asked a new question then I'm sorry.
The game itself is just a random number game. I got this from a different page an I didn't create it. What I would like to do is have it so the form is gone, and the links appear if they got it right or wrong. This way they only play once on the same page.

Right now, this will work correctly for the game itself, and show the results at the bottom.
The task is to REPLACE the game with the results showing, instead of just undernieth it. Can't be a different page or redirect because the results will need to show first, and the form for the game will have to be removed.

I have tried several scripts to just disable the button after the click with javascript but it wont send the form when I do that


 

<?php

$number= $_POST['number_entered'];

$submitbutton= $_POST['submit'];


$randomnumber= mt_rand(1,5);


?>



<?php 

echo '
<form  id="id" action="" method="POST">
Guess a Number Between 1 and 5: 
<input type="text" name="number_entered" value=""/> <br><br>

';


echo '
<br><br>
<input type="submit" name="submit" value="Enter Guess" /><br><br>
</form>
';

if ($submitbutton){

if (($number > 0) && ($number <6)){
if ($number != $randomnumber)
{
echo "Incorrect guess. The correct number was $randomnumber. <a href='play.php'>TRY AGAIN</a>";
}
else 
{
echo "$randomnumber is the correct guess. <a href='Some Link To Redeem'>Click Here To Redeem Winning Tokens</a>";
}
}

}

?>

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.