4 Replies - 978 Views - Last Post: 14 April 2010 - 06:50 AM Rate Topic: -----

#1 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

Checking IP Address

Posted 14 April 2010 - 05:34 AM

Id like to write a script that will look at the incoming users IP Address and if that address is outside the internal network force the user to logon before they can view anything.

I know I can get the IP using the code below, im not sure how to check weather or not they are on the internal network or not.

<?php
$ip = $_SERVER['REMOTE_ADDR'];
?>


As a side note im writing this code for Drupal and if anyone knows their api and could help with that also I would rep you to infinity if I could. Any help is appreciated.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Checking IP Address

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,141
  • Joined: 08-August 08

Re: Checking IP Address

Posted 14 April 2010 - 06:10 AM

Private ip addresses:
http://en.wikipedia....Private_network
Was This Post Helpful? 0
  • +
  • -

#3 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

Re: Checking IP Address

Posted 14 April 2010 - 06:15 AM

View PostCTphpnwb, on 14 April 2010 - 05:10 AM, said:



Hey, CTphpnwb thanks for the response.
I know the difference between a private IP address and a public IP address, im gessing I can't just say

if (ip == 10.0.0.0 – 10.255.255.255 || ip == 172.16.0.0 – 172.31.255.255 || ip == 192.168.0.0 – 192.168.255.255) {
     load content for user
else 
     force logon



Im not sure how I would list such a broad range of IP's in an if statement.

Edit: come to think of it i only need to check for class C addresses but I still have the same problem

This post has been edited by JordanH: 14 April 2010 - 06:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,141
  • Joined: 08-August 08

Re: Checking IP Address

Posted 14 April 2010 - 06:23 AM

There are three ranges. Those addresses starting with:

10.x.x.x
172.16.x.x through 172.31.x.x
192.168.x.x

So those are the only numbers you need to check.

If you only need to check class C, you only need to check if the address starts with 192.168. A simple str_pos() will do.
Was This Post Helpful? 0
  • +
  • -

#5 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Checking IP Address

Posted 14 April 2010 - 06:50 AM

View PostJordanH, on 14 April 2010 - 07:15 AM, said:

I know the difference between a private IP address and a public IP address, im gessing I can't just say...

Actually, you sort of can. Take a look at the ip2long() function. It converts an IP address string into a long integer. That will allow you to do numeric comparisons on the IP addresses. So you could do something like:
$your_ip = ip2long($_SERVER['REMOTE_ADDR']);
if (ip2long("10.0.0.0") <= $your_ip && $your_ip <= ip2long("10.255.255.255")) {
   # Do stuff for internal traffic
}


This post has been edited by AdaHacker: 14 April 2010 - 06:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1