Cainj Posted October 27, 2015 Share Posted October 27, 2015 I would like to know the most effective way of preventing a page refresh. Writing a text game. Going well. However, browser games can get messy if you don't get the F5 under control. I know I could use a Session/Cookie variable, but not sure how to impliment that efficiently. While the game is large, it is managed very nicely within the index.php all is centraled around a "switch/case" based on url request. This method has served me well for many apps I have writen. So thought I would do a game which encompasses many apsects of programing - and one snag I ran into was the dreaded F5. The causes unlimited just about everything, if put in the right spot. I checked the web, but it seems people are asking the wrong question: "..how to disable the F5 key" - which is not correct. I will keep searching, and if I find something before a reply comes here, I'll post what I find that is successful. Thanks!! Quote Link to comment Share on other sites More sharing options...
scootstah Posted October 27, 2015 Share Posted October 27, 2015 Just forget about this idea right now. It's not going to happen. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 27, 2015 Share Posted October 27, 2015 (edited) the browser should just be displaying the current state. the control over what happens should be by the server-side code, regardless of how many times the current state is requested/displayed. what exactly is occurring when a page gets requested/refreshed that is causing a problem? are you using post method forms to submit data, with one-use-tokens to prevent double-submission, followed by a header() redirect to the exact same url that the form submitted to, to cause a get request to display the current state? Edited October 27, 2015 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Cainj Posted October 27, 2015 Author Share Posted October 27, 2015 the browser should just be displaying the current state. the control over what happens should be by the server-side code, regardless of how many times the current state is requested/displayed. what exactly is occurring when a page gets requested/refreshed that is causing a problem? are you using post method forms to submit data, with one-use-tokens to prevent double-submission, followed by a header() redirect to the exact same url that the form submitted to, to cause a get request to display the current state? Hi Mac hmmm for simplicity, I'll use the healer shop. pretty basic. not a game changer on refresh, but helps to illustrated the dilema: player pays for x amount of gold to have y amount of hp to be restored. Does this successfully. Hitting F5 will of course repeat this, and depleate their bank. But the code and the process in itself is very ugly.(imo) It is started with a <a href="?healer&heal">Heal</a> then php catches it if (isset($_GET['heal'])) { [ process healing code here So hitting refresh, the "get" is still there. This example works fine regardless if they refresh, but things like combat could become problematic. I've seen several solutions that use cookies/sessions, but then the problem arises of where to UNSET these so you can access the given location and/or activity again (example, if i leave the healer, and then come back, I do not want the session/cookie to think I refreshed the page). Those examples I have found are mostly for app of non-gaming in nature. I am sure I will arrive at something. But its gnawing at me LOL anyways. hope that helps a little. thanks for taking time to read this. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 27, 2015 Share Posted October 27, 2015 Your problem isn't the refresh, it's understanding the basics of HTTP. GET requests are not supposed to have any side effects. They are meant to get a resource (hence the name). So when you use URLs to change data, you're already doing it wrong, and it's no wonder you run into all kinds of trouble. Fix this by using POST requests and the PRG pattern, and a lot of those problems will actually go away. Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 27, 2015 Share Posted October 27, 2015 (edited) Fix this by using POST requests and the PRG pattern, Well how about that! That is what I do and didn't even know it had a name. Edited October 27, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
0x00 Posted October 28, 2015 Share Posted October 28, 2015 I don't use redirect, rather I just send them wherever... header("Location: ".my_path_self()); exit(); However, after reading Jacques link I see the beauty of using a redirect instead because of the bookmarks issue. So... function redirect($url, $statusCode=303){ header('Location: ' . $url, true, $statusCode); die(); } (* Not my function) Codes: 301: Permanent redirect 302: Temporary redirect (default used by header function I believe) 302: Other redirect Quote Link to comment Share on other sites More sharing options...
scootstah Posted October 28, 2015 Share Posted October 28, 2015 I don't use redirect, rather I just send them wherever... header("Location: ".my_path_self()); exit(); That is a redirect my friend. Quote Link to comment Share on other sites More sharing options...
0x00 Posted October 28, 2015 Share Posted October 28, 2015 lol, yeah I wrote that before reading, I've never really thought of it as a redirect but can't think of anything else I could have called it previously??? Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 28, 2015 Share Posted October 28, 2015 A bit tidier IMO die(header("Location: somepage.php")); Quote Link to comment Share on other sites More sharing options...
Cainj Posted October 28, 2015 Author Share Posted October 28, 2015 (edited) is this thread suggesting I remove the list of links (options), and substitute for a form? Edited October 28, 2015 by Cainj Quote Link to comment Share on other sites More sharing options...
0x00 Posted October 28, 2015 Share Posted October 28, 2015 is this thread suggesting I remove the list of links (options), and substitute for a form? Its the same issue either way... All you need to do is redirect after processing the input (GET or POST) Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 28, 2015 Share Posted October 28, 2015 (edited) is this thread suggesting I remove the list of links (options), and substitute for a form? Yes. A form using the POST method. Links aren't meant to change anything. Edited October 28, 2015 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Cainj Posted October 28, 2015 Author Share Posted October 28, 2015 this is sorta like watching a movie you have not seen in a long while. You watch it KNOWING you seen it before, but only remember it while your watching it. I'm super rusty. I went back and looked at my code, matching what is said here - and thought NOOB LOL at first I thought scoots post was rude - until I realized what was said - "links" are not gonna happen. der! thanks guys, I'll rewrite it, post a snippet here for future viewers, if it is still not up to par, you can rip it up cheers! thanks Quote Link to comment Share on other sites More sharing options...
scootstah Posted October 28, 2015 Share Posted October 28, 2015 at first I thought scoots post was rude - until I realized what was said - "links" are not gonna happen. der! No, it meant you are not going to prevent a browser from refreshing the page. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.