Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

A list of IP ranges and actions depending on the visitor IP - how?

$
0
0

Hey there, php freaks. I'm trying to whip up a script that is supposed to do the following:

 

I have a list of certain IP address ranges and single IP addresses that I would like to use as an array in a separate file. Whenever someone visits the webpage, I want, depending on whether the visitor's IP address matches any of the IP ranges/IPs in the array, one of two actions to be executed:

 

1. Visitor's IP address is not found on the IP list - show a certain html code ( page variation 1 )

 

2. Visitor's IP address matches one of the entries on the list - show another html code ( page variation 2 ).

 

How would I go about doing that? Do I have to use "ip2long" for the IP ranges? Also, since my IP list will include both single IP addresses and IP ranges, how should I go about checking for both?

 

 

<?php

$start_range = ip2long( "x.x.x.x" );
$end_range = ip2long( "x.x.x.x" );
$user_ip = ip2long( $_SERVER['REMOTE_ADDR'] );

if ( $user_ip >= $start_range && $user_ip <= $end_range) { /* html code */}


?>

 

 

That should do the trick, but I want to define the database with the IP ranges in a separate file, not directly in the php code, since there are quite a few of them. The database with the IP ranges should be a simple TXT file, not a SQL db.


Viewing all articles
Browse latest Browse all 13200

Trending Articles