开发者

How do I change to a different website using html or php logic?

I have spent WAY too much time searching for this. I know it must be a simple so开发者_Go百科lution so I must be not thinking clearly. here is what I want: I have some html code and php code in php file. I perform some logic in php. Based upon this logic, I wish to send the user to a different web page (url). I tried the "Redirect", but all I get is an error message about Headers already being sent. is there a simple solution in HTML or PHP? I also tried looking at the Javascript solution but that needs to be in the header, and this code is in the body. Thanks for the help. I searched like mad and couldn't find this answer in any OPEN discussions. John


Your problem is that the PHP code is embedded in your HTML code. When the server reads your HTML code, it begins to send that output to the output buffer. As such, any PHP code that wants to re-direct to a completely new page must come BEFORE any HTML code, or be in a completely separate file, as the buffers cannot be modified once they are already sent.

The easy answer: just make sure your PHP code that does the header() redirect function comes before any HTML code in your file, or put this code in a separate file.


Make sure that header is before any output

Correct

    <?php
     if ( some condition ){
        header('Location: http://www.test.com/');
        exit;
     }
    ?> 
<htmL>

Incorrect will give error

<htmL>
<?php
 header('Location: http://www.test.com/');
?>


In PHP, headers need to be sent before any HTML is displayed. Probably the easiest way to do that is to submit a form:

<?php
// Since this is before the DOCTYPE, you can still send headers.
if (isset($_POST['foo'])) {
    header('location: new-url.html');
}
?>
<!DOCTYPE html>
<html>
<!-- rest of the page goes here -->


Chances are you have HTML content before your PHP:

<span>This is HTML!</span>
<?php header("Location: http://www.example.com"); ?>

This won't work because you can't use the header function after having sent output. The HTML code counts as output so the example is the same as:

<?php
    echo "<span>This is HTML!</span>"
    header("Location: http://www.example.com"); // FAIL
?>

... here the output is very clearly before the execution of header, so it won't work.

It won't work because of the way HTTP responses are structured: headers have to be sent before the response body.

Under the hood, the output of a PHP script is a HTTP response:

HTTP/1.1 200 OK
Content-Type:text/html

<span>This is HTML!</span>

Usually the first line and the headers are added to the response implicitly and your PHP code only outputs the body, but when you use the header function you're adding headers, which have to come before the response body.

This means you have to call header before you output any of the response body:

<?php
    header("Location: http://www.example.com");
    echo "<span>This is HTML!</span>"
?>

... resulting in something like:

HTTP/1.1 303 See Other
Content-Type:text/html
Location: http://www.example.com

<span>This is HTML!</span>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜