Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

harden php sessions - comparison error

Tags:

php

session

I'm trying to harden up my sessions and found the code below. My question is this line isset($_SESSION['last_ip']) !== $_SERVER['REMOTE_ADDR'].

When I echo out the comparison the IP numbers are the same yet that line of code compares the two to be different. If i compare it as != then the comparison works. Why is that? Shouldn't both values be totally identical? Any suggestions how can I fix it so they are ===?

ini_set('session.cookie_httponly', true);
session_start();
if ( isset($_SESSION['last_ip']) === false ) {
    $_SESSION['last_ip'] = $_SERVER['REMOTE_ADDR'];
}
if ( isset($_SESSION['last_ip']) !== $_SERVER['REMOTE_ADDR'] ) {
    echo $_SESSION['last_ip'] . ' / ' . $_SERVER['REMOTE_ADDR']; // the output is identical
}
like image 738
t q Avatar asked Dec 19 '12 01:12

t q


2 Answers

if ( isset($_SESSION['last_ip']) !== $_SERVER['REMOTE_ADDR'] ) {

You're checking to see if one is set and the other matches that boolean value. Remove the isset.

like image 101
Waleed Khan Avatar answered Oct 03 '22 14:10

Waleed Khan


Personally, i'll be solving this with AND operator in the IF sequence such as:

if ( isset($_SESSION['last_ip']) && $_SESSIOn['last_ip'] != $_SERVER['REMOTE_ADDR'] ) {

Does this helps?

like image 34
Jakub Strzadala Avatar answered Oct 03 '22 14:10

Jakub Strzadala