Bir oturumu ayarlama ile yardımcı

2 Cevap php

Ben benim diğer soru yani biraz belirsiz oldu tihnk ...

Bu durum.

Her sayfa yenilendiğinde rastgele 24 karakter dizesi oluşturur bir xml besleme var.

Bir kullanıcı 'yapmak nasıl yapılır-mavi-widgets.php' adlı bir sayfa sitemde ve toprakları ziyaret ettiğinde, - kendilerine özgü dize 38jsue710ppahchd67ywha94 örneğin oluşturulur.

Bu kullanıcı başka bir sayfaya tıkladığında Şimdi, 'how-to-make-kırmızı-widgets.php' Diyelim - besleme yepyeni bir dize oluşturur: 836aeq88udh761aso09kjs21.

Ne yapmak istediğinizi ziyaret her sayfada kullanıcının ziyareti boyunca besleme tarafından oluşturulan ilk değerini saklamak olduğunu.

Ben oturumları kullanarak ve burada benim kod ediyorum:

  // 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'];

Ben ilk siteyi, komut oturumu dize tükürür ziyaret zaman çalışıyor, ama ben yenileme zaman, hiçbir şey çıktılar.

Herhangi bir fikir?

2 Cevap

Denemek

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

yepyeni bir oturum ile.

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

Eğer SimpleXMLElement ilgilenmiyoruz ama dom gösterimi (halen) geçerli ise bunun metin içeriği sadece (dize) için cast beri. Sen (elbette) seri hale getirilebilir "normal" bir dizge olarak içeriğini alacak.

Bu deneyin:

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'];