[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] [PHP] Serialisering af objekt i session



Ole Kofoed Hansen wrote:

Jeg tror dit problem skyldes, at sessionen indlæses før klassedefinitionerne. Det advarer dokumentationen i alt fald om: http://dk2.php.net/session

Klassedefinitionen har hele tiden stået før sessionsstarten....

Du skulle kunne løse det ved enten eksplicit at inkludere filen med klassedefinitionen eller ved at definere en __autoload() inden du kalder session_start().

Der er noget i det du siger her. Hvis jeg "sikrer" at følgende sider efter initiering af objektet ikke kender til klassedefinitionen får jeg nøjagtig samme fejl, som hvis jeg forsøger at sikre at de gør. Jeg troede indtil nu at mit problem "kun" var at objektet ikke blev båret videre gennem sessionen.

Og du bør bruge $_SESSION i stedet for session_register().

Jeg kan simpelthen ikke finde nogle eksempler på hvordan man ville tildele en af pladserne i $_SESSION array'et et objekt. Kan du hjælpe?

For at sikre at vi taler om det samme er her min eksempelkode indtil videre (med session_register()):

________________firstpage.php____________________________
<?php
include_once('Log.inc');
session_start();

print "<html>\n";

$now = strftime("%c");

if(!session_is_registered('l'))
{
  $l = new Log('/tmp/persistentlog_' . session_id());
  session_register('l');
  $l->write("Created at $now");
}
$l->write("Viewed first page at $now");
print 'This is in the log file:<br />';
print nl2br($l->read());
print '<br />';
print '<a href="secondpage.php">Next page</a><br />';

print "</html>\n";

?>
____________________________________________________________





_______________secondpage.php_______________________________
<?php
include_once('Log.inc');
session_start();

print "<html>\n";

$now = strftime("%c");

$l->write("Viewed second page at $now");
print 'This is in the log file:<br />';
print nl2br($l->read());

print "</html>\n";

?>
______________________________________________________________



__________________Log.inc______________________________________
<?php

class Log
{
  var $filename;
  var $fp;

  function Log($filename)
  {
    $this->filename = $filename;
    $this->open();
  }

  function open()
  {
    $this->fp = fopen($this->filename, "a") or
      die("Unable to open file '" . $this->filename . "'");
  }

  function write($note)
  {
    fwrite($this->fp, "$note\n");
  }

  function read()
  {
    return join('', file($this->filename));
  }

  function __wakeup()
  {
    $this->open();
  }

  function __sleep()
  {
    fclose($this->fp);
    return array('filename');
  }
}

?>
___________________________________________________________


Med venlig hilsen
David List


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2008-02-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *