Jump to content

need help on php 4


prakash

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/76477-need-help-on-php-4/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/76477-need-help-on-php-4/#findComment-387333
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/76477-need-help-on-php-4/#findComment-387365
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.