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

时间:2013-02-20 15:09:32

标签: php javascript

当我有

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

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

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

它不起作用。

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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

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

代码:

<? header('Location: newthread.html'); ?>

答案 2 :(得分:-1)

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

header

相比,输出后会显示

为什么选票下来?