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
}
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With