Jump to content

Unserialize custom class from string sent in $_POST data


mdeaves

Recommended Posts

Hi there, 

 

I have been trying for hours now to unserialize a string sent via form post data from from php page to another with no luck.  On the same page, it works in code.  Posting the serialized string to another page however, results in me getting "Call to a member function getID() on a non-object" errors...  The strings sent between pages are exactly the same, but passing the serialized string via post from php page1 to php page 2 doesnt allow me to unserialize the object.

 

I am including my class files at the top of each page too....

 

If anyone can help me out with this it'd make my week.

 

Heres the simple class I am serializing:

 

class Item

{

private $itemID;

private $itemName;

private $itemDesc;

private $itemPrice;

 

//Main constructor

public function Item($ID, $Name, $Desc, $Price)

{

//Initialisation stuff goes in here

$this->itemID = $ID;

$this->itemName = $Name;

$this->itemDesc = $Desc;

$this->itemPrice = $Price;

}

 

public function getID()

{

return $this->itemID;

}

 

public function getName()

{

return $this->itemName;

}

 

public function getDescription()

{

return $this->itemDesc;

}

 

public function getPrice()

{

return $this->itemPrice;

}

}

 

and here is how I am serialising it on page one:

 

$item1 = new Item(1, 'Tooth Brush', 'A nice blue toothbrush', 2.99);

$item1Serialised = addslashes(serialize($item1));

 

and how I am posting it in form data (page one also):

 

echo '<input type="hidden" name="param" value="' . $item1Serialised . '">';

 

and how I am trying to pick it up in page two:

 

$test = unserialize(stripslashes($param));

 

Instead of addslashes() i have tried htmlspecialchars, htmlentities and all the corresponding decode methods to no avail.

 

I have also tried storing the class instance in an array and then serialising the array... no luck.

 

does any one have any ideas?

 

any hints much appreciated.

 

thanks,

 

mndeaves

 

Link to comment
Share on other sites

Call to a member function getID() on a non-object thats the error show me the code where u use that function

 

$item1 = new Item(1, 'Tooth Brush', 'A nice blue toothbrush', 2.99);???

 

I think thats wrong

$item1 = new Item;//Item the name of the class

$item1 ->Item(1, 'Tooth Brush', 'A nice blue toothbrush', 2.99);//thats how to call a function within a class

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.