Problem with: $ip_Address = $_SERVER['REMOTE_ADDR'];

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
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Problem with: $ip_Address = $_SERVER['REMOTE_ADDR'];

Post by mikebr »

I am trying to build some secure checking into a login script that I use as an include at the start of each page in a members area and can't seem to get a value returned for $ip_Address, but if I use the code as:

Code: Select all

<?php
$ip_Address = $_SERVERї'REMOTE_ADDR'];
echo "$ip_Address";
?>
I get an address returned, can anyone see what I'm doing wrong here? the include script is as follows:

Code: Select all

<?php
session_start();

$goLogin = "./userlogin.php";

$ip_Address = $_SERVERї'REMOTE_ADDR']; // NO VALUE RETURNED
#
########################### - CHECK THE IP ADDRESS IS THE SAME AS THE USERS AND THE VARIABLES ARE SET FOR THE SESSION
#
# - Check if the IP Address is set but it is not that of the session

$notHighjacked = isset($_SESSIONї's_ipAddress'])
	 		       && ($_SESSIONї's_ipAddress'] = $ip_Address);

# - Check if all the session variables are set

if ($notHighjacked && isset($_SESSIONї's_ipAddress']) 
			    && isset($_SESSIONї's_c_login']) 
		 	    && isset($_SESSIONї's_client_ID'])
		 	    && isset($_SESSIONї's_c_password'])) {
		
$authorized = true;
}
				
# Check if the $highjack flag is true or the $authorized flag is false

if(!$notHighjacked) {
# - The request did not originate from the machine that was used to create the session

// email code here

$logintext = "You are not authorized to access this area, your IP address of " . $ip_Address . " has been recorded."; // NO VALUE RETURNED FOR $ip_Address
($_SESSIONї'logintext'] = $logintext);
				
// Relocate back to the login page
header("Location: " . $goLogin);
exit;
}
else if(!$authorized)
{

// The user has not logged in

$logintext = "You need to log in.";
($_SESSIONї'logintext'] = $logintext);

# RELOCATE BACK TO THE LOGIN PAGE
header("Location: " . $goLogin);
exit;
}
# - ALL IS OK SO LET THE USER IN
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

can't see the errorm but do you really want to set $_SESSION['s_ipAddress'] by
$notHighjacked = isset($_SESSION['s_ipAddress'])
&& ($_SESSION['s_ipAddress'] = $ip_Address);
?
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post by mikebr »

Oh yes, thanks for pointing that out.

The variable thing is weird, if I change the lines of

Code: Select all

<?php
$logintext = "You are not authorized to access this area, your IP address of " . $ip_Address . " has been recorded."; // NO VALUE RETURNED FOR $ip_Address 
($_SESSIONї'logintext'] = $logintext); 
?>
and make it

Code: Select all

<?php
echo "You are not authorized to access this area, your IP address of $ip_Address has been recorded.";
?>
I get a value returned for $ip_Address but I don't if used as the first option?
Post Reply