Why isn't the header location line being processed?
Moderator: General Moderators
Why isn't the header location line being processed?
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?
<?
$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?
- 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?
You can't echo anything to the page before using header().
Re: Why isn't the header location line being processed?
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
Jerry
- 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?
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
EDIT: See this thread
Re: Why isn't the header location line being processed?
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 ... ... 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
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);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
Re: Why isn't the header location line being processed?
thanks to everyone. I got it working.
Jerry
Jerry
- 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?
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')?Nice work using the full URL by the way ... that is how redirects *should* be sent.
Re: Why isn't the header location line being processed?
Clipped from the PHP manual page for header();
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*
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.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 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*
- 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?
Ah, I see. Thanks for the info, I'll have to keep that in mind. 