Jump to content


Photo

Wierd Session Problem


  • Please log in to reply
2 replies to this topic

#1 jbreits

jbreits
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 29 June 2006 - 04:51 PM

I had a strange session problem crop up a couple days ago. I was able to finally nail it down, and I think I may have fixed it, but I'm not sure why it has happened. Basically, my sessions were working great, although I wasn't storing much in them other than user name and user id (which are obviously only set if the login is correct). Well I decided I needed a new session var that would be used on every page whether logged in or not. At the end of each page I put the following:

$_SESSION['LastMessage'] = 'no message';

When I did that, REALLY strange things started to happen. When I would load up some pages, they would load half-way, and the reload, again only making it halfway before stopping. Some pages just would come up blank and some would show an error. I couldn't figure the thing out. I started trying different variable names and there were others that worked and others that didn't work.

$_SESSION['Last'] = 'no message';  // works
$_SESSION['LastM'] = 'no message';  // works
$_SESSION['LastMe'] = 'no message';  // works
$_SESSION['LastMes'] = 'no message';  // works
$_SESSION['LastMessage'] = 'no message';  // works

I was going nuts. I figured that since I had written my own session handling funcitons, that maybe they were the problem. I switched to PHP standard session and it worked fine. So I nailed it down to my functions. Long story short, I eventually nailed it down the the session write handler. In there, I call date('Y-m-d H:i:s') in order to populate the LastAccessed field in my sessions table. Turns out that it was the call to date that was messing the whole thing up. Took that out and it worked fine.

So I set about trying to figure out how to get it to work so I could use it. The manual mentioned something about classes being destroyed before sessions so if the session write method cannot access a class. I didnt assume that date() was a class, but just to test I put a session_write_close() at the end of my page rather than just letting the system close it and it WORKED!

Is date() really a class? I am wondering if it this fixed for good or if it will crop up again on a different variable name. Not really sure why some var names worked and some did not. Here is very simplified code that can be used to reproduce the error. I was using PHP 5.1.2 on WinXP.

Index.php
<?php
error_log ('page load start'.$VarName);
include_once ('session.php');
StartSession();
?>



<html>

<head>
</head>

<body class="Main">

here we are!!!!

<br>
PHP 5.1.2<br>
WinXP

</body>

</html>

<?php
  $_SESSION['LastMessage'] = 'no message';
  //session_write_close();
  error_log ('page load end');
?>

session.php:
<?php


function open($save_path, $session_name) 
{
 return true;
}

function close() 
{
 return true;
}

function read($id) 
{
  return true;
}

function write($id, $sess_data) 
{
  error_log ('sess_data: '.$sess_data);
  error_log ('writing '.date('Y-m-d H:i:s'));
  error_log ('writing dummy date');
  return true;
}

function destroy($id) 
{
 return true;
}

function gc($maxlifetime) 
{
 return true;
}

function StartSession ()
{
  error_log ('start session'.$VarName);
  session_set_save_handler("open", "close", "read", "write", "destroy", "gc");  
  session_start();      
}
?>



#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 05:10 PM

If you read the manual carefully, read the NOTE. Most of your bugs solve there.

First of all date() is a function, it returns a string.

in order to store $var in session, this include class, session must know the definition of this class before it's start.

say you have class called A, and it is defined in A.php

//not working:
session_start();
include('A.php');
$var = new A();
$_SESSION['var'] = $var;


// working
include('A.php');
session_start();
$var = new A();
$_SESSION['var'] = $var;


notice that order of session_start is different in 2 cases.
an alternative to this problem is looking at serialize() and unserialize()

Life's too short for arguing.

#3 jbreits

jbreits
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 29 June 2006 - 06:42 PM

Thanks for the reply, but what you wrote really has nothing to do with the issue I was describing. And I did read the manual.

I was not trying to store or retrieve any classes in my session, nor is my session a class. In the example posted, there is nothing but a few simple functions and variables. What I didn't understand was why the date() function was causing these problems. The only way I could get it to work was to manually close the session before the page ended. And according the manual, the reason you would do this is if you were trying to access classes in the session write handler, which I am not.

The only thing I could think of is that the date() function was part of a class (that I didn't know about) and that class was being freed before writing to the session. That would kind of explain the flaky behavior of it working sometimes and sometimes not, because sometimes the memory location might still have some valid data in it where as other times it may not. Either that, or PHP is destroying other things besides classes before it writes the session.

Any thoughts?

JBreits




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users