Eric
Eric

Reputation: 41

Allowing website access only from a specific network

I'm making a website that appeals to students at my school. I want to only allow access if the user is on the campus wifi or hardwire. Using PHP, how can I restrict access to people I am sure are on the campus internet?

Upvotes: 1

Views: 1535

Answers (4)

KevinDTimm
KevinDTimm

Reputation: 14376

It's already been mentioned, but the 'right' way to do it is to specify the IP range in the setup of your webserver (IOW, don't do it in PHP)

Upvotes: 0

tanjir
tanjir

Reputation: 1334

At first, you need to get the range of IPs from your school's network admin. Then from PHP:

$ip=$_SERVER['REMOTE_ADDR'];
if(inRange($ip)) die();
else { ....

Now write inRange($ip) function to return true if the given ip is in the range. you can use explode function to get pieces of the ip to compare.. :)

Upvotes: 0

AndreKR
AndreKR

Reputation: 33697

This is usually done in the webserver configuration, which has the advantage of also working for images, but in theory you could put

if ($_SERVER['REMOTE_ADDR'] != '...')
    die();

in every of your PHP pages.

Upvotes: 1

James Thompson
James Thompson

Reputation: 1027

You would need to get a range of IP addresses and put them in a while list. You could then use the $_SERVER['REMOTE_ADDR'] variable to check against the white list for access. Do it at the beginning of the page with something like this:

if(in_array($_SERVER['REMOTE_ADDR'],$white_list)) {
  //allow execution code?
} else {
  exit;
}

Upvotes: 2

Related Questions