Warning: Cannot modify header information - headers already

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alejandron
Forum Newbie
Posts: 1
Joined: Thu Apr 26, 2007 8:55 pm

Warning: Cannot modify header information - headers already

Post by alejandron »

Hello,

I have 1 problem in my web

is this:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/subdomain/index.php:4) in /home/user/public_html/subdomain/conectar.php on line 22
and the conectar.php

Code: Select all

<?php
session_start();
$_SESSION['Titulo']="title";
$strUsuario=$_REQUEST['usuario'];
$strContrasena=$_REQUEST['contrasena'];
//echo "Usuario: ".$strUsuario." ".$strContrasena. "<br>";
if(($strUsuario!="")||($strContrasena!="")){
	$conn=mysql_connect("localhost","dates","dates");
	$bd=mysql_select_db("dates",$conn);
	$sql = "SELECT id FROM aso WHERE usuario='".$strUsuario."' AND password='".$strContrasena."'";
	//echo $sql. "<br>";
	$orden_fecha = mysql_query($sql, $conn) or die(mysql_error());
	$result = mysql_fetch_assoc($orden_fecha)or die("errores");
	$total = mysql_num_rows($result);
	//echo "total: ".$total. "<br>";
	//if($total!=0){
		$_SESSION['usuario']=$result['id'];
		//echo "[".$_SESSION['usuario']. "]";
	//}
}else{
	header("Location: ../index.php"); //<<<<------  line 22
}
?>
thanks for the help
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Please search "headers already sent", as this is likely the most common problem around here.
rebus
Forum Newbie
Posts: 11
Joined: Tue Nov 07, 2006 6:13 pm
Location: Croatia, Zadar

Post by rebus »

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/subdomain/index.php:4) in /home/user/public_html/subdomain/conectar.php on line 22
Please notice this part of error:

(output started at /home/user/public_html/subdomain/index.php:4)

and if you were to read your manual on header() you would see that headers need to be sent before _any_ output is sent to browser ( like echo 'something' or even error message that was generated before headers where sent.)
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post by PhpMachine »

The problem is the first line before your header():

Code: Select all

echo "Usuario: ".$strUsuario." ".$strContrasena. "<br>";
One way to get rid of the problem is to use

Code: Select all

ob_start()

[code goes here...]

ob_end_clean()
Else, you have to restructure your code so you don't have any output
before the header().
rebus
Forum Newbie
Posts: 11
Joined: Tue Nov 07, 2006 6:13 pm
Location: Croatia, Zadar

Post by rebus »

PhpMachine isn't that code commented out? Shouldn't effect headers being sent.

My guess is the output is started in index.php on line 4 not in conectar.php on line 22

But ob_start() hint is very usefull ;)
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post by PhpMachine »

Hi rebus

Yes, you're right:
ob_start():
"This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer."
How come it works then?

Works not:

Code: Select all

<?php
 echo "output";
 header("location:index.php");
?>
Works:

Code: Select all

<?php
 ob_start();

 echo "output";
 header("location:index.php");

 ob_end_flush(); // Not needed, but it's more readable if it's there.
?>
Greetings
Last edited by PhpMachine on Fri Apr 27, 2007 6:14 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Switching on output buffering so you can send headers after sending page content is a terrible solution. You're going to have increased your server overhead, used much more memory, and slowed everything down simply because you're too lazy to make your script do things in the proper order.

Refactor the code so that the headers are sent before the content. It's not very difficult.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

The issue might be because of this

Code: Select all

$_SESSION['usuario']=$result['id'];
as it is sending a session variable to a cookie.

Wayne
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Dreamweaver is known to throw in hidden characters now and again, copy+paste into notepad and saving usually does the trick.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Jcart wrote:Dreamweaver is known to throw in hidden characters now and again, copy+paste into notepad and saving usually does the trick.
At the start of the file, yes. As the output is coming from line 4 of index.php that seems unlikely.
Post Reply