This topic is locked

Enable Login only from specific IP Addresses

1/28/2009 19:07:40
PHPRunner General questions
jpedwardspost author

Hi,
Is there a way to only enable phprunner login only for users from a list of specific ip addresses.
I would like to prevent login from locations other than the office.
Thanks,
JP.

R
ringah 1/29/2009

If the IP address is fixed you could check the users IP from session variable (see PHPR help) and create a simple IF statement in user login event to check if IP is valid..something like...

$ip=$_SERVER['REMOTE_ADDR'];

if $ip = "192.168.1.1" {

echo "ok";

}


If more than one or two IP's probably best way is to create a database table with valid IP ranges:
ID | StartIP | EndIP |

+-----+-----------------+-----------------+

| 1 | 123.123.123.123 | 123.123.200.200 |

| 2 | 124.129.0.123 | 124.129.200.200 |

| 3 | 231.160.124.1 | 231.255.255.255 |
and then create code in user login event to check if IP exists in table something like:

function check_ip($IP)

{

$IP = ip2long($IP);

$result = mysql_query("SELECT * FROM IPRangeTable");

while($row = mysql_fetch_array($result))

{

$StartIP = ip2long($row['StartIP']);

$EndIP = ip2long($row['EndIP']);

if ($IP >= $StartIP && $IP <= $EndIP)

{

return FALSE;

break;

}

}

return TRUE;

}


A rough example but hope it helps..