RE: Sessions - Bug?

From: Date: Mon, 28 Aug 2000 00:28:26 +0000
Subject: RE: Sessions - Bug?
References: 1  Groups: php.windows 
Request: Send a blank email to php-windows+get-2192@lists.php.net to get a copy of this message
Completed more testing and found weird behaviour. I'm very new to all of this so I'm probably the one in the wrong but surely the following is a bug? <?php session_start(); session_register(MyVariable); $MyVariable = 1; echo "Variable is: $MyVariable<br>\n"; ?> Everything occurs as expected. "Variable is: 1" is displayed onscreen and the session file contains: MyVariable|i:1; Next run this as a separate PHP script. <?php session_start(); echo "Variable before change is: $MyVariable<br>\n"; $MyVariable = "2"; echo "Variable after change is: $MyVariable<br>\n"; ?> Again everything runs as expected. The following output is displayed onscreen: Variable before change is: 1 Variable after change is: 2 The contents of the session file contains: MyVariable|s:1:"2"; Now for the problems! Modify the above script to: <?php session_start(); echo "Variable before change is: $MyVariable<br>\n"; $MyVariable = "3"; // MAKE SURE YOU CHANGE THIS TO 3!!!!! echo "Variable after change is: $MyVariable<br>\n"; session_unset(); // have to use session_unset() because session_unregister() DOESN'T work (the global variable is still accessible) session_destroy(); ?> Now run the modified script (above). What would you expect the output to be? This is the screen output you should get: Variable before change is: 2 Variable after change is: 3 This looks correct BUT now look at the session file. My understanding is that it should be 0 bytes, i.e. no variables. However, it contains MyVariable|s:1:"2"; Now this is weird. If it isn't 0 bytes then the next obvious thing should be that the variable contain the value "3"? When we changed the value above from 1 to 2 and DIDN'T destroy the session the value was correctly updated in the session file. So this is either a) A bug, b) I'm going mad or c) I'm being really stupid and messing up in a big way! The only thing I can think of is that session_unset() reverses any changes made to the session variables since the session was opened on that page? However, if you remove the session_unset() command from the script the same thing still occurs. With just session_destroy() the session file still contains the variable value "2". Please help..... Please make sure you are running PHP on Windows (NT 2000 would be best!) as the Unix version seems to work fine(?). Thanks James PS: Please note that all commands work properly if they are called in the same script that originally started the session. If you run session_unset it removes all global variables and resets all variable values in the session file to null. If you run session_destroy() it removes all variables from the session file and makes it 0 bytes. Why do the commands work properly here and not from scripts run later? -----Original Message----- From: Klaus Schmidt [mailto:ks@ksweb.de] Sent: 27 August 2000 19:09 To: James Duncan Cc: php-windows@lists.php.net Subject: AW: Sessions yes, it the same here on WIN98,Apache 1.3.12, PHP4.0.2dev (based on the actual file from php.net) I tried the following and the Server ist clean, no 'orphan'-session-files hope it works <? session_start(); $id=session_id(); // I didn't try so set this variables 'behind' the $path=session_save_path(); // _unset() and _destroy ...up2u $file = $path . "sess_" . $id; echo $file."<br>"; @session_destroy(); // shows a warning without that '@' :-)) an doesn't destroy the file // but without that line the unlink() don't works. // so it seems that any important internal link/index is destroyed! echo unlink($file); // success ?> outputs: C:\usr\htdocs\ksweb\sessions\sess_6a8f3ec2809f5fa3b2a0975ab3191629 1 cu Klaus p.s.: here a important text from php.net.manual: (hope you know that) theo@alldarlings.com 09-Jan-2000 01:54 <PRE> What session_start does is looks for an existing session, and if none exists, it creates a new one. What it also does (that wasn't fully eplained above by the statement "initializes variables" is looks for any session variables, and sets them globally on the page that start got called from. If you have any variables already set (say from a get/post from the previous page) they will get overwritten. Also, changing a variable on a page will not update its session counterpart. Also, note that any call to session_register will automatically call session_start (and therefore pull all session variables and set their local counterparts.) If I wasn't so lazy, I'd upload an example of what I mean. </PRE>

« previous php.windows (#2192) next »