View Full Version : Passing Variables using php?

02-24-2010, 01:22 AM

I have a webpage that has URL information passed to it i.e. www.domain.com/?URL=www.test.com/page1/page2/page3.html

What I would like to do is grab the information displayed in the URL string and pass it into the webpage, either in a link or text. Here's the kicker though. I don't want the entire URL to be displayed in the webpage, I only want the root domain i.e. test.com, without http or www. or any subdomains, subpages or ugly query strings.

I've searched high and low for the answer and can't find it anywhere. If someone could spell out the exact code required, i.e. getting the variable to be displayed on the page and making it 'clean', I would be most grateful. My php knowledge is very limited so assume no prior knowledge to be on the safe side.

Thanks in advance for your help.

02-24-2010, 01:28 AM
So you want a GET method so it becomes



02-24-2010, 02:49 PM
Hi masterofollies,

What I would need is a GET method that pulls just the test.com part and nothing else. I'll try and clarify it a bit better than I did in the previous post.

When a visitor hits the webpage their address bar will show http://www.domain.com/?URL=www.test.com/page1/page2.html

The GET function would then pull out just test.com from the query string above and display it in the visible text part of the webpage i.e.

Words words words test.com words words words etc.

I should also add that I can't modify the variables that are passed through to the webpage as this is done by a third party. So my only choice is to clean the URL after it's been passed.

02-26-2010, 01:53 PM
Can anyone shed some more light on the above query? Would really appreciate it.


02-26-2010, 04:08 PM
I think that this should work:

$path = $_SERVER["PHP_SELF"];
$parts = Explode('/', $path);
$currentDomain = $parts[0];

echo $currentDomain;

This should print the domain name (I think that that's what you want). If it doesn't try playing around with the different indexes of $parts.

02-26-2010, 04:17 PM
I think this is what you're looking for:


$hostDetails = parse_url($_GET["URL"]);

print "<pre>\n";
print "</pre>\n";

$showWWW = false;

print (string)(($showWWW) ? $hostDetails["host"] : (str_replace("www.", "", $hostDetails["host"])));



I've put the print_r in there so you can see which other options you're open to.

Hope this helps,

03-01-2010, 01:27 PM
Thanks everyone for your help. Query solved.