can I use php to generate a redirection url?

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
eatrom
Forum Newbie
Posts: 15
Joined: Tue Jul 16, 2002 8:32 am
Location: White North of Minnesota

can I use php to generate a redirection url?

Post by eatrom »

I have a client who has a range of agent (this can vary, agents are maintained in a mysql db) and she wants to be able to use a link with the agents name, ie: domain.com/agentname and have this redirect to the agents profile page.

Is it possible with php to do something like this? I don't see creating a redirction page for each agent; there could be a hundred. Do I need to do something at the server level to deal with pages that don't exist?

Thanks in advance

MacTrom
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

eatrom
Forum Newbie
Posts: 15
Joined: Tue Jul 16, 2002 8:32 am
Location: White North of Minnesota

Post by eatrom »

yeah -- I already figured the quick and dirty would be to write a custom 404 to deal with it. I'm in the midst of that now, so "We'll See"

MacTrom
eatrom
Forum Newbie
Posts: 15
Joined: Tue Jul 16, 2002 8:32 am
Location: White North of Minnesota

Post by eatrom »

Not quite what I need.

I can't replace the server's generated 404 error page, but I was able to edit it to redirect to my own php script in order to query the database. unfortunately, I lose the actual url that was entered and found to be non-existant.

IE:
user enters http://domain.com/agentname

404 error page redirects to not_found.php which tries to query the db for the agent name, but by now, it's lost.

I'm thinking I can use javascript in the original 404 error page to extract the url params and pass that along. I'm a bit rusty though on javascript and what I need to get this info. document.referrer doesn't work, as the url is being entered directly, not from a link.

Help???

MacTrom
eatrom
Forum Newbie
Posts: 15
Joined: Tue Jul 16, 2002 8:32 am
Location: White North of Minnesota

Post by eatrom »

Just thought I would post this as the code that worked for me.

I edited the 404 error doc to be:

Code: Select all

<head>
<SCRIPT LANGUAGE="JavaScript">
loc = document.location.href;
pos= loc.indexOf(".")+5    // strips the domain.com/ from the url
parm=loc.substr(pos,99)
document.write("<meta http-equiv='refresh' content='0;url=not_found.php?link="+parm+"'>");
</SCRIPT>
</head>
and then my not_found.php code page:

Code: Select all

<?
	$req_url=$_REQUEST&#1111;'link'];
	$sql="select AgentID from agents where concat(left(FName,1),LName) = '$req_url' ";
	$rs=mysql_query($sql);
	while($row=mysql_fetch_object($rs))&#123;
		$agent_id=$row->AgentID;
	&#125;
	if($agent_id) &#123;
		header("Location: agent_profile.php?ag=$ag&agent_id=$agent_id");
	&#125;
... rest of page showing navigation and "File Not Found" text.
Thanks again for the help

MacTrom
Post Reply