tags:

views:

203

answers:

5

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

+1  A: 

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.

Jon Purdy
+1  A: 

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.

Will Vousden
+3  A: 

Try

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

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
ThiefMaster
NO I dnt need to restrict all visitors to certain IPs
santanu
+1  A: 

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.

ThiefMaster
Thank you for your answer. but i am confused in one thing that how would I expired the session if someone is inactive?I mean how would I get to know ?
santanu
Whenever a logged in user does something, you store the timestamp.Then you can ignore inactive sessions (`(time() - lastactivity) > delay`) and/or delete them easily.
ThiefMaster