dtt5830659 2014-09-24 20:43
浏览 8

是否有一个很好的实践重定向PHP,允许显示<BODY>内容?

I've read somewhere that using a META refresh for a redirect, is not a recommended practice.

As far as I understand header( "refresh:0;url=$url" ); in PHP, and <meta http-equiv="refresh" content="0;url=$url/" />as a META tag in HTML, are both considered a META refresh which would return this header:

HTTP/1.x 200 OK
...
Refresh: 0;url=$url

Assuming I don't want to use a JS redirect, would it be possible to use another recommended practice redirect such as 301 or 302, while still running the following elements in the redirect page, such as the following message and a certain script?

<?php
$url = "could-not-get-a-foo-parameter-at-all-error-monitoring-page.html"; 

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

    switch ($_POST['foo']) {

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

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

    default:
        $url = "foo-parameter-exists-but-is-wrong-error-monitoring-page.html";
        break;
    }
}

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

?>  
<!doctype html>
<html>
<head>

<style>
.message {
   color:blue;
}
</style>
</head>
<body>
<div class="message">Redirecting, Please hold on...</div>
<script>
something
</script>
  • 写回答

4条回答 默认 最新

  • duan6832168 2014-09-24 20:46
    关注

    You can't invoke PHP once the page has rendered (no more PHP until another server request), so any form of delayed redirect is impossible that way. You're stuck with JS or the meta header refresh (obviously, you can't use PHP variables in pure HTML and you need more than 0 seconds if you actually want to keep the page up for a while before redirecting).

    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析