-1

Possible Duplicate:
Headers already sent by PHP

I have a PHP file which I'm using to check username and password. This part is working, but after successful login I would like to use header() to redirect to user panel page. This is the logged error that I'm getting:

[10-Dec-2012 12:25:26] PHP Warning: Cannot modify header information - headers already sent by (output started at /home2/jzperson/public_html/imes/php/login.php:10) in /home2/jzperson/public_html/imes/php/login.php on line 32

This is line 10:

<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>

And this is line 32:

header("Location: http://imes.jzpersonal.com/userpanel.html");

Any idea why?

Community
  • 1
  • 1
Jakub Zak
  • 1,212
  • 6
  • 32
  • 53

5 Answers5

3

You probably have some output echoed out before getting to the line 32 with your header call.

See description of the header function: http://php.net/manual/en/function.header.php

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Clarifications

To clarify things a little bit, the redirection using header() is performed by including a raw location response-header field to the server response. When the receiving party reads the response and sees that header field, it drops the current response and issues another request to the destination you provided.

Now, headers always come at the top (head) of the server response. That's why they are called headers! If you output any content, PHP will immediately "prefix" it with default headers and it's not possible to add any more of them after this point. So, by attempting to set another header later in your code, you get an error:

Cannot modify header information - headers already sent

By outputting HTML at line 10 you can no longer issue any more headers, because they were already sent (prefixed to your HTML output).

You can find more information about headers here: http://www.faqs.org/rfcs/rfc2616.html

Oleg
  • 9,341
  • 2
  • 43
  • 58
2

Basically, you need to check whether the user is logged in or not (and redirect) before anything is sent to the browser (before HTML). Your code, then, would look something like this:

<?php
...
if($loggedIn)
{
    header("Location: http://imes.jzpersonal.com/userpanel.html");
    exit();
}
?>
<html>
...
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
1

You are trying to write something before header statement Remove any echo statements/html content before header statement. That should do the trick

Sudheer
  • 99
  • 2
1

You could also cheat and just use output buffering - at the very beginning of the script tree use ob_start(); to begin capturing the output. You can use headers and set cookies etc as much as you like then.

At the last line of the script tree use ob_end_flush(); to send the output. You can also grab it to a variable to further process if you wish with $buffer = ob_get_clean();

Although its not a solution as such it does allow for a more flexible coding environment AND it will solve your above problem.

Its best to flush and die if you are going to be sending a Location header: ob_start(); /* very long snip */ header('Location: somepage.php'); ob_end_flush(); die();

This will prevent any further processing after the location change has been sent.

Just as a side note: When I speak of a script tree I mean the include path - like put the ob_start(); into a header file thats included before anything else and a footer file that flushes (and processes if required) the output buffer. Remembering, as highlighted above, that Location changes should have the script halted immediately after.

Sessions may also need to be closed with a header Location followed by a die - to use that simply

ob_start();
/* very long snip */
header('Location: somepage.php');
ob_end_flush();
session_write_close();
die();

I found that one out after hours of wondering why session data was being lost! Bear that on mind.

Ben Duffin
  • 1,066
  • 10
  • 16
0

You can't use header(); if anything has already been sent as output. This means HTML. Do all your PHP processing first, then output your HTML/JS.

Major Productions
  • 5,914
  • 13
  • 70
  • 149