CodeIgniter Forums
Bool in session is turned into string - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Bool in session is turned into string (/showthread.php?tid=23281)

Pages: 1 2


Bool in session is turned into string - El Forum - 10-06-2009

[eluser]überfuzz[/eluser]
I did a login system, quite simple but its dandy. But looking through the code I found something puzzling.

In the function setting the session-value to state logged in.
Code:
$data = array(
    'username' => $this->input->post('username'),
    //etc...
    'logged_in' => TRUE //this would be the boolean.
);
$this->session->set_userdata($data);

In the my MY_Controller I only just realized that I was checking logged_in like this:
Code:
if($this->session->userdata('logged_in') != TRUE ) //not !==
{
    redirect('login');
}
!== didn't work so I trowed a
Code:
var_dump($this->session->userdata('logged_in')); //output string(1) "1"

Why is the bool turned into a string?

Edit, a glitch pointed out by bigtony plus != TRUE


Bool in session is turned into string - El Forum - 10-06-2009

[eluser]bigtony[/eluser]
[quote author="überfuzz" date="1254830067"]Why is the bool turned into a string?[/quote]
It may be happening when the session data is written to the cookie or serialized to the db (if you're using db sessions).
Also, noticed that you set 'logged_in' but were testing 'is_logged_in' instead.


Bool in session is turned into string - El Forum - 10-06-2009

[eluser]Phil Sturgeon[/eluser]
Yeah it does that.

Everything is converted into a string or an array of strings based on this "private" method in the Session library.

Code:
if (is_array($data))
        {
            foreach ($data as $key => $val)
            {
                $data[$key] = str_replace('\\', '{{slash}}', $val);
            }
        }
        else
        {
            $data = str_replace('\\', '{{slash}}', $data);
        }

        return serialize($data);
    }

You can use typecasting to get them back to their correct types:

Code:
if((int) $this->session->userdata('logged_in') !== 1 )
{
    redirect('login');
}

That's just an example of how to convert the type. In this specific case I would just stick with:

Code:
if(!$this->session->userdata('logged_in'))
{
    redirect('login');
}



Bool in session is turned into string - El Forum - 10-06-2009

[eluser]überfuzz[/eluser]
[quote author="Phil Sturgeon" date="1254835753"]Yeah it does that.

Everything is converted into a string or an array of strings based on this "private" method in the Session library.
[/quote]

Ok, good to know! Typical php-laziness or is there a reason for this?


Bool in session is turned into string - El Forum - 10-06-2009

[eluser]n0xie[/eluser]
[quote author="überfuzz" date="1254835971"][quote author="Phil Sturgeon" date="1254835753"]Yeah it does that.

Everything is converted into a string or an array of strings based on this "private" method in the Session library.
[/quote]

Ok, good to know! Typical php-laziness or is there a reason for this?[/quote]
PHP is dynamically typed. It is not a bug, it is a feature.


Bool in session is turned into string - El Forum - 10-06-2009

[eluser]überfuzz[/eluser]
[quote author="n0xie" date="1254837163"][quote author="überfuzz" date="1254835971"][quote author="Phil Sturgeon" date="1254835753"]Yeah it does that.

Everything is converted into a string or an array of strings based on this "private" method in the Session library.
[/quote]

Ok, good to know! Typical php-laziness or is there a reason for this?[/quote]
PHP is dynamically typed. It is not a bug, it is a feature.[/quote]
Exactly, and shifting types back and fort without knowing it is something I haven't seen in any other language.


Bool in session is turned into string - El Forum - 10-06-2009

[eluser]n0xie[/eluser]
Then you haven't worked much with dynamically typed languages.

For most dynamically typed language it works the same (at least the ones I am familiar with: PHP, Python, Ruby, Perl and TCL ):
Code:
(int) 0 = (string) '0' = (float) 0.00 = (bool) FALSE
(int) 1 = (string) '1' = (float) 1.00 = (bool) TRUE



Bool in session is turned into string - El Forum - 10-06-2009

[eluser]sl3dg3hamm3r[/eluser]
Most scripting languages are not type-safe. I for myself sometimes wish more type-safety. It makes a language simply more robust, less error prone. Combined with some implicit converting, it is a good thing!


Bool in session is turned into string - El Forum - 10-06-2009

[eluser]überfuzz[/eluser]
[quote author="n0xie" date="1254838447"]Then you haven't worked much with dynamically typed languages.

For most dynamically typed language it works the same (at least the ones I am familiar with: PHP, Python, Ruby, Perl and TCL ):
Code:
(int) 0 = (string) '0' = (float) 0.00 = (bool) FALSE
(int) 1 = (string) '1' = (float) 1.00 = (bool) TRUE
[/quote]
Resumé: I started out with C++ and JAVA. I pretty sure C++ was type-safe, not sure of JAVA. But it was consider veeeeery bad practice to alter types. Teacher vent: >:-(


Bool in session is turned into string - El Forum - 10-06-2009

[eluser]BrianDHall[/eluser]
[quote author="überfuzz" date="1254842539"][quote author="n0xie" date="1254838447"]Then you haven't worked much with dynamically typed languages.

For most dynamically typed language it works the same (at least the ones I am familiar with: PHP, Python, Ruby, Perl and TCL ):
Code:
(int) 0 = (string) '0' = (float) 0.00 = (bool) FALSE
(int) 1 = (string) '1' = (float) 1.00 = (bool) TRUE
[/quote]
Resumé: I started out with C++ and JAVA. I pretty sure C++ was type-safe, not sure of JAVA. But it was consider veeeeery bad practice to alter types. Teacher vent: >:-([/quote]

Yeah, this is one of those love-it-or-hate-it things. PHP doesn't really care what type a variable is unless you call for type-casting. I've only run into it a couple of times in the last five years, but if you aren't aware of the functioning it can be really hard to figure out what is up with the magic.

If you just assume everything is a string or integer and rely on PHP's own evaluation of what is true and false rather than trying to be very explicit about what you are looking for you tend to almost never run into the problem.

But when you are used to lower level languages like C++ or Java its hard not to try to be thinking about what type a variable is. One of the upsides/downsides of coding in PHP is you tend to just not think about it.