PHP试图通过编写JavaScript代码重定向url


PHP Trying to redirect url by writing JavaScript code

当我有时

<script type="text/javascript"> <!-- window.location = "newthread.html" //--> </script>

重定向工作正常,我希望它由php代码控制,但当我尝试时

<?php print('<script type="text/javascript"> <!-- window.location = "newthread.html" //--> </script>'); ?>

它不起作用。

使用php头指令。在将任何内容输出到页面之前,此指令必须发生在脚本的最顶部。

<?php
header("Location: newthread.html");
exit;
?>

对于服务器的重定向,在将其他内容返回给用户之前,您需要返回重定向头(而不是用于重定向的客户端脚本)

请参阅http://php.net/manual/en/function.header.php

代码:

<? header('Location: newthread.html'); ?>
  $pagename="foo.php?bar=123";
  echo("<script type='"text/javascript'"> 'n <!-- 'n top.location.href='$pagename'; 'n //--> 'n </script>");
  exit();

header 相反,这将在输出某些内容后工作

为什么投了反对票?