IP based authentication.

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

IP based authentication.

Post by redhair »

Code: Select all

<?php
   //check ip and decide who is admin.

   $ip_visit = getenv("REMOTE_ADDR"); 


   $ip_array[] = "127.0.0.1"; 
   $ip_array[] = "192.168.1.1"; 
   $ip_array[] = "192.168.1.2"; 

   foreach ($ip_array as $ip_adm) 
   { 
      if (preg_match("/$ip_visit/","$ip_adm")) 

      { 
         $is_admin = "1"; 
         session_register(is_admin); 
      } 
   } 

?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Unfortunately you can't use IP for authentication.

(1) dynamic IPs (and AOL users might change IP within the same session)
(2) NATs
(3) IP spoofing
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Might be okay for an Intranet though?! Certain machines have access to certain pages.

Mark
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Yes - wasn't thinking about intranets.
User avatar
jad
Forum Newbie
Posts: 1
Joined: Fri Feb 27, 2004 8:58 am
Location: Middle East - Jordan
Contact:

Post by jad »

hi folks
Well, I think it could be a great implementation to avoid session hijacking.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

simplified version:

Code: Select all

$_allowed=array(
 "127.0.0.1",
 "192.168.1.1",
 "192.168.1.2"
);

if(in_array($_SERVER["REMOTE_ADDR"],$_allowed))
  $_SESSION["is_admin"]=true;
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

[quote="Weirdan"]simplified version:[/quote]

Better :)
Post Reply