I have a dynamic website with pages drawn using PHP.
Using the $_GET variable I am getting commands from the user, executing PHP based on the $_GET command, and drawing a page.
However, when the user presses the back button I want them to see the page that was dynamically drawn for them before, instead of re-executing code.
I’ve seen this done, but can’t figure out how to do it.
For instance assume the following code:
if ($_GET['cmd'] == "time") {
echo "The current Unix timestamp is: " . time;
}
Clicking the url: somepage.php?cmd=time executes the code properly but when using the back button, re-executes the code. Is there a way using cache, or something else I don’t know about, that will allow the user to see the time as it was when the page was drawn, instead of re-executing?
=========================================================================================
To try and be a little more specific, the pages and code that I am talking about perform multiple functions and alter MySQL data based on the commands given then draw the page.
I want to know if there is a way, when using the back button, to not re-execute but to just show the page that was drawn dynamically the first time.
I’ not sure if this is what you are looking for, but it may be of help. Rather than using php get, you might use ajax and then implement this: http://www.nerdswithlives.com/2010/03/yui-ajax-browser-history-back-button.html
You could also have some type of variable stored every time a specific get command is executed, then check for that variable to determine which content to redraw on page load.
EDIT
From thinking more about your problem, I believe the answer lies with using PHP Sessions, and storing data on the clients machine. When a user clicks “back” he/she IS going to the cached page… so caching is not your answer. You need it re-drawn a specific way, but because you are using GET, the browser does NOT cache this… at least with back button functionality anyway. Your answer is to start a session on each page this dynamic content exists, store a variable like
$_SESSION['sessionVar'] = 1;or whatever. Then dynamically change the variable depending on what was drawn on that page. Then, when the user clicks “back” you can check for whatever that variable is and get the data again. Get out of the mindset of using cache for this – you need to RE-DRAW whatever data the user saw previously. Sessions would be useful in this case.