0

Possible Duplicate:
Headers already sent by PHP

My website's main URL redirects any visitors to the local sub-pages, and I use freegeoip.net to achieve this, along with PHP:

<!DOCTYPE html>
<html>
<head>
<title>MLP Today</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="google-site-verification" content="*snip*" />
<?php
$csv = str_getcsv(file_get_contents('http://freegeoip.net/csv/'));
$geoloc = strtolower($csv[1]);

    if ($geoloc == 'hu' || 
        $geoloc == 'ro'){
        header("Location: hu/");
    }
    else if($geoloc == 'nl' || 
            $geoloc == 'be' || 
            $geoloc == 'de' || 
            $geoloc == 'sr' || 
            $geoloc == 'au'){
        header("Location: de/");
    }
    else {
        header("Location: en/");
    }
?>
<link rel="icon" type="image/png" href="img/favicon/favicon.png">
</head>
<body>
</body>
</html>

However, when loading the site from my local Apache Server, I get an error, saying:

Warning: Cannot modify header information - headers already sent by (output started at D:\DJDavid\Dokumentumok\Dropbox\Web\Today\index.php:7) in D:\DJDavid\Dokumentumok\Dropbox\Web\Today\index.php on line 13

Line 13 is: header("Location: hu/");

I read some answers before, but all of them was referring to live websites, not Apache Web-Servers. Some said I have to remove everything before & after the PHP code, but that did not help. Also, I'm not outputting or returning anything, as you can see, while that was the second most common cause of this problem. The third suggested was, to use ob_start(), but that was no help either.

The thing is, it works perfectly on the web host, but not when I'm using localhost (the local server) to preview changes before going live.

I'm guessing I have to change something in httpd.conf but I don't know what.

Community
  • 1
  • 1
SeinopSys
  • 8,787
  • 10
  • 62
  • 110
  • Yes you are outputting something. When your first line (doctype) is sent, you have started outputting something. After that, you **cannot send headers**. – cmbuckley Jan 26 '13 at 12:21

1 Answers1

0

If your server start sending HTTP 200 OK response and conent like:

<!DOCTYPE html>
<html>
<head>
<title>MLP Today</title>...

The you can't change respnse to 3xx Redirect. Try to move your redirecting logic before sending any content.

Other way is to turn on buffering output instead of sending it as fast as you can by: ob_start()

Also this can be a BOM problem (three bytes added by Windows Notepad at begining of a file)

Piotr Müller
  • 5,323
  • 5
  • 55
  • 82
  • `ob_start()` - Forgot to include this to the list of things suggested & didn't work. I do get a *HTTP 200 OK* by the way. – SeinopSys Jan 26 '13 at 12:21