2015-11-26 65 views
0
我有3個Session變量的一個問題

,我有一個文件名爲chat.php是這樣的:與Session變量未定義指數

class Chat implements MessageComponentInterface { 
protected $clients; 
protected $data_clients; 
protected $target = "xxxxx"; //conversation target number/JID 

public function __construct() { 
    $this->clients = new \SplObjectStorage; 
    $this->data_clients = new \SplObjectStorage; 

    session_start(); 
    echo 'begin'; 
    $_SESSION["running"] = time(); 
    $_SESSION["inbound"] = array(); 
    $_SESSION["outbound"] = array(); 
    echo 'end'; 

    $this->send_message('hello'); 

    $this->Listen(true); 

} 



public function onError(ConnectionInterface $conn, \Exception $e) { 
    echo "An error has occurred: {$e->getMessage()}\n"; 
    $conn->close(); 
} 

public function Listen($initial){ 
    $band=false; 

    $options = array(
     CURLOPT_HEADER => false, 
     CURLOPT_TIMEOUT => 40000 
    ); 

    $params=['initial'=>$initial, 'target'=>$this->target,'method'=>'prueba']; 
    $defaults = array(
     CURLOPT_URL => 'localhost/socket/src/myApp/script2.php', 
     CURLOPT_POST => true, 
     CURLOPT_POSTFIELDS => http_build_query($params), 
    ); 
    $ch = curl_init(); 
    curl_setopt_array($ch, ($options + $defaults)); 
    curl_exec($ch); 

    if(curl_errno($ch)){ 
     $band=true; 
    } 
    curl_close($ch); 

    if(!$band) 
     $this->Listen(false); 
}//fin Listen 


public function send_message($mensaje){ 

    $band=false; 
    $options = array(
     CURLOPT_HEADER => false, 
    ); 

    $params=['method'=>'sendMessage','target'=>$this->target,'message'=>$mensaje]; 
    $defaults = array(
     CURLOPT_URL => 'localhost/socket/src/myApp/ajax2.php', 
     CURLOPT_POST => true, 
     CURLOPT_POSTFIELDS => http_build_query($params), 
    ); 
    $ch = curl_init(); 
    curl_setopt_array($ch, ($options + $defaults)); 
    curl_exec($ch); 
    curl_close($ch); 


}//fin de enviar mensaje 
} 

所以,我$這個 - > send_message(」你好')從構造函數調用一個名爲ajax2劇本,但後來我有此錯誤:

Notice: Undefined index: outbound in C:\xampp\htdocs\socket\src\myApp\ajax2.php on line 16 
envia 
Notice: Undefined index: running in C:\xampp\htdocs\socket\src\myApp\socket2.php on line 15 

ajax2的內容是:

<?php 
session_start(); 

$method = $_POST["method"]; 

switch ($method) { 
    case "sendMessage": 
     $target = $_POST["target"]; 
     $message = $_POST["message"]; 
     $outbound = $_SESSION["outbound"]; 
     $outbound[] = array("target" => $target, "body" => $message); 
     $_SESSION["outbound"] = $outbound; 
     break; 
    case "pollMessages": 
     $inbound = @$_SESSION["inbound"]; 
     $_SESSION["inbound"] = array(); 
     $profilepic = @$_SESSION["profilepic"]; 
     $ret = new JSONResponse(); 
     if ($profilepic != null && $profilepic != "") { 
      $ret->profilepic = $profilepic; 
     } 
     $_SESSION["profilepic"] = ""; 
     if (count($inbound) > 0) { 
      foreach ($inbound as $message) { 
       $ret->messages[] = $message; 
      } 
     } 
     echo json_encode($ret); 
     break; 
} 

那麼,問題出在會話變量,我不知道我做錯了,我已經作出在session_start均勻。如果你看到,我在構造中創建它們,然後在ajax2中使用它們。謝謝!

回答

0

它不工作,因爲你是在試圖訪問會話變量$ _SESSION [ 「出站」],$ _ SESSION [ 「入站」],$ _ SESSION一個完全不同的會話[ 「profilepic」 在ajax2.php

通過session_start()導致會話在chat.php中創建;與session_start()在ajax2.php中創建的會話不同。導致你正在通過CURL訪問ajax2.php,這會在ajax2.php中創建一個新的部分,這將與chat.php中的會話不同。

+0

歡迎來到Stack Overflow!雖然此代碼片段可能會解決此問題,但包括[解釋](http://meta.stackexchange.com/q/114762/305455)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要使用解釋性註釋來擠佔代碼,因爲這會降低代碼和解釋的可讀性! – jmattheis