Jump to content
Sign in to follow this  
toreachdeepak

Serialize Not Working In Php

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;?>==============================

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...