Hello, I know that the following code prints output of the referring URL. <?php $ref=$_SERVER['HTTP_REFERER']; echo "$ref"; ?> Code (markup): For example the referring URL is http://forums.digitalpoint.com/showthread.php?t=123, i need the value to be grabbed as just digitalpoint removing all the stuff after and before. The following links may be useful in reaching the solution for this. http://phosphorusandlime.blogspot.com/2007/08/php-get-base-domain.html http://in.php.net/parse_url PHP str_replace and other PHP Standard functions. I am trying to get this working by preg_replace and str_replace but really couldn't get it working. Expecting a quick reply from someone who had achieved such a thing.
This'll work: <? if (isset($_SERVER['HTTP_REFERER'])) { // check there was a referrer $uri = parse_url($_SERVER['HTTP_REFERER']); // use the parse_url() function to create an array containing information about the domain Echo $uri['host']; // echo the host } ?> Code (markup):
Thanks for this. I tried this but I just need something more. say for example, the referring domain may be domain.com or www.domain.com I would like the answer to be domain.com irrespective of that. So is it possible to match and split ?
<? if (isset($_SERVER['HTTP_REFERER'])) { $uri = parse_url($_SERVER['HTTP_REFERER']); $domain = substr($uri['host'], strpos($uri['host'], ".")+1, strlen($uri['host'])); Echo $domain; } ?> Code (markup): If the referrer is something like subdomain.domain.com it will strip the subdomain though. Here's what you could do if you want to strip www. but keep the subdomains: <? if (isset($_SERVER['HTTP_REFERER'])) { $uri = parse_url($_SERVER['HTTP_REFERER']); $domain = str_replace("www.", "", strtolower($uri['host'])); Echo $domain; } ?> Code (markup):