Page 1 of 1

*solved* troubles using header functions

Posted: Tue Apr 18, 2006 10:31 am
by pedrotuga
I get this error:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /var/www/2006.04.18/ligabd.php:12) in /var/www/2006.04.18/login.php on line 30
on the line i use:

Code: Select all

header(location:xpto.php);
the ligabd only contains this:

Code: Select all

<?php

$dbname = "dbname";
$dbuser = "username";
$host = "myserver.com";
$password = "userpass";

mysql_connect($host, $dbuser, $password);
mysql_select_db($dbname);

?>
why da heck doesnt it redirect to xpto.php?

Posted: Tue Apr 18, 2006 10:35 am
by Chris Corbyn
Where's line 30 and do you know you haven't enclosed the string in header() inside quotes? (Might generate a notice).

EDIT | Look at line 12 of that other file too. I bet there's whitespace after the closing <?php tag.

Posted: Tue Apr 18, 2006 10:38 am
by pedrotuga
line 30 contains this

Code: Select all

header("location: xpto.php");
byuseing include am i sending any header to the output? if so how can i use a redirect after an include?

Posted: Tue Apr 18, 2006 10:38 am
by rubberjohn
if there are any spaces before and after the php tags it will trigger an error like that and you cant have any output (echo/ print_r) before the header redirect line

rj

Posted: Tue Apr 18, 2006 10:42 am
by pedrotuga
rubberjohn wrote:if there are any spaces before and after the php tags it will trigger an error like that and you cant have any output (echo/ print_r) before the header redirect line

rj

damn... i didnt know PHP had this strict rules... well.. i gess it should be due to spaces somwhere.

BTW... sorry this being a stupid question... but... allways beter to ask..

does an echo inside an IF which condition is not satisfied generates one of those errors? or should i only be carefull about echos that are runned successfully?

Posted: Tue Apr 18, 2006 10:45 am
by s.dot
Only if it is sent succesfully.

Code: Select all

//    this will work
$x = 2;

if($x == 1){
    echo 'that';
}

header("Location: xxx.php");

//    this will not work
$x = 2;

if($x == 2){
     echo 'that';
}

header("Location: xxx.php");

Posted: Tue Apr 18, 2006 10:48 am
by pedrotuga
thank you all!

working... i pop up within a while with some other questions ;)..lol.. just hope not so soon