rockyraw
rockyraw

Reputation: 1145

creating a <a href> element once an unexpected delay in PHP header redirection occurs?

I have a page with a form in the POST method, in which the user posts a z value, to the following PHP page that process it, and redirect the user accordingly:

<?php
{

if(isset($_POST['z'])){

    switch ($_POST['z']) {

    case "a":
        $url = "http://www.a.com";
        break;

    case "b":
        $url = "http://www.b.com";
        break;

    default:
        $url = "http://www.default.com/";
    }
}

header( "refresh:0;url=$url" );

}
?> 
<!doctype html>
<html>
<head>
<style>
.wrap{
    margin-top: 25%;
}
</style>
</head>
<body>
<div class="wrap">
You are now being redirected.
</div>
</body>
</html>

Usually this seems to work ok, but today I have received a complaint from a certain visitor that said that the message "You are now being redirected." kept showing and he was never redirected.

I Saw on the server logs that he tried several times to do this (say him posting the form at least 3 times).

  1. What could have caused this issue? (the code in the form haven't change).

  2. To try to handle such cases, I thought about giving the user the option to click on a a href link to $url, but I want this link to show up only after 5 seconds (meaning in general, most users would never see the link, as the header refresh should occur in 0 seconds). yet I want ALL users to see the message "you are being redirected".

a. I am not sure this would necessarily solve the problem, since if the problem origin was that the $url value was empty, such a link would not help.

b. I don't want the user to be able to see the link destination by viewing the page source, unless he fits the 5-seconds delay category. therefore I don't think using Javascript suits here, because it's client side, right? So I'm looking for a PHP server side solution - that would load the page with everything as it is now, but would also add a a href element after 5 seconds. Is there anyway to do this?

EDIT: here's the form:

  <form action="redirect.php" method="POST" target="_blank">
  <input name="z" type="hidden" value="a"/>
  <input type="submit" value="Go"></form>

Upvotes: 0

Views: 120

Answers (1)

Barmar
Barmar

Reputation: 780889

Assign the default value before the if instead of in the default: case:

$url = "http://www.default.com/";
if(isset($_POST['z'])){

    switch ($_POST['z']) {

    case "a":
        $url = "http://www.a.com";
        break;

    case "b":
        $url = "http://www.b.com";
        break;
    }
}

This way, if $_POST['z'] isn't set for some reason, they'll still get the default redirection.

Upvotes: 1

Related Questions