Welcome Guest, Not a member yet? Register   Sign In
Bool in session is turned into string
#11

[eluser]überfuzz[/eluser]
[quote author="BrianDHall" date="1254857310"][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.[/quote]
Ok, I vote downside. We just got a clear case where a core operator was used to check value a-n-d type. Is there any one sitting on the info about a reason why every session-value is turned into a string in CI?

Edit a earlier post, teacher allowed type being altered, IN A CONTROLLED FASHION. Furthermore, this tells us that JAVA i type dynamic, don't it. ;-)
#12

[eluser]BrianDHall[/eluser]
[quote author="überfuzz" date="1254865948"]Ok, I vote downside. We just got a clear case where a core operator was used to check value a-n-d type. Is there any one sitting on the info about a reason why every session-value is turned into a string in CI?

Edit a earlier post, teacher allowed type being altered, IN A CONTROLLED FASHION. Furthermore, this tells us that JAVA i type dynamic, don't it. ;-)[/quote]

For the 'why', Phil posted the code as to when it happens. All session variables are fed to str_replace, which explicitly returns a string - so the boolean is cast to a string. Actually, everything is cast to a string because of str_replace().

On Java, actually I believe it isn't type dynamic (working off memory here, it's been years), you must tell it what type you want if it isn't the default that the object returns. So if you want a string you have to call Object.toString, etc - everything in Java is an object, I believe even Strings are objects. In PHP, everything is most certainly not an object.

PHP manages references and variable types at run time attempting to maximize performance and minimize the need to ever consider what type a variable is.

PHP is usually very smart about this, thus the rarity of problems - but CI is calling a PHP function that, by its very nature, takes a string (and will cast a provided variable to a string if it isn't a string) and returns a string. Thus your frustration in the magically changing types.

I suppose you might make the case that this is actually a bug, and CI should check to make sure something is a string before running str_replace() - thereby converting it to a string even if it shouldn't be.
#13

[eluser]überfuzz[/eluser]
[quote author="BrianDHall" date="1254866497"]I suppose you might make the case that this is actually a bug, and CI should check to make sure something is a string before running str_replace() - thereby converting it to a string even if it shouldn't be.[/quote]

No, but I'm very close to scrabbling a mp about that it should be stated in the users_guide. Please don't say that it is and that I missed out on it...
#14

[eluser]n0xie[/eluser]
you could always explicitly cast it yourself if you feel so inclined.

Code:
$logged_in = (bool) $this->session->userdata('logged_in');
if($logged_in === FALSE ) //the '=' operator explicitly checks if it is a boolean
{
    redirect('login');
}
#15

[eluser]BrianDHall[/eluser]
[quote author="überfuzz" date="1254869742"][quote author="BrianDHall" date="1254866497"]I suppose you might make the case that this is actually a bug, and CI should check to make sure something is a string before running str_replace() - thereby converting it to a string even if it shouldn't be.[/quote]

No, but I'm very close to scrabbling a mp about that it should be stated in the users_guide. Please don't say that it is and that I missed out on it...[/quote]

Actually...I kind of really think it is a bug. Something should really only be run through str replace if is_string() comes back true. This would allow floats, integers, and boolean values to remain unmolested through the CI session process.

Really, this is not really expected behavior - PHP can freely play with variables, but there isn't really a good reason to explicitly caste everything to string form.
#16

[eluser]überfuzz[/eluser]
[quote author="n0xie" date="1254886608"]you could always explicitly cast it yourself if you feel so inclined.

Code:
$logged_in = (bool) $this->session->userdata('logged_in');
if($logged_in === FALSE ) //the '=' operator explicitly checks if it is a boolean
{
    redirect('login');
}
[/quote]
Now that I'm aware about this dodgy behavior I wouldn't say I'm the one inclined. More so every user that likes to keep the code tidy, not knowing about this issue.
#17

[eluser]Phil Sturgeon[/eluser]
It's a bug but not one that will make life tricky.

Code:
echo "1" == 1; // true
echo "1" === 1; // false

Just be careful.
#18

[eluser]überfuzz[/eluser]
[quote author="Phil Sturgeon" date="1255035570"]It's a bug but not one that will make life tricky.

Code:
echo "1" == 1; // true
echo "1" === 1; // false

Just be careful.[/quote]

Not sure if what we chose to call it makes any difference. Types are changes in a forced way. It should be stated in the users_guide. Thast would make life less tricky. Confusedmirk:
#19

[eluser]Unknown[/eluser]
I have also enabled db stored sessions and i was standing in front of the same problem like the thread author.

I checked the session Library and it's the str_replace of the backspace.

My quick&dirty;solution was adding a if statment with preg_match and only replace backspace if needed.

Replace in the method _serialize
Code:
$data[$key] = str_replace('\\', '{{slash}}', $val);
with
Code:
if (preg_match('/\\\\/', $val)) {
    $data[$key] = str_replace('\\', '{{slash}}', $val);
} else {
    $data[$key] = $val;
}

I also added the opposite to the _unserialize method...

Hope it's useful for somebody and i hope the ci team fix this bug also in the next release.
I also posted on the open Bug Report the thread author opened.

btw great work guys for this framework! keep on rocking ^^
#20

[eluser]Phil Sturgeon[/eluser]
This is fixed in 2.0:




Theme © iAndrew 2016 - Forum software by © MyBB