Why isn't the header location line being processed?

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
jwcrosby
Forum Newbie
Posts: 8
Joined: Mon Sep 29, 2008 4:51 pm

Why isn't the header location line being processed?

Post by jwcrosby »

I’m working on a simple user authentication page that once the user is authenticated it branches to another html page. Here’s the code I have that the login form is posting to:

<?
$sUserName=$_POST['username'];
$sPassword=$_POST['password'];
echo $sUserName;
echo $sPassword;

if ($sUserName == "ryan" and $sPassword == "crosby") {
echo "Just above header line<br>";
header("Location: http://www.interactministries.org/blogs/b_creator.htm");
echo "Just below the header line";
} elseif ($sUserName = "siberia" and $sPassword = "interact") {
header("Location: b_creator.htm");
} elseif ($sUserName = "alaska" and $sPassword = "interact") {
header("Location: b_creator.htm");
} elseif ($sUserName = "canada" and $sPassword = "interact") {
header("Location: b_creator.htm");
}
?>

The echo lines are for troubleshooting.

When I enter “ryan” for the user and “crosby” for the password on the login form, it does run the above php page. I know because it echos the user name and password, as well as the two echo sentences (“Just above header line” and “Just below the header line”).

But it does not branch to the b_creator.htm page (which I know does exist).

Why?
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Why isn't the header location line being processed?

Post by The_Anomaly »

You can't echo anything to the page before using header().
jwcrosby
Forum Newbie
Posts: 8
Joined: Mon Sep 29, 2008 4:51 pm

Re: Why isn't the header location line being processed?

Post by jwcrosby »

So, are you telling me that it is the echo lines that are causing the header line to not be read? I deleted them and it made no difference.

Jerry
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Why isn't the header location line being processed?

Post by The_Anomaly »

There can not be anything output to the browser, at all. This includes whitespace, or echos, or prints, or anything. Header(Location: x) will ONLY work if absolutely nothing has been first sent to the browser.

EDIT: See this thread
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Why isn't the header location line being processed?

Post by Stryks »

If your location header is arriving after headers have already been sent (ie. any other output has been sent) you should be getting an error message.

If it is just failing to do anything, perhaps your errors are hidden. For testing purposes, you can add ...

Code: Select all

error_reporting(E_ALL);
ini_set("display_errors", 1);
... to the top of your script. (this should be removed from a live site)

That should tell you why it is failing. And I would recommend not using the ob_ commands unless you actually need to use their functionality. With a little thought you can overcome these issues without resorting to workarounds.

Also, just in passing, it's normally a good practice to call exit; right after sending the location header. It just stops PHP from pointlessly parsing the rest of the file after you have instructed it to redirect.

Nice work using the full URL by the way ... that is how redirects *should* be sent.

Cheers
jwcrosby
Forum Newbie
Posts: 8
Joined: Mon Sep 29, 2008 4:51 pm

Re: Why isn't the header location line being processed?

Post by jwcrosby »

thanks to everyone. I got it working.

Jerry
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Why isn't the header location line being processed?

Post by The_Anomaly »

Nice work using the full URL by the way ... that is how redirects *should* be sent.
Now that his problem is worked out, I wanted to ask about this. Why do you say that the use of full absolute URLs are encouraged? I mean, is say header('Location: ../../peanuts/butter.php') not the proper way of doing it? How about header('Location: /var/www/peanuts/butter.php')?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Why isn't the header location line being processed?

Post by Stryks »

Clipped from the PHP manual page for header();
Note: HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

Code: Select all

 <?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
 
It's probably a bit on the pedantic side I know, but if it's not in this format, some clients may not work as expected.

It's just unusual to see someone go to the trouble of using it the way it is technically supposed to be used, so I thought it worth the comment. Having said that, I have to admit that I cant recall ever having a relative URL fail. Of course, I'm pretty predictable with my choice of browser. Go Mozza!!

Cheers

*Mmmmm ... peanut butter*
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Why isn't the header location line being processed?

Post by The_Anomaly »

Ah, I see. Thanks for the info, I'll have to keep that in mind. :)
Post Reply