views:

105

answers:

2

Hello All,

I have weirdest problem with PHP itself, that I've ever seen.

Setup:

PHP 5.33, (tried PHP 5.2.14 as well) under IIS

Problem: PHP deletes all session data as soon as I put exclamation point into a key in session array.

Example:

session1.php

session_start();
$_SESSION["foo"] = 'test';
header('Location: session2.php');

session2.php

session_start();
var_dump($_SESSION);
die();

Works fine, I see variable data printed out.

array(1) { ["foo"]=>  &string(4) "test" } 

But if I change line in first file to be

$_SESSION["foo!"] = 'test'; 

or

$_SESSION["f!oo"] = 'test'; 

I mean if I add exclamation point -then the $_SESSION array is empty when I get to second file

array(0) { } 

I thought this is a buggy version of PHP when I was on 5.2.14, but upgrade didn't help. I don't even know what the problem might be. Maybe this has something to do with Windows setup, or IIS?

Any ideas?

+1  A: 

well what is your question about? what is the reason of this error or how to avoid it?
A first one is probably because of some odd PHP internals. For example, you cannot use numeric keys with same consequences.
A latter one is even simpler - do not put exclamation point into a key in session array.

As I recall, PHP sessions mechanism came up from the PHPLib - a first PHP framework ever. Written by some volunteer students. Not a very optimal one. Once added to PHP in the version 4.0, become usable only at 4.1 but still with some odd legacy, like register_globals support. The latter one is most likely the reason of your problem. $_SESSION array key must be valid PHP variable name, in sake of that ancient register_globals behavior where session variables become global PHP variables.

Col. Shrapnel
register_globals has been deprecated in 5.3. cbglum is using 5.3.3
stillstanding
@stillstanding so what?
Col. Shrapnel
I turned register_globals off first thing after I noticed the problem. Now I'm more convinced this is more of IIS, or Win issue, but I don't have any idea how to fix the issue without leaving this server. Any hints?
cbglum
@cbglum If I'm right with my guess, this is not *setting* problem, but PHP core code problem. By changing a setting you don't change a code itself, it remains the same. With all it's bugs intact. A hint I've told you already: DO NOT use ! mark in the key name. At least make it one level deeper, `$_SESSION["cart"]["f!oo"] = 'test';` but I still don't understand why you insists on using this odd name
Col. Shrapnel
+1  A: 

It's not PHP, so it must be Windows. I get the proper output using MAMP on my Mac with your exact code:

array(1) { ["foo!"]=>  string(4) "test" } 

Solution? Switch operating systems and use Apache. Then you will not have to deal with Microsoft telling you what you can and cannot do. In addition, your costs will be greatly reduced. (Linux is free)

cdburgess