Jump to content

Recommended Posts

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!!

Link to comment
https://forums.phpfreaks.com/topic/298870-a-refreshing-question/
Share on other sites

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 by mac_gyver

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.

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.

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

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 :P

 

cheers!

thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.