The simplest way that I would do this is to use a session variable and a single point of entry to the code.
If a single index.php file takes the user through the code and displays the output as needed, then I would simply use a variable within the session object to redirect the user to the same page until it was in a different state.
Something in your code like:
if ($_SESSION['in_combat'])
{
// redirect user back to the same page
}
else
{
// allow other action as requested by link clicked
}
Should do the trick, if the session is set to true
then any action will redirect the user back to the page that needs their attention, otherwise, it will allow them to perform any action based on a link that they click.
Edit: By single point of entry, I mean that the code ALL functions off a single index.php
page which then controls the further flow of the execution of the code. That index page will check for the various session variables, interpret and execute the various POST
, GET
requests and the like. A single point of entry is the best thing you can do when writing code that ensures your user is only able to perform the actions that they are supposed to perform.
Edit 2: First off, the single Point of Entry is a way to ensure that the PHP executes as you need it to - which means that ALL your HTML is displayed via a single PHP code. I normally do this by making a base index.php page that then directs the flow of execution depending on variables passed to it - whether they are in a session or post/get requests.
While it may not always be the most efficient way of doing it, I generally have a case statement that controls what is both displayed and executed depending on variables passed to the code.
If you already have a MVC set up, then this is the obvious place to control the execution of the page that is displayed to the user - this is obviously the index.php page itself.
As for the $_SESSION
variable, this is now easy, put the control flow into the MVC while at the same time have the code that triggers it embedded within the execution path of your program. By that I mean something like this control flow:
if($_SESSION['inCombat'])
{
// This is where you redirect the user to code that ensures that the user cannot get out of the combat sequence if they are in it already.
}
else
{
// Normal Flow of execution...
}
if($hasInitiatedCombat)
// or something like the following:
// if($_REQUEST['startCombat']==true)
{
// This is where the code execution path leads to when combat is initiated.
$_SESSION['inCombat']==true;
}
With this simple code, whenever the person enters combat (or whatever) a session variable is set to TRUE
then any link of page that they request checks this variable to determine what code the execute. If it is set to TRUE
then the code automatically executes to ensure that they are still within the combat phase, rather than allowing them to simply click on a link to exit the current code execution path and go elsewhere.