Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stupid Stupid header location!!

Status
Not open for further replies.

jonperks

Programmer
Nov 19, 2003
40
GB
Code:
$Username = $_SERVER['DOCUMENT_URI'];
$Username = str_replace("/", "", $Username);

$db = mysql_connect("server", "user", "pass");
@mysql_select_db("db",$db);

$sql = "SELECT UserID FROM Users WHERE Username = '$Username'";
$result = mysql_query($sql);
$row_rs_item = mysql_fetch_assoc($result);

	if ($row_rs_item['UserID'] == ""){
		
	header('location: [URL unfurl="true"]http://www.mydomain.com');[/URL]
	exit;

	}else{
	
	$sql2 = "SELECT ID FROM Details WHERE UserID = '$row_rs_item[UserID]'";
	$result2 = mysql_query($sql2);
	$row_rs_item2 = mysql_fetch_assoc($result2);
	
	$URL = "[URL unfurl="true"]http://www.mydomain.com/ido/designs/initial.php?ID="[/URL] . $row_rs_item2['ID'];
	header('location: $URL');
	exit;
	
	}

	mysql_close ($db);

This doesn't work I have no idea why, its a custom 404 page and its taking what the user is entering and then using that to login ie I am trying..


It then logs in using this... When i run it though it just doesn't do anything, no errors nothing, the outputs are all ok its just not actually redirecting.
 
What is in $_SERVER['DOCUMENT_URI'] ?
What will $username be
Which redirect is not working ?
 
$_SERVER['DOCUMENT_URI'] would be the bit after the slash from

(which doesn't exist, and this code is a custom 404 page)

so $_SERVER['DOCUMENT_URI'] holds "/user"

$Username then holds "user" because of removing the "/"

they both work fine i've checked several times now

Neither redirect is working, its bizarre
 
I recommend that you try footprinting your code:

Code:
$logfilename = 'log.txt';

$Username = $_SERVER['DOCUMENT_URI'];
$Username = str_replace("/", "", $Username);

$db = mysql_connect("server", "user", "pass");
@mysql_select_db("db",$db);

$sql = "SELECT UserID FROM Users WHERE Username = '$Username'";
$result = mysql_query($sql);
$row_rs_item = mysql_fetch_assoc($result);

[red]error_log ( 'UserID:' . $row_rs_item['UserID'] . "\n" , 3 , $logfilename);[/red]
if ($row_rs_item['UserID'] == "")
{
[red]error_log ( "In if\n" , 3 , $logfilename);[/red]
	header('location: [URL unfurl="true"]http://www.mydomain.com');[/URL]
	exit;
}
else
{
[red]error_log ( "In else\n" , 3 , $logfilename);[/red]
	$sql2 = "SELECT ID FROM Details WHERE UserID = '$row_rs_item[UserID]'";
	$result2 = mysql_query($sql2);
	$row_rs_item2 = mysql_fetch_assoc($result2);

	$URL = "[URL unfurl="true"]http://www.mydomain.com/ido/designs/initial.php?ID="[/URL] . $row_rs_item2['ID'];
[red]error_log ( 'URL:' . $URL . "\n", 3 , $logfilename);[/red]
	header('location: $URL');
	exit;
}

mysql_close ($db);

Then examing log.txt to see what the script is doing.



Want the best answers? Ask the best questions! TANSTAAFL!
 
The .htaccess file has been edited to point the 404 error to 404.php then this code is 404.php nothing else, this seems to be the best way to do this as I dont have apache mod rewrite etc its on zeus? If that means anything
 
oh and if i go straight to 404.php and run it, it works fine, its obviously something to do with the headers once its been pointed to the 404 error page?

Hope someone can help?
 
In this line:
Code:
header('location: $URL');
You are using single quotes with a variable in the string, change the quotes to double quotes, so PHP will interpret the variable.
 
Thanks for exampeling the error_log technique sleipnir.
 
Tried that previously no joy......

Any other ideas, it's working when its not used as a 404 error page, its something to do with its implementation in that way.
 
Solved the problem with javascript redirect instead, still be nice to know what PHP isn't working though
 
it looks like your re-entering the page after the intial redirect, can you repost the trace after putting the double quotes around $URL please
 
So this page is the 404 so gets invoked by something not being on the server ?
So you do some striping and SQL to get the user ID which you display as 10 - ok?
Because the user is not a null you go down the else leg where you construct the URL with another id obtained from SQL which is 6 - ok?
You then issue the redirect on this URL.
Your code is then rentered I would think as a result of another 404, whixh explinas why the second display is a empty string and you go down the if part of the switch.
My question is does the URL actualy exists ?
What happens if you type this into the address on a browser?
Which browser are you using?
Do you get the same behaviour with a differecnt browser
What web werver are you using ?
 
Everything you said is right, the 404 is invoked through the .htaccess file.

The page exists yes, it does now work as I am using a javascript redirect. Using the PHP redirect for some reason wont work when used in this situation. And it works it does the same in all browsers. I am using Safari.

The web server is zeus? means nothing to me...

Thanks for everyones help, as I said I have it working but I just cant understand whats going on with the PHP redirect not working, the only thing I can think is that the 404 is a header status and is set to error, and the header location isn't setting this to OK. I have tried something to this affect in setting status to OK but this didn't seem to do anything?

Guess its a mystery....
 
I#ll have a play tonight at home on my apache/windows set up. There have been quite a few threads on this board about how various web servers handle HTTP headers (notably IIS) when they have cookies as well, I wonder if Zeus does odd things as well.
As I say I'll have a play and if I find any thing siggnifficnt I'll post back, if not I'll go back to sleep !
 
Thanks

Dont worry your head too much, I think zeus is the culprit! and I dont have access to apache.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top