5

I've summarized the crux of problem to be as brief as possible:

A simple script:

<?php
session_start();

$_SESSION['user']="logged";

then overwrite

$_SESSION['user']=0;  

and show $_SESSION contents

var_dump($_SESSION);

shows $_SESSION['user'] is '0' - sure since it's just been overwritten

BUT now watch

if ($_SESSION['user']=="logged"){
    echo "logged";
}
else{
    echo "unlogged";
}

outputs "logged"....

Seems the change of variable type is only superficial - I've no idea what I'm doing wrong.. Do I need to use the === comparison to include checking the type?

Joey
  • 409
  • 1
  • 3
  • 11

2 Answers2

4

Exactly, you need to do strict comparison ===

That is because PHP try convert your string in a number so "logged" pass to be 0

and then 0 == 0

  • (int) "logged" = 0
  • (int) "1logged" = 1
  • (int) "logged1" = 0

http://www.php.net/manual/en/language.types.type-juggling.php

Maks3w
  • 6,014
  • 6
  • 37
  • 42
1

That is normal because:

(int)"logged" = 0

so 0 = 0

you need to do strict comparison

if ($_SESSION['user'] === "logged")
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
  • Maks3w and Mihai, do I understand correctly that when trying to overwrite a string value with an int Php converted the str value to int ((int) "logged" = 0)? It should happed with variables not values as I understand it...? I'm confused.. – Joey Sep 11 '12 at 07:33
  • It tries to convert both variables left and right, right is already `INT` and it tries to get both to same type. – Mihai Iorga Sep 11 '12 at 07:37
  • It has always seemed to me most logical that $my_array['key1']=1 should overwrite any string value that has been on $my_array['key1'] before and not create a variable from that string and convert it to int 1.. Need to give it a thought, thx for replies! – Joey Sep 11 '12 at 07:42
  • if you do `$my_array['key1']='1';` then yes, it will overwrite with a string as `'1'` is a string.., but `0` is an integer. – Mihai Iorga Sep 11 '12 at 07:43
  • ok so coming back to my initial example, the str "logged" from $_SESSION array has become a variable $logged converted to int = 0 after overwriting? That would explain why $_SESSION['user']=="logged" is TRUE but it's so confusing to me, since str "logged" should be erased/wiped out after overwriting.. – Joey Sep 11 '12 at 07:54
  • But it is wiped out ... try comparing `"mywipeoutlogged"` to `0` => `if ($_SESSION['user'] == "mywipeoutlogged")` it will result in same issue – Mihai Iorga Sep 11 '12 at 07:55
  • so "mywipeoutlogged" has become wiped out thus with 0 value when converted to int, right? But that would mean that the string "mywipeoutlogged" has been turned into $mywipeoutlogged variable.. is my thinking correct..? – Joey Sep 11 '12 at 08:04
  • It is not turning into a variable, it's just converting from string to int. The `if` statement results: `if (0 == (int)"mywipeoutlogged")` => `if (0 == 0)`. On a strict comparison it becomes `if(string === int)` so they are different, it doesn't convert anything anymore, it just strict compares. – Mihai Iorga Sep 11 '12 at 08:07
  • OK, I'm home:) I understood 'if ($_SESSION['user']=="logged")' as 'check whether str "logged" resides on the key 'user' in $_SESSION array' WHILE it goes 'check whether 0 == str "logged" (after converting "logged" to int to do the comparison)'; the table makes it even clearer http://www.php.net/manual/en/types.comparisons.php; sometimes it's difficult to return to right perspective if you look at sth from a wrong angle:) THX again! – Joey Sep 11 '12 at 08:57
  • Yes .. no problem. Glad you understood. :) – Mihai Iorga Sep 11 '12 at 08:58
  • The funny thing is it's actually a trivial thing and how could I follow that trait of thinking.. 'take perspective' is the lesson, looks like we learn all life:) – Joey Sep 11 '12 at 09:06