-4

I want to detect if a particular php script has been run or if a particular form has been submitted, if that is true don't let the user go anywhere else on the site without finishing clicking submit buttons first! If you go back and want to refresh or if you want to click something else on my site, NO, no you can't because you didn't finish with your previous task and website will just reload the previous form or result for you and you have to click form button "Quit" or header link "Return home" to continue.

Reason I wan't this is because I am simulating a simple combat game with php and I don't know how to achieve this. Maybe with javascript?

I am even providing a picture to understand me better :) I really hope someone has a php or javascript solution for this.

Picture of what I am talkig about

EDIT: before you downvote atleast provide me with some tips where to look for the solution and do the research... I am lost, don't know what to search for

Matic
  • 39
  • 1
  • 6
  • C'mon guys, downvotes are supposed to be accompanied by a comment at the very least. Have some integrity and at least tell this new user why you think this is a poorly asked question - or answer it as it stands. – Fluffeh Aug 04 '13 at 10:21

1 Answers1

0

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.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • Thank you for not ignoring my question! Though I am having a hard time understanding. I already have a MVC setup for my site, meaning I have html that displays the forms and data from database, php that does all the work in the back and another php which only handles $_SESSION variables and displays the info to the html page. This being said, where do I put single point of entry? In my controller php that displays the data to user? Also what do I put in my $_SESSION['in_combat'] variable? – Matic Aug 04 '13 at 10:31
  • Wow this will certainly take me some time to process, I will get back to you if I have a question. Thank you again, your level of patience and help is amazing! – Matic Aug 04 '13 at 10:58
  • No problem, I like to help people. Just remember, if this solves your question, Accept it as the answer, otherwise please do expand your question so that others can answer it further to the needs of your question :) – Fluffeh Aug 04 '13 at 11:09
  • Hello again @Fluffeh I did more research on MVC in order to better understand you. I also posted this question on stackoverflow: [link](http://stackoverflow.com/questions/18065482/which-page-is-which-acording-to-model-view-controller). Take a look there if you are still interested in helping me. Which page should I turn into index.php or how to include it on my pages and then initialize your above solution? – Matic Aug 05 '13 at 18:54
  • Looks like I won't be establishing any kind of MVC for my project, I do however have post-request-get pattern for my site up and running and some kind of basic MVC look a like structure. I hope I can successfully pull this off with index.php. You are basicly saying I need to put every display logic in my index php along with included html files that trigger only when needed? Or is index.php just a file that redirects users according on their choices? – Matic Aug 05 '13 at 20:58
  • also how do I set my $_SESSION variable to be false again? – Matic Aug 05 '13 at 22:00
  • The index.php page is the one that realistically takes all the data from the user (whether it is URL/POST/Session info) and then controls what the rest of the code does. Also, to set a Session to false is no different than any other variable. `$_SESSION['boogey']=false;` though if you are doing a boolen comparison like `if($_SESSION['boogey'])` then you can also simply set it to null. – Fluffeh Aug 06 '13 at 09:23