Pass a URL in a PHP GET Variable

Today I had to figure out how to pass a full URL or Domain Name with a query attached to another PHP page for processing. I Googled it and came up with no answer from Google surprisingly, so I figured I’d write about how to send a URL through to another page using a PHP GET variable. In other words I am sending a URL through a URL using PHP. Is that clear? I hope so!

Anyway, here is some PHP code:

//$theurl = $_GET[‘u’];//doesn’t work so:
$theurl = $_SERVER[‘REQUEST_URI’];
$theurl2=urldecode($theurl);
//explode theurl2 at ?u=:
$urlarr = explode(“?u=”,$theurl2);//key1 is just the url from the url:
$theurl3 = $urlarr[1];
echo “u variable from URL contains: $theurl3<br />”;

If you put the above code in a file named getURL.php and went to the following URL in your browser:

http://example.com/getURL.php?u=http://example.com/NFL/?visitor=Buffalo Bills&home=Miami Dolphins&date=09/08/2014&sbmt=Make Prediction!

Replace example.com with your domain and the output of the above PHP code would be:

u variable from URL contains: http://example.com/NFL/?visitor=Buffalo Bills&home=Miami Dolphins&date=09/08/2014&sbmt=Make Prediction!

Summary

Hopefully I have demonstrated here that while you can pass a URL in a URL using PHP, you cannot retrieve the URL from the URL using the $_GET[‘u’] method. You can however retrieve a URL in a URL using $_SERVER[‘REQUEST_URI’]. I hope that helps. If it doesn’t, this tutorial may be beyond your level of understanding PHP. Keep studying!

Leave a Reply

Your email address will not be published. Required fields are marked *