tags:

views:

68

answers:

2

Hi Guys,

I tihnk my other question was a bit vague so...

This is the situation.

I have an xml feed that generates a random 24 character string on every page refresh.

So, when a user visits my site and lands on a page called 'how-to-make-blue-widgets.php' - their unique string is generated as 38jsue710ppahchd67ywha94 for example.

Now, when this user clicks on another page, lets say 'how-to-make-red-widgets.php' - the feed creates a brand new string: 836aeq88udh761aso09kjs21.

What I want to do is store the first value created by the feed throughout the user's visit on every page visited.

I am using sessions and here is my code:

  // Start Session
  session_start();

  ...

  // Don't worry about this bit - $sessionId is the random string
  if($xmlobj = simplexml_load_string(file_get_contents($xml_feed)))
  {
        $result = $xmlobj->xpath("TrafficMeta");
        $sessionId = $result[0]->sessionId;
  }


  // Main Part
  if(isset($_SESSION['sessionString'])): // if 'random' session is set
        $string = $_SESSION['sessionString'];
  else: 
        $string = $sessionId; 
        $_SESSION['sessionString'] = $string;
  endif;

  echo $_SESSION['sessionString'];

It works fine when I first visit the site, the script spits out the session string, but when I refresh, it outputs nothing.

Any ideas?

+2  A: 

Try this:

if (!isset($_SESSION['sessionString'])) {
    if ($xmlobj = simplexml_load_string(file_get_contents($xml_feed))) {
        $result = $xmlobj->xpath("TrafficMeta");
        $_SESSION['sessionString'] = $result[0]->sessionId;
    } else {
        // Error: no session ID available
    }
}

echo $_SESSION['sessionString'];
Gumbo
Thanks for the reply. Same story I'm afraid.
Keith Donegan
@Keith Donegan: How do you carry the session ID along? Via cookie or URL?
Gumbo
For the moment, I am just trying to get the string properly (which VolkerK has helped me with) - Now I will move onto this :)
Keith Donegan
+4  A: 

Try

$sessionId = (string)$result[0]->sessionId;

with a brand new session.

When you read an xml document with simplexml_load_string() libxml (the library driving php-dom and php-simplexml behind the scene) will create a dom representation of the document in memory. All the SimpleXMLElement objects that "belong" to an xml document reference the same representation. Without this dom representation the SimpleXMLElements are more or less worthless.
Without the explicit cast to string you store a SimplXMLElement in _SESSION. At the end of the php instance or when session_write_close() is called _SESSION gets serialized and along with it the SimpleXMLElement. But SimpleXMLElements do not persist the libxml dom representation they belong to when they get serialized. And then when they're unserialized again they do not reference a valid dom anymore and PHP raises a warning

Warning: unserialize(): Node no longer exists in ....

Since you're not interested in the SimpleXMLElement but the text-contents of it just cast it to (string) while the dom representation is (still) valid. You will get the contents as a "normal" string that can (of course) be serialized.

VolkerK
Wow, please tell me how that works and what I was doing wrong?
Keith Donegan
thanks so much for taking the time to post again!
Keith Donegan