【问题标题】:Loading page in php用php加载页面
【发布时间】:2014-02-24 07:57:47
【问题描述】:

我正在开发一个航班预订网站。我必须先用搜索创建一个加载页面,3 秒后我想将请求重定向到另一个 php 页面。

我试过的代码:

<?php
    ob_start();
    $date = new DateTime($_REQUEST['departuredate']);
    if($_REQUEST['type'] == "ROUND"){
        $adate = new DateTime($_REQUEST['arrivaldate']);
    }
    date_default_timezone_set('Asia/Kolkata');

    $dcn = explode("-", $_REQUEST['departure']);
    $acn = explode("-", $_REQUEST['arrival']);

    $departure = substr(strstr($_REQUEST['departure'],'['), 1, 3);
    $arrival = substr(strstr($_REQUEST['arrival'],'['), 1, 3);
?>
<html>
    <head>
        <?php
            if($_REQUEST['type'] == "ONE"){
                echo "<title>".trim($dcn[1])." &#8594; ".trim($acn[1])." on ".date_format($date, 'D d, M Y')."</title>";
            }else if($_REQUEST['type'] == "ROUND"){
                echo "<title>".trim($dcn[1])." &#8594; ".trim($acn[1])." &#8594 ".trim($dcn[1])." on ".date_format($adate, 'D d, M Y')."</title>";
            }
        ?>
        <style>
            body{
                text-align:center;
                font-family:'WOL_Reg','Segoe UI',Tahoma,Helvetica,sans-serif;
            }
            img{
                border:0 none;
                vertical-align:middle;
            }
            table{
                border:1px solid #bcbcbc;
                border-radius:3px;
                box-shadow:0px 0px 3px inset #bcbcbc;
                text-align:center;
            }
            .nomargin{
                margin:0px;
                padding:0px;
            }
            .first{
                background-color:#bcbcbc;
                color:#fff;
                font-weight:600;
                text-align:left;
            }
            td{
                text-align:left;
                border-right:1px solid #bcbcbc;
                font-size:12px;
            }
            .reasons2{
                font-size:12px;
                margin:50px 0;
                text-align:center;
            }
            .reasons2 span{
                color:#F68500;
                font-weight:600;
            }
        </style>
    </head>
    <body>
        <p class='nomargin'><img src='images/logo.gif' alt='Geeglobia' /></p>
        <p><img src='images/loading.gif' alt='Loading...' /></p>
        <p>Loading.... Please do not close this window</p>
        <table cellspacing='0' cellpadding='5' width='30%' border='0' align='center'>
            <tr class='first'>
                <td>From</td>
                <td>To</td>
                <td>Date</td>
            </tr>
            <tr>
                <td><?php echo trim($dcn[1])." [".$departure."]"; ?></td>
                <td><?php echo trim($acn[1])." [".$arrival."]"; ?></td>
                <td><?php echo date_format($date, 'D d, M Y'); ?></td>
            </tr>
            <?php if($_REQUEST['type'] == "ROUND"){ ?>
            <tr>
                <td><?php echo trim($acn[1])." [".$arrival."]"; ?></td>
                <td><?php echo trim($dcn[1])." [".$departure."]"; ?></td>
                <td><?php echo date_format($adate, 'D d, M Y'); ?></td>
            </tr>
            <?php } ?>
        </table>
        <p class='reasons2'><span>Reasons to book with us ? &nbsp; &nbsp; </span><img alt='Tick' src='images/tick.jpg'> Lowest price promise &nbsp; &nbsp; <img alt='Tick' src='images/tick.jpg'> Expert travel advise &nbsp; &nbsp; <img alt='Tick' src='images/tick.jpg'> 24X7 travel support</p>
    </body>
</html>
<?php
    ob_end_clean();
    if($_REQUEST['stype'] == "INTL"){
        header("Location: isearch.php?".$_SERVER['QUERY_STRING']);
    }else{
        header("Location: search.php?".$_SERVER['QUERY_STRING']);
    }
    ob_end_flush();
?>

但在我的情况下似乎不起作用。立即重定向而不显示任何内容。有什么想法吗?

【问题讨论】:

    标签: php buffer wait ob-start


    【解决方案1】:

    您可以使用元标记重定向:

    html: <meta http-equiv="refresh" content="3; URL=/urlgoeshere.url">
    

    php: echo "<meta http-equiv=\"refresh\" content=\"3; URL=/urlgoeshere.url\">";
    

    您可以在浏览器正文中放置元刷新,它仍然会刷新网站(这可能被认为是糟糕的 html 编码) content=" 后面的数字定义了重定向前要等待的秒数。 在你的情况下:

    if($_REQUEST['stype'] == "INTL"){
    echo "<meta http-equiv=\"refresh\" content=\"3; URL=/isearch.php?".$_SERVER['QUERY_STRING']."\">";
    }else{
    echo "<meta http-equiv=\"refresh\" content=\"3; URL=/search.php?".$_SERVER['QUERY_STRING']."\">";
    }
    

    注意:在 page.php 中设置 URL=foo.bar 之类的 URL 将重定向到 page.phpfoobar,因此您可能需要使用 / 或 .前缀

    希望对你有帮助:)

    【讨论】:

    • 有效!但是我们不能用php来做吗?
    • PHP 手册中的一句话:请记住,header() 必须在发送任何实际输出之前调用,无论是通过普通 HTML 标记、文件中的空白行还是来自 PHP 。我猜 x 秒后的 php 重定向效果不太好,但您可以使用 javascript:&lt;script type="text/javascript"&gt; setTimeout("window.location='url'",3000); &lt;/script&gt; 时间以毫秒为单位设置(3000 = 3 秒)
    猜你喜欢
    • 2014-08-09
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    相关资源
    最近更新 更多