I'm pretty new to regex, and this is the first time I've tried to use them in php. I'm just trying to take $_SERVER['HTTP_REFERER'] and produce a host string such as http://forums.digitalpoint.com. I'm not even sure if I'm using the best function p; eregi('^(.*//[a-z0-9.-:]+)',$_SERVER['HTTP_REFERER'],$adcustom['referring_host']); var_dump($adcustom['referring_host']); PHP: It seems like this would work as I didn't didn't ask for more slashes within the [] brackets, but it still returns the rest of the referer string that includes the filename.
This is the example of php.net <?php // get host name from URL preg_match('@^(?:[url]http://)?([/url][^/]+)@i', "http://www.php.net/index.html", $matches); $host = $matches[1]; // get last two segments of host name preg_match('/[^.]+\.[^.]+$/', $host, $matches); echo "domain name is: {$matches[0]}\n"; ?> PHP: Put the referrer variable in place and you will get referring host in return. Wish you good luck. Ref. http://au3.php.net/preg-match
var_dump(preg_match('@^(?:[url]http://)?([/url][^/]+)@i',$_SERVER['HTTP_REFERER'], $matches)); Code (markup): prints int(0) when $_SERVER['HTTP_REFERER']==='http://localhost:82/labs/test.php' But I am still trying to figure it out Thanks coolsaint
This is the code I used for a different project where I wanted to get the root domain $url = preg_replace("/^http:\/\//", "", $_GET['url']); $url_tokens = explode("/", $url); $url = $url_tokens[0]; PHP: First I stripped off the http://, then seperated out the root and each folder and then just selected the root. It's not pure regex but it works and it was easy to create.
Also, if you want to block a certain referer, you should remove the www. when checking. extract(parse_url($_SERVER['HTTP_REFERER'])); $referer = $scheme .'://'. str_replace('www.', null, $host); echo $referer; PHP:
Old post - but I see the regex solution to this problem everywhere. Just use explode with / as the delimiter. $foo = @$_SERVER['HTTP_REFERER']; if ( strlen($foo) > 0 ) { $bar = explode("/",$foo); $host = $bar[2]; } else { $host = ""; } Code (markup):