0

When i put my website online, all of the headers will not work. But when i built it, just on a local database etc. it worked "Works" fine.

I get this ERROR on the website currently:

Warning: Cannot modify header information - headers already sent by (output started at /customers/5/8/4/infuze.dk/httpd.www/index.php:22) in /customers/5/8/4/infuze.dk/httpd.www/includes/functions.php on line 16

I used my google foo a little and as far as i can understand is that i cant output any data before i use headers. But i dont thing that is the case here.

Here is the code i used in on the site.

if(empty($_GET['page'])){
    header('location: index.php?page=homepage');
}

or at least one example

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

3 Answers3

1

I usually get the error "Cannot modify header information - headers already sent by" when the page you're trying to redirect from contains information. Check to see if the script uses echo, include, require, etc., or contains HTML markup before the line at which you try to redirect.

<p>Hello!</p>
<?php
header('location: index.php?page=homepage');
?>

Will NOT work, because it sends the browser information before trying to redirect.

<?php
echo "Hi!";
header('location: index.php?page=homepage');
?>

Also won't work, because it also sends the browser information before trying to redirect.

<?php
header('location: index.php?page=homepage');
include('setup.txt');
?>
<h1>Amazing page!</h1>

WILL work, because it tries to send the browser information AFTER the redirect, but not before.

jlarsson13
  • 26
  • 2
0

Headers won't redirect in the following conditions:

Any blank space is remaining in the file after ?> tag.

To avoid this, please avoid ending ?> tag (file end).

Please check whether, any echo or print statement is written accidentally.

Also, please check whether any HTML tag is remaining there.

If you follow, all this, your script will work.

Finally, in the starting of the file, put

ob_start()

This function will store all your page's output in a buffer and thus your redirection will work.

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

Try this;

<?php
  ob_start();

  // code 

 ob_end_flush();
?> 

>> Call ob_start() at start of the script.
>> call  ob_end_flush() at the end of script.

Thanks!

Anand Solanki
  • 3,419
  • 4
  • 16
  • 27