how to include the right php by checking the ip

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sean82
Forum Newbie
Posts: 8
Joined: Fri Apr 16, 2004 5:33 am

how to include the right php by checking the ip

Post by sean82 »

hi
i need a script that includes a file.php only if the user is from germany and if not (if the user has other ip) it includes the other file2.php
is this possible?
can u help
thx
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

well, you would need a database which lists which ips are from which country

then just check thier ip and base your descision upon which country the ip correlates to.

geoip has one for free i beleive.


but still, there will always be ips that will be identified incorrectly. it think geoip claimed high 90% accuracy but i think you can do better in this situation. this would also add a lot of load to the server, having to query the db so many time from such a large set of data



i beleive most browsers send an HTTP_ACCEPT_LANGUAGE header for this purpose

Code: Select all

if (isSet($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    $accept = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']);
    if (false !== strpos($accept, 'de')) {
        // include german.php
    } elseif (false !== strpos($accept, 'en')) {
        // english
    } else {
        // default
    }
} else {
    // default
}
User avatar
sean82
Forum Newbie
Posts: 8
Joined: Fri Apr 16, 2004 5:33 am

Post by sean82 »

this is a great idea but how does it work
i used this but nothing was included just an empty screen

Code: Select all

<?
if (isSet($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {  
  $accept = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']);   
   if (false !== strpos($accept, 'de')) {      
     // include default.php 
        } elseif (false !== strpos($accept, 'en')) {     
           // include navigation.php  
             } else {     
                // default   
                 }
                } else {   
                 // default
                 }
           ?>
Post Reply