prakash Posted November 8, 2007 Share Posted November 8, 2007 I need help on two things First how can I get only hostname from $_SERVER['HTTP_REFERER'] on php 4 it works fine with component in php 5 but I cannot figure out how to do it in php4 Second How can I get only hostname from specific variable on php4 same thing works fine with parse_url in php5 using component but not in php4 Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 8, 2007 Share Posted November 8, 2007 You can use $_SERVER['HTTP_HOST'] instead. PhREEEk Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 8, 2007 Share Posted November 8, 2007 Oh wait, I might have misunderstood... if you need to extract the hostname from the referer, you'll need to use a Regex for it. There are literally hundreds of pre-made preg_match formulas around the net for extracting host names, so no real need to re-invent the wheel. Just search php.net or wherever. PhREEEk Quote Link to comment Share on other sites More sharing options...
prakash Posted November 8, 2007 Author Share Posted November 8, 2007 hi, I searched for the same on phpfreaks and littlw bit on google but can't find exactly what I want. could you give me the link or help me for the code thanks Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 8, 2007 Share Posted November 8, 2007 This is right from php.net examples for preg_match. Save this as referer.php. Change the "Let's test it" domain to yours (or localhost, or whatever). Run it. It will first echo "domain name is: " and it will be empty. Click the link, and it should then echo your domain name. That verifies it's working to whatever degree. Very complicated referers and some search engine referers might break the reg match. YMMV. This was tested on a live production server running PHP 4.x <?php // get host name from Referer $check_referer = $_SERVER['HTTP_REFERER']; preg_match('@^(?:http://)?([^/]+)@i', $check_referer, $matches); $host = $matches[1]; // get last two segments of host name preg_match('/[^.]+\.[^.]+$/', $host, $matches); echo "domain name is: {$matches[0]}\n"; echo "<p> Let's test it! <a href=\"http://www.your_domain.com/referer.php\">Click Here!</a>"; ?> PhREEEk Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.