Question

I have tried to implement the POST/REDIRECT/GET design pattern in PHP and it works with Safari but not Google Chrome (for Mac) when I redirect to the exact same page. If I vary the URL slightly (say adding a trailing-slash) it works fine.

Here is a fully functional example:

<?php
if(TRUE === isset($_POST['submit'])) {
    header('Location: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'], true, 303);
    exit();
}?>
<html><head></head><body>
<form action="" method="post"><input type="submit" name="submit" value="submit" /></form>
</body></html>

When I go to the page and click the submit button and then refresh the page, I get a dialog box asking me to confirm the re-submission of the form data. All my searching has said that the above code is the proper way to implement POST/REDIRECT/GET and should work (and it does in Safari) but it doesn't with Chrome on Mac.

Was it helpful?

Solution

Your code seems okay, I guess what you're experiencing is related to Issue 177855: Wrong handling of Redirection in Chrome 25

OTHER TIPS

Refreshing a page you posted to gives you that popup box because the browser wants to know if it should resend the data that got you to that page originally. That will happen with all forms. You say it doesn't work in chrome. Do you get an error? Does it just not redirect?

Also, It would make more sense to use the anchor tag to link someone to a new page directly, or to use a roundabout way such as:

<a href="http://<?php echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] ?>?act=forward">Forward</a>

And to check for $_GET["act"] to be equal to "forward" opposed to checking if the form has been sent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top