Page 1 of 1

Obtain Variables from URL

Posted: Thu May 12, 2005 2:31 am
by lazersam
Hi all,

Does anyone know how php can get this data (myid) from the URL...

Example 1: mydomain.com/myid

I am aware that if I wrote the URL 'mydomain.com/?id=myid' I could GET the variable 'id' using the $_GET statement but how do I get myid from example 1?

Thanks in advance.

Larry.

Posted: Thu May 12, 2005 3:22 am
by phpScott
you might be able to with $_REQUEST['myId']
not sure though.

Look throught the superglobals on php.net

Posted: Thu May 12, 2005 6:33 am
by lazersam
Hmmm.. no that didn't work. I am experimenting with reading the URL and then spliting the address - the only problem is that the data is not included in the URL I have retrieved...

Code: Select all

<?php

		//HTTP_REFERRER = "mydomain.com/test.php/myid"
		$referred_from = $HTTP_REFERER; 
		
		
	$parsed = parse_url("$referred_from");
       print_r($parsed);//prints Array ( [scheme] => http [host] => mydomain.com [path] => /test.htm )

		
?>
The $referred_from is not picking up the 'myid' data!

Any ideas?

Larry.

Posted: Thu May 12, 2005 6:44 am
by JayBird
what about this

Code: Select all

$elements = split("/", $referred_from);

array_reverse($elements);

echo $elements[0];

Posted: Thu May 12, 2005 11:51 am
by lazersam
Thanks pimptastic... but that didn't work. The problem I am having is that I cannot bring the data part of the URL into a string. Whenever I try to caputre the URL I get the entire string except the data at the end for some strange reason....

This is the string... 'mydomain.com/test.php/myid' but the 'myid' part is always missed out. Even the HTTP_REFERER and parse_url function cuts it off(??)

I am able to retract the URL into a string variable BUT the data part (i.e. myid) is always missing from the string. If I can figure out how to bring the entire URL into a string then the problem is solved.

This is becoming a bigger issue then I first thought :-|

Can anyone help?

Posted: Fri May 13, 2005 2:44 am
by Chris Corbyn
What exactly is it you're trying to acheive? It sounds like you may want to look into using mod_rewrite and then parsing an actual URL such as mydomain.com/test.php?id=myid into something the user sees as mydomain.com/myid...

That's pretty simple RewriteRule to make :-)

Posted: Fri May 13, 2005 3:13 am
by m3mn0n
Never trust HTTP_REFERER. There is browsers out there that do not pass on that variable, among others.

Run this:

Code: Select all

echo '<pre>';
print_r ($_SERVER);
echo '</pre>';
Check out some of the variables listed in that array dump.

Posted: Fri May 13, 2005 3:13 am
by lazersam
Hi d11wtq

I am writing a script that allows users to shorten their affiliate URL's by using my domain name with their username. Visitors would use the shortend version of the URL, bounce into my script and be re-directed to the users URL.

So, 'www.TheirAffiliateLink.com/whatever.php/?id=theirid' could be shortend to 'mydomain.com/TheirUsername'. My script would need to disect the URL and identify the data 'TheirUsername'. The problem is that I would like the URL to be 'mydomain.com/TheirUsername' rather than 'mydomain.com/?id=TheirUsername' - BUT I am unable to isolate the data 'TheirUsername' from the URL string as functions are failing to pick it up. As mentioned above, even HTTP_REFERER and parse_url do not bring it into my string variables.

I hope this helps :)

Larry.

Posted: Fri May 13, 2005 3:24 am
by m3mn0n
Sami wrote:Never trust HTTP_REFERER.
Here is a recent post with some info to back that suggestion up:

viewtopic.php?t=33296

Posted: Fri May 13, 2005 3:25 am
by lazersam
Sami... thanks for that. If I run

Code: Select all

$data1 = $_SERVER['PATH_INFO'];
then the data part of the URL is recovered. That's what I wanted :D

Larry.

Posted: Fri May 13, 2005 3:26 am
by phpScott
trying to do whathttp://www.tinyurl.comdoes?