Page 1 of 1

header problem

Posted: Mon Nov 14, 2005 6:50 pm
by JasonTC
The following function:

Code: Select all

function verify_login($user, $pass)
{
	global $db_connection;
	$sql = "SELECT password FROM logins WHERE username='$user';";
	$result = odbc_exec($db_connection, $sql);
	
	if (crypt($pass, "hello") == odbc_result($result, 'password'))
		header("Location: mail_form.php");
	else
		echo "<center><font color=\"#CC0000\">Login invalid. Please try again.</font></center><br>";
}
Gives the following error:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at c:\wwwroot\taxsale\mail\login.php:6) in c:\wwwroot\taxsale\mail\login.php on line 48
What's the deal?

Thanks,
Jason

Posted: Mon Nov 14, 2005 6:52 pm
by feyd
line 6 of that file output data, headers cannot be modified after data has been output... read this: viewtopic.php?t=1157

Posted: Mon Nov 14, 2005 6:55 pm
by blacksnday
Think i read that putting

Code: Select all

ob_start();
after the below the <?php tag
can solve the header problem in that situation.

I know I did that in my ban script and
with the header() redirect located in several different sections
of the script, it doesn't cause any header errors with rest of code.

edit:
feyd much faster at typing then me :P

Posted: Mon Nov 14, 2005 7:10 pm
by feyd
ob_start() is a band-aid.. fix the problem, don't hide it ;)

If you have a header call inside a page, the page should finish processing before output starts.

Posted: Mon Nov 14, 2005 7:20 pm
by blacksnday
feyd wrote:ob_start() is a band-aid.. fix the problem, don't hide it ;)

If you have a header call inside a page, the page should finish processing before output starts.
In my situation I don't think thats needed so much.

I created a IP Ban system and the header calls inside the page are
purely conditional with elseif's and if's. Actually, nothing is sent unless the user
is banned, and then if they are only under 2 conditions do I use header()
and thats for bad bad users constantly attempting access when banned.

and to further the conditional header(), only if I have it set at that time......

The reason I have used
ob_start() is because I am using a javascript outside page
to track certain stats while the code is being worked on.
Once it's complete I won't have the need to worry about headers
since the javascript will be gone...
(i am thinking that anyways :P )

Besides my Shout Box, the IP Ban is the second fully independent
piece of work from my News script, and soon plan to release it GPL
just as soon as the admin interface is pleasant enough to do so... lol

Posted: Mon Nov 14, 2005 8:22 pm
by Jenk
Actually, nothing is sent
Incorrect, there is definately some output sent to the user agent prior to your header call, else you would not be getting that error message.

white space counts as output, check you do not have any spaces before the <?php opening tag, as that is quite often the cause.

Posted: Mon Nov 14, 2005 8:25 pm
by blacksnday
Jenk wrote:
Actually, nothing is sent
Incorrect, there is definately some output sent to the user agent prior to your header call, else you would not be getting that error message.

white space counts as output, check you do not have any spaces before the <?php opening tag, as that is quite often the cause.
Yes there is something sent, i explain that above :)
during the dev of the script, i include some javascript
hence the reason I use ob_start()
once script is released, javascript will be gone :P

example can be seen at
http://s4m.us/banned.php

currently the site I am testing ban's on.
the banned.php is included into all of s4m.us and is never directly accessed