Santanu
Santanu

Reputation: 8064

PHP: allow only two IP addresses at the same time

I want my website to be accessed from only two IP addresses. when ever the site is accessed by more than 2 IP addresses it will shoe an error. can any body done this in php please help me

thank you

Upvotes: 0

Views: 2579

Answers (5)

ThiefMaster
ThiefMaster

Reputation: 318768

To restrict the number of concurrent users you need some kind of sessions stored in a database. Then, when a new user logs in, check if there are already two sessions from different ips and in this case throw an error.

Note that you MUST make sessions expire quickly if someone is inactive so he doesn't prevent someone else from logging in just because he did not logout.

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318768

If you want to restrict all visitors to certain IPs, the easiest/fastest way would be doing so in your web server, e.g. with an Apache .htaccess, instead of doing it in PHP:

Order Deny,Allow
Deny From All
Allow From 1.2.3.4
Allow From 5.6.7.8

Upvotes: 3

Vidar Vestnes
Vidar Vestnes

Reputation: 42984

Try

if($_SERVER['REMOTE_ADDR'] != '212.100.232.111' && $_SERVER['REMOTE_ADDR'] != '212.100.232.112'){
 die('No access');
}

Upvotes: 3

Will Vousden
Will Vousden

Reputation: 33408

You need to use the $_SERVER['REMOTE_ADDR'] superglobal variable: this should give you the IP address from which the client's request originated. Just test to see whether it's allowed and show your error message if not.

Upvotes: 1

Jon Purdy
Jon Purdy

Reputation: 55069

Use $_SERVER['REMOTE_ADDR'] to get the address of the incoming connection, and test against it to perform the appropriate redirection or what have you.

Upvotes: 1

Related Questions