Home > Software engineering >  PHP: Redirect every user of a specific page to an another page when one of them click on a button
PHP: Redirect every user of a specific page to an another page when one of them click on a button

Time:03-14

I know the title seem to be confusing but let me explain. My plan is to make a treasure hunting creator app where you can test the other hunting from creator. I want to create a page that know how many user are in this page, every spot of the treasure hunting is a page and you can play the hunting game with your friends so when there is a group on a same spot (same page), if one user complete the riddle then it will redirect all the user to the next spot (it will redirect all the user of the same page to an another page)

I hope i was more clear about what I want, the things i couldn't put the good word to describe what I was looking for while searching for solution.

Do you have any idea how to start this type of application?

CodePudding user response:

I'm going to write psuedo code here and I hope you can figure the rest out yourself!

Alright, so if you want everyone to be redirected from your page, one terrible, inefficient option is going to be adding a database. A PHP Mysql database. Once you have one and it's all connected, add a table called Clues. For each entry into the table Clues, add some information like "Hint num 1" or "Clue num 3" then add a column that has some boolean operator.

Then what you can do it, when you click the button, it sends a PHP POST request (with a form, method="Post" action="your backend php file") which switches the database entry for Clues from a 1 to a 0 or vice versa. Then depending on which page they are on you make a loop that checks the database every 1-5seconds (or less). Then redirects you if the boolean is 1.

This is a terrible idea, use sockets in php, or another language alltogether. But if you are crazy, use that method.

CodePudding user response:

Server Sent Event seems is one of your solution, it is simple and match your requirement. It is something like a websocket but configless.

https://www.w3schools.com/html/html5_serversentevents.asp

Lets me explain in short.

  1. a game.htm will be loaded by players (2 or more players)
  2. game.htm will access gameServer.php using SSE
  3. a player complete the game, send FORM POST to gameEnd.php
  4. gameEnd.php will update the variable to gameServer.php
  5. gameServer.php read the variable, while the game was ended, send response to game.htm via SSE
  • Related