Obtain Variables from 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
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Obtain Variables from URL

Post 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.
Last edited by lazersam on Thu May 12, 2005 3:26 am, edited 1 time in total.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

you might be able to with $_REQUEST['myId']
not sure though.

Look throught the superglobals on php.net
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

what about this

Code: Select all

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

array_reverse($elements);

echo $elements[0];
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :-)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

trying to do whathttp://www.tinyurl.comdoes?
Post Reply