PHP headers already sent when page redirected

Possible duplicate:
Headers Already Submitted by PHP

Hi, when I go to the site, he 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 no one 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'); } } } ?> 
+6
html php
source share
3 answers

There are spaces between your two PHP tags.

 ?> <?php 

This will result in sending headers before starting any session and sending more headers.

I suggest just removing this bit of code.

+18
source share

problem between lines 16 and 18:

 ?> <?php 

this will write some spaces (and send headers)

+8
source share
 ob_clean(); 

Add the code above. Clears a buffer or something like that

If you had google for this http://www.google.co.in/search?q=Warning%3A+session_register%28%29+[function.session-register†%3A+Cannot+send+ session + cache + limiter + - + + headers have already been sent + the first result has a solution that says

make sure you add ob_start (); on the first line of the web page and ob_end_flush (); at the end of the web page // I used ob_end_clean () or ob_clean () in a similar case

Update:

I was sent down. So let me clarify.

In the code in question, the space between the php tags was clearly visible, so the solution was simple. However, if the question was not so simple, perhaps it was an include file that was problematic. What if the code got into bundles and loops before generating a space or other output, and you could not track it? What if?

If you add stuff, you don't need the output between ob_start () and ob_end_clean (). The output is not sent. ob_clean () does something similar.

Update 2 As Colonel Shrapnel said

Never miss a mistake

Soultion: Remove offensive space.

Update for @meagar

While the answer to this problem should be closing the gap, I am posting a solution to the problem using ob_clean () only for 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 top of the page. ob_clean () is only required to complete buffering. If you are sure that the page will not display and redirect, then ob_clean is also not needed. (Otherwise, use ob_clean () before the segment of the problematic code.) Thanks to @meagar for code and inspiration.

+1
source share

All Articles