Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP headers already sent while redirecting page [duplicate]

Possible Duplicate:
Headers already sent by PHP

Hello there when i go to the site it says

Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at /home/content/49/5712349/html/c/admin/admin.php:17) in /home/content/49/5712349/html/c/admin/admin.php on line 39

Warning: Cannot modify header information - headers already sent by (output started at /home/content/49/5712349/html/c/admin/admin.php:17) in /home/content/49/5712349/html/c/admin/admin.php on line 41

I saw other questions and none answered me.

Here is the code thanks a lot.

<?php 

if (isset($_SESSION['mattyc-admin'])){
    header ('Location: admin/home.php');
}

if (!isset($_GET['me'])){
    header ('Location: http://www.stat-me.com/mattyc');
}

if ($_GET['me'] != 'mattyc'){
    header ('Location: http://www.stat-me.com/mattyc');
}

?>

<?php

if ($_POST['name'] != "" && $_POST['password'] !="") {

//require "../../scripts/connect_to_mysql.php";

$name = $_POST['name'];
$pass = $_POST['password'];

$name = strip_tags($name);
$pass = strip_tags($pass);
//$name = mysql_real_escape_string($name);
//$pass = mysql_real_escape_string($pass);
$name = eregi_replace("`", "", $name);
$pass = eregi_replace("`", "", $pass);

//$pass = md5($pass);

if ($name == 'mattyc' && $pass == 'qwerty'){
    if (isset ($_SESSION['mattyc-admin'])){
        header ('Location: admin/upload.php');
    }else{
     session_register('mattyc-admin'); 
     $_SESSION['mattyc-admin'] = ('mattyc-adminp');
     header ('Location: admin/upload.php');
    }
}

}
?>
like image 896
DonJuma Avatar asked Oct 12 '10 16:10

DonJuma


3 Answers

Between your two PHP tags you have whitespace.

?>

<?php

This'll cause your headers to be sent before starting any session and sending more headers.

I suggest just removing that bit of code.

like image 109
Mikee Avatar answered Nov 06 '22 11:11

Mikee


the problem is between lines 16 and 18:

?>

<?php

this will write some whitespace (and send headers)

like image 8
nothrow Avatar answered Nov 06 '22 11:11

nothrow


ob_clean();

Add the above before the offending code. Cleans the buffer or something.

If you would have googled for it http://www.google.co.in/search?q=Warning%3A+session_register%28%29+[function.session-register]%3A+Cannot+send+session+cache+limiter+-+headers+already+sent the first result has the solution which says

make sure you have added ob_start(); at the first line of webpage and ob_end_flush(); at the end of webpage //i have used ob_end_clean() or ob_clean() in a similar case

Update:

I was downvoted. So let me clarify.

In the code in question , the space between the php tags was seen clearly and so the solution was straightforward. However, if the question in issue was not that simple, perhaps it was a include file which was problematic. What if the code ran into reams and reams before generating a space or other output and you couldn't track it? What if?

If you enclose stuff you do not want output for between ob_start() and ob_end_clean(). The output is not sent. ob_clean() does something similar.

Update 2 As Col. Shrapnel said

Never gag an error

Soultion: Remove the offending space.

Update for @meagar

While the answer to this problem should be removal of the space, I am posting a solution for the problem using, ob_clean() just for the upvote(I hate downvotes! :))

<?php
ob_start(); //started buffering
?>
Hello World and other dangerous texts
<?php 
if (isset($_SESSION['mattyc-admin'])){
    header ('Location: admin/home.php');
}

if (!isset($_GET['me'])){
    header ('Location: http://www.stat-me.com/mattyc');
}

if ($_GET['me'] != 'mattyc'){
    header ('Location: http://www.stat-me.com/mattyc');
}

?>

<?php
if ($_POST['name'] != "" && $_POST['password'] !="") {

//require "../../scripts/connect_to_mysql.php";

$name = $_POST['name'];
$pass = $_POST['password'];

$name = strip_tags($name);
$pass = strip_tags($pass);
//$name = mysql_real_escape_string($name);
//$pass = mysql_real_escape_string($pass);
$name = eregi_replace("`", "", $name);
$pass = eregi_replace("`", "", $pass);

//$pass = md5($pass);
ob_clean(); //can use ob_end_clean() too
if ($name == 'mattyc' && $pass == 'qwerty'){
    if (isset ($_SESSION['mattyc-admin'])){
        header ('Location: admin/upload.php');
    }else{
     session_register('mattyc-admin'); 
     $_SESSION['mattyc-admin'] = ('mattyc-adminp');
     header ('Location: admin/upload.php');
    }
}

}
?>

With output buffering set to Off in my php.ini, I tested the above code. Just add ob_start to the start of the page. ob_clean() is only needed to end buffering. If you are sure that the page will show no output and only redirect then ob_clean is not needed too. (Otherwise use ob_clean() before the problematic code segment.) Thanks to @meagar for the code and inspiration.

like image 2
abel Avatar answered Nov 06 '22 11:11

abel