compare $_SSSION['ip'] with real ip

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

compare $_SSSION['ip'] with real ip

Post by Think Pink »

hellow.
i have a login page where in login.php i have:

Code: Select all

<?php
$_SESSION['ip'] = $_POST['ip'];
?>
in auth.php I check to see

Code: Select all

<?php
$new_ip = getenv("REMOTE_ADDR"); 
if($_SESSION['ip'] != $new_ip)
{
   die ("Not logged in");
}
?>
the problem is that if the user is behind a proxy, I won't be able to see his real ip.
So for this I have the following function:

Code: Select all

<?php
function getIP() {
$ip;
if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
else $ip = "UNKNOWN";
return $ip;
}
?>
my question is :
How can I check now in auth.php if $_SESSION['ip'] is the same with the ip provided by the function?

thx
jakobdoppler
Forum Commoner
Posts: 46
Joined: Wed May 21, 2003 6:16 pm

Post by jakobdoppler »

What about that ?
_yak

Code: Select all

<?php
if($_SESSION['ip'] != getIP()) 
...
?>
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

compare $_SSSION['ip'] with real ip

Post by Think Pink »

i'm not that stupid
i tried that and is not wotking. i get a parse error.
Post Reply