Page 1 of 1

PHP Sessions, Objects and Errors

Posted: Mon Mar 13, 2006 11:16 am
by dakini
Hi,

I am experiencing an intermittant error "Call to a member funtion on a non object" using php objects as session variables.

My program structure stores the customer object as session variables. For instance, when a customer logs in I generate a customer object and store this as their session variable (with name, email etc etc). This gives me easy access to the information I need, when I need it.

Every now and then the error "Call to a member function on a non object" occurs. However, this is intermittant and does not follow any pattern.

I have experience this error when a customer continually refreshes the page. In this instance the session var is lost and therefore the object error occurs.

My quesion is, how can I get round a problem such as this?

Here is an example..

$cust= unserialize($_SESSION['cust']);
$cust->doSomthing();
$cust= serialize($cust);

If the client refreshes the page whilst the function doSomthing() is called, the session var is lost.

I have started looking at sql optimisation for faster code excecution.

Really, I need some advise on this? How can I ensure this error does not occur? Should I recover the session?

Many thanks, in advance

Dak
:)

Posted: Mon Mar 13, 2006 11:58 am
by feyd
you don't need to serialize the object, the session handlers do that for you. If you want, you can use is_object() and/or is_a()

Posted: Mon Mar 13, 2006 12:04 pm
by dakini
Is there anything else I need to do if I don't serialise the object?

It is a session object variable.. I thought this was standard practice???

So.. I declare my customer session object as follows

$_session['customer'] = $customer;

When I need to use this object, I use $customer = unserialise($_SESSION['customer']);

I use it an thats it????

Im sure I get errors if I don't serialise it having used it.. Is there any php config settings I need to change to enable this?

Any chance you can show me an example?

Thanks

Dak

Posted: Mon Mar 13, 2006 12:24 pm
by feyd
So long as your class is defined prior to starting the session, you don't need to serialize or unserialize anything regarding stored variables in the session.

class.foo.php

Code: Select all

<?php

class foo
{
  var $foo = '';

  function bar()
  {
    $this->foo = debug_backtrace();
  }
}

?>
page1.php

Code: Select all

<?php

include('class.foo.php');

session_start();

$_SESSION['foo'] = new foo();

$_SESSION['foo']->bar();

?>some output junk and a link to page2.php which is followed
page2.php

Code: Select all

<?php

include('class.foo.php');

session_start();

$foo =& $_SESSION['foo'];

print_r($foo->foo);

?>