doufuhuang6981 2014-09-23 12:04
浏览 43

一旦PHP标头重定向发生意外延迟,创建一个<a href>元素?

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>
  • 写回答

1条回答 默认 最新

  • dongxing8766 2014-09-23 12:20
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭