如何在PHP中重定向

时间:2014-04-26 16:46:19

标签: php redirect

文件redirect.php包含以下代码:

   <?php

$url  = $_SERVER["PHP_SELF"];
$path = explode("?q=", $url); 
$last = end($path);
header('Location: http://mywebsite2.com/something&q='.$last);
?>

但是,当我转到http://mywebsite.com/redirect.php?q=anything时,会显示错误:

Cannot modify header information - headers already sent by".

如何解决?

3 个答案:

答案 0 :(得分:1)

header('Location: '.$yourURL);

或者您可以使用

header("Location: yourPage.php");
die();

为什么要使用die() go to this link

答案 1 :(得分:0)

在打开<?php标记之前,您似乎有一些空格。这将导致错误。

   <?php
^^^ remove

答案 2 :(得分:0)

标题必须是文档中的第一个HTML元素记住空格确实计为html元素

删除它们

header('Location: http://mywebsite2.com/something&q=anything');
die():

了解更多

PHP variable in header function

已编辑

在您的网址中添加任何内容:)简单