32
include 'header.php'; // ... some code header('Location:index.php'); exit; 

The above code keeps giving me an issue with the redirect. The error is the following:

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15) in /Applications/MAMP/htdocs/testygubbins/OO/test/form.php on line 16.

What should I be doing to make it work?

header.php code:

<?php include 'class.user.php'; include 'class.Connection.php'; $date = date('Y-m-j'); ?> <html> <head> <link rel=StyleSheet href="css/style.css" type="text/css" media=screen> <title>Test</title> </head> <body> <div id="page"> 
1
  • 1
    You can't redirect if you have already sent HTML output. Either use output buffering or replace 'include "header.php"' with the PHP block in header.php. Commented Jan 8, 2009 at 15:46

9 Answers 9

54

Look carefully at your includes - perhaps you have a blank line after a closing ?> ?

This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls.

Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.

(EDIT: looking at your header, you need to avoid doing any HTML output if you want to output headers, or use output buffering to capture it).

Finally, as the PHP manual page for header points out, you should really use full URLs to redirect:

Note: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

Sign up to request clarification or add additional context in comments.

1 Comment

It's not just the whitespace, it's the whole HTML output after the PHP block that's causing the error.
9

Alternatively, not to think about a newline or space somewhere in the file, you can buffer the output. Basically, you call ob_start(); at the very beginning of the file and ob_end_flush(); at the end. You can find more details at php.net ob-start function description.

Edit: If you use buffering, you can output HTML before and after header() function - buffering will then ignore the output and return only the redirection header.

Comments

8

Try This :

ob_start(); include 'header.php'; // ... some code header('Location:index.php'); ob_end_flush(); exit; 

Comments

2

If I understand correctly, something has already sent out from header.php (maybe some HTML) so the headers have been set. You may need to recheck your header.php file for any part that may output HTML or spaces before your first

EDIT: I am now sure that it is caused from header.php since you have those HTML output. You can fix this by remove the "include('header.php');" line and copy the following code to your file instead.

include('class.user.php'); include('class.Connection.php'); $date = date('Y-m-j'); 

Comments

2

You may have some "plain text" somewhere in php files that is interpreted as script output. It may be even a newline before or after the php script tag specifier (less-than + question mark + "php").

Besides, if I remember correctly, according to http specification, the "Location" header accepts only full URLs, not relative locations. Have that in mind too.

Comments

2

Don't include header.php. You should not output HTML when you are going to redirect.

Make a new file, eg. "pre.php". Put this in it:

<?php include('class.user.php'); include('class.Connection.php'); ?> 

Then in header.php, include that, in stead of including the two other files. In form.php, include pre.php in stead of header.php.

Comments

1

Your include produces output, thereby making it impossible to send a http header later. Two option:

  1. Move the output somewhere after the include.
  2. Use output buffering, i.e. at the very start of your script, put ob_start(), and at the end, put ob_flush(). This enables PHP to first wait for all the output to be gathered, determine in what order to render it, and outputs it.

I would recommend you learn the second option, as it makes you far more flexible.

Comments

0

Also see your php file text encoding. Mine was UTF-8 with BOM and it prevented the script to work. But now works flawlessly after removing the BOM...

Comments

-2

Try redirection with JavaScript:

<script type="text/javascript"> window.location.href='index.php'; </script> 

1 Comment

How do search engines interpret this? It sure can't be a 301 redirect, can it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.