Page 200 - Open Soource Technologies 304.indd
P. 200
Web Technologies-I
Notes fwrite($fp, $s);
fclose($fp);
// page2.php:
// this is needed for the unserialize to work properly.
include(“classa.inc”);
$s = implode(“”, @file(“store”));
$a = unserialize($s);
// now use the function show_one() of the $a object.
$a->show_one();
?>
If you are using sessions and use session_register() to register objects, these objects are serialized
automatically at the end of each PHP page, and are unserialized automatically on each of the
following pages. This basically means that these objects can show up on any of your pages once
they become part of your session.
It is strongly recommended that you include the class definitions of all such registered objects
on all of your pages, even if you do not actually use these classes on all of your pages. If you
do not and an object is being unserialized without its class definition being present, it will lose
its class association and become an object of class stdClass without any functions available at
all, that is, it will become quite useless.
The Log.inc file
<?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(“Cannot open {$this->filename}”);
}
function write($note)
{
fwrite($this->fp, “$note\n”);
} function read( )
{
return join(‘’, file($this->filename));
}
function _ _wakeup( ) { $this->open( ); }
function _ _sleep( )
{
194 LOVELY PROFESSIONAL UNIVERSITY