I am not sure why this is coming up. I am not serializing the XML, but my array that I created from an RSS feed (note this is just a snippet):
$game_data = array (
'sysreqos' => $game->systemreq->pc->sysreqos,
'sysreqmhz' => $game->systemreq->pc->sysreqmhz,
'sysreqmem' => $game->systemreq->pc->sysreqmem,
'sysreqdx' => $game->systemreq->pc->sysreqdx,
'sysreqhd' => $game->systemreq->pc->sysreqhd,
);
Then I serialize it $some_var = serialize($game_data)
and write to a text file fputs($fh,$some_var)
.
But it does not get that far, it errors out on the serialize line:
Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'
In the Classes and Objects docs, there is this: In order to be able to unserialize() an object, the class of that object needs to be defined.
Prior to PHP 5.3, this was not an issue. But after PHP 5.3 an object made by SimpleXML_Load_String() cannot be serialized. An attempt to do so will result in a run-time failure, throwing an exception. If you store such an object in $_SESSION, you will get a post-execution error that says this:
Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in [no active file]:0 Stack trace: #0 {main} thrown in [no active file] on line 0
The entire contents of the session will be lost. Hope this saves someone some time!
<?php // RAY_temp_ser.php
error_reporting(E_ALL);
session_start();
var_dump($_SESSION);
$_SESSION['hello'] = 'World';
var_dump($_SESSION);
// AN XML STRING FOR TEST DATA
$xml = '<?xml version="1.0"?>
<families>
<parent>
<child index="1" value="Category 1">Child One</child>
</parent>
</families>';
// MAKE AN OBJECT (GIVES SimpleXMLElement)
$obj = SimpleXML_Load_String($xml);
// STORE THE OBJECT IN THE SESSION
$_SESSION['obj'] = $obj;
By: Ray.Paseur
what i do is as 'Stefan Gehrig' said, cast the XML data to a string
$_SESSION['obj'] = (string)$obj;
You have to cast the XML data to a string because internally they are all SimpleXMLElement
s.
$game_data = array (
'sysreqos' => (string)$game->systemreq->pc->sysreqos,
'sysreqmhz' => (string)$game->systemreq->pc->sysreqmhz,
'sysreqmem' => (string)$game->systemreq->pc->sysreqmem,
'sysreqdx' => (string)$game->systemreq->pc->sysreqdx,
'sysreqhd' => (string)$game->systemreq->pc->sysreqhd
);
Or perhaps a little bit more elegant:
$game_data = array();
$properties = array('sysreqos', 'sysreqmhz', 'sysreqmem', 'sysreqdx', 'sysreqhd');
foreach ($properties as $p) {
$game_data[$p] = (string)$game->systemreq->pc->$p;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With