Jump to content

Serialize Not Working In Php


toreachdeepak

Recommended Posts

Firstly index1.php is called to serialize and then index2.php is called to unserialize. But index2.php is not showing the content============================File1 - index1.php<?php class xyz123 { public $one = 1; public function show_one() { echo $this->one; } } $a = new xyz123; $s = serialize($a); // store $s somewhere where page2.php can find it. file_put_contents('store', $s);?>==========================================================File 2 - index2.php<?php // this is needed for the unserialize to work properly. $s = file_get_contents('store'); $a = unserialize($s); // now use the function show_one() of the $a object. echo $a;?>==============================

Link to comment
Share on other sites

Don't you mean:

// now use the function show_one() of the $a object.$a->show_one();

Also, make sure PHP can write to the directory.

Link to comment
Share on other sites

If you are unserializing a class, you must have the class defined before you unserialize the object. This would work:

<?phpclass xyz123 {  public $one = 1;  public function show_one() {	echo $this->one;  }}// this is needed for the unserialize to work properly.$s = file_get_contents('store');$a = unserialize($s);// now use the function show_one() of the $a object.$a->show_one();?>

Since the class needs to be defined, that's one reason why it's better to define the class in an external file, and then include the class definition whenever you need to use it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...