Jump to content

Headers already sent


terungwa
Go to solution Solved by Frank_b,

Recommended Posts

I am trying to do a page redirect using the header() function.

  1. I am aware that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
  2. and also that reading code with include, or require, functions, or another file access function, can create spaces or empty lines that are output before header() is called. thus causing a redirect failure.

I have therefore used this output control function (ob_start(); ob_implicit_flush()  right at the top of my page.

I am then creating the redirect like this as part of my login() function:

if($user_role == ADMIN_LEVEL || $user_role == EDITOR_LEVEL)
{
		if (!headers_sent($filename, $linenum)) 
		{
				return header("Location:http://mysite.com/dashboard.php");
				exit;
		}
		else
		{
				echo "Headers already sent in $filename on line $linenum\n" .
				  "Cannot redirect\n";
				exit;
		}
}

Unfortunately I am still getting this error:

Headers already sent in /home/steveoje/public_html/login.php on line 1 Cannot redirect

And this is what I have at the top of the login.php page:

<?php ob_start(); 
ob_implicit_flush(); 
require_once 'includes/config.php';
if(isset($_POST['submit']))
{
	login($_POST['username'], $_POST['password']);	
}
require_once "includes/header.inc.php" ?>
//other html content

So why would the redirect fail in this instance?

 

Thanks.

Edited by terungwa
Link to comment
Share on other sites

First of all:

 

Not all PHP features are a good practise to use. You will never find ob_start() and other ob_* functions inside my code files.

 

First off all ob_* functions is just caching your output. But besides of that you would not need them if you follow one golden rule:

 

Allways first handle your PHP logic without a single output. On the bottom of your script (i prefer in an other file) you start your output.

<?php
require_once 'include/config.php';

$message = 'Please login';
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $success = login(...);
    if($success)
    {
        header('Location: member.php');
        exit; // do not forget to use exit after header('Location: ???');
    }

    $message = 'Wrong credentials';
}

include '/path/to/templates/loginform.html.php';
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<?php echo $message; ?>
<!-- Login form here -->
</body>
</html>

Edited by Frank_b
  • Like 1
Link to comment
Share on other sites

  • Solution

Sometimes it happens that you have a BOM (Byte Order Mark) on top of your file(s).

 

http://en.wikipedia.org/wiki/Byte_order_mark

 

You are able to check the files in an hex-editor like for example http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm. If there are a few bytes on top of the file before the content that you wrote this causes output and therefor the header() function will not work anymore.

 

To get rid of it open a new php file in a php edittor, copy and past your code from the old file into the new file and save the new file. (overwrite the old one!)

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.