header problem

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
JasonTC
Forum Commoner
Posts: 92
Joined: Wed Nov 02, 2005 11:05 am
Location: Grand Rapids, MI

header problem

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

line 6 of that file output data, headers cannot be modified after data has been output... read this: viewtopic.php?t=1157
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post 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
Post Reply