Hi,
Can I change the information of a site depending on ip address.

For example:
I have different folders for each geographic location, say for USA it should show a different site, for UK it should show a different site, like wise depending on the IP address it should go to that particular folder or index file.

Is tht possible using php?

Dani AI

Generated

Detecting a visitor's country from their IP and showing a country-specific folder is doable in PHP. already pointed to the two broad approaches; in practice a maintained geolocation dataset or a hosted geolocation API is far easier, faster, and more reliable than trying to parse WHOIS output and follow referrals (which is brittle and slow). For a local solution, the common choice is MaxMind's databases (MaxMind GeoIP2/GeoLite2); for registry lookups see the regional registries such as ARIN.

A minimal PHP pattern (local DB) looks like this:

require 'vendor/autoload.php';
use GeoIp2\Database\Reader;

$reader = new Reader('/path/to/GeoLite2-Country.mmdb');
$country = $reader->country($_SERVER['REMOTE_ADDR'])->country->isoCode;

switch ($country) {
  case 'US':
    header('Location: /usa/');
    exit;
  case 'GB':
    header('Location: /uk/');
    exit;
  default:
    include '/default/index.php';
}

Key practical notes and troubleshooting:

  • Remote IP detection: if behind a proxy/load balancer, use trusted X-Forwarded-For handling; never trust client headers from untrusted networks.
  • Cache lookups (APCu, memcached, or DB) because disk/DB lookups per request add latency.
  • Accuracy is not perfect; consider fallbacks and logging mismatches for review.
  • WHOIS-based routing (the approach described) can work but requires handling referrals, different registry formats, and aggressive caching to avoid rate limits.
  • Consider privacy and SEO: IP-based redirects affect crawlers and may require hreflang or temporary (302) redirects while testing; review regional privacy rules (e.g., GDPR) before making automated content changes.

Recommended Answers

All 3 Replies

Hi,
Can I change the information of a site depending on ip address.

For example:
I have different folders for each geographic location, say for USA it should show a different site, for UK it should show a different site, like wise depending on the IP address it should go to that particular folder or index file.

Is tht possible using php?

There are two ways of doing this. One is to buy the MaxMind database, another is to do whois calls using a socket (not through their web interface!) and cache the netblock results so you don't hammer whois and get yourself banned.

-r

Can you tell me how can I use whois? If I use that will it be possible something like this : If user is from US he shuold see one page, If he is from UK then he must see different page...and so on different places differen pages...

you can parse country information from whois response. the various ip authorities have different record formats so you have some work ahead of you.

ARIN will tell you if you need to go to another authority. Look for the Referral Server field. The presence of this field indicates that the record is delegated.

Also look for and follow referral records to the final netblock record. if you get a record with nothing but netblocks, the smallest netblock is the one you need to search for the whois record on to get the information. These referral records are created by ISP's who sublet chunks of their IP allocation.

If you aren't familiar with text parsing and especially IP address calculations, working with whois, and need to do this in a hurry, I'd buy the database. It's a *lot* easier. They have a trial database so you can demo it. Just make sure you use IP's in the trial database for demo.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.