Page 1 of 1

Warning: Cannot modify header information - headers already

Posted: Thu Apr 26, 2007 8:59 pm
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

Posted: Thu Apr 26, 2007 9:33 pm
by John Cartwright
Please search "headers already sent", as this is likely the most common problem around here.

Posted: Fri Apr 27, 2007 4:31 am
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.)

Posted: Fri Apr 27, 2007 5:22 am
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().

Posted: Fri Apr 27, 2007 5:29 am
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 ;)

Posted: Fri Apr 27, 2007 5:47 am
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

Posted: Fri Apr 27, 2007 6:02 am
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.

Posted: Fri Apr 27, 2007 9:51 am
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

Posted: Fri Apr 27, 2007 10:24 am
by John Cartwright
Dreamweaver is known to throw in hidden characters now and again, copy+paste into notepad and saving usually does the trick.

Posted: Fri Apr 27, 2007 10:57 am
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.