Jump to content

Serialize Not Working In Php


toreachdeepak
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...