简体   繁体   中英

PHP headers issue on server

I recently moved from godaddy to letshost.ie for my web hosting. However, none of my scripts would work on the new letshost server.

So I did some tinkering and here's what I found:

When a php header function is used in the same file as a php include, an error is thrown. The header function to redirect will only work if there are no includes in the script.

The following script:

<?php
     include ("div.php");
     header("Location: redirect.php");
?>

can be viewed here . The error I get is

Warning: Cannot modify header information - headers already sent by (output started at /home/michae28/public_html/header/div.php:2) in /home/michae28/public_html/header/withinclude.php on line 3

The following script, attempts to perform the same redirect with the header function except it does't have a php include. It works fine.

<?php

    header("Location: redirect.php");

?>

This can be seen here.

The lets host support team have been unable to help me. One engineer suggested there may be something wrong with my .htaccess file. I haven't changed my .htaccess file at all so I don't know how this could be causing any problems.

Here it is incase anyone can see any problems:

# -FrontPage-

IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName michaeloneill.ie
AuthUserFile /home/michae28/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/michae28/public_html/_vti_pvt/service.grp

No whitespace is at the end of my file.

Any help appreciated.

You cannot set a header() call if you have already output something.

As seen on you error ( headers already sent by (output started at /home/michae28/public_html/header/div.php:2) ) check line 2 of div.php, that lines prints something

This error commonly occurs when output is sent to the browser before a redirect. Check your include file 'div.php' and see if there are any echo statements.

Header statements have to be used before any html code.

Source: http://pl1.php.net/manual/en/function.header.php

In your code, it sends output This is included. No Redirect. to browser and after that you are using header() to redirect. So it will not work . You'l have to change order or header(); and include .

header("Location: redirect.php");
include ("div.php");

But it will redirect always to redirect.php

What you're describing is simple PHP. Normal output is unbufferered. It is sent to the client as soon as it is generated. In that vein, you can't send another response code to the client after you've started on your 200's content.

The fix is simple enough. Buffer your output if you think you need to change direction halfway through the script.

<?php
    ob_start();
    include ("div.php");
    header("Location: redirect.php");

But that's still starting a buffer and partially filling it. It's bad logic if you can get halfway through the output before you realise you actually need to redirect. It's a waste of CPU. The best method is to move all the logic that could dictate a redirect to before any content is generated.

A decent MVC (or other) framework will handle the business logic before it starts on presentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM