我遇到3个会话变量的问题,我有一个名为 chat.php 的文件,这是:
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
}
所以,我 $ this-> send_message(' hello')来自构造函数调用一个名为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 :(得分:0)
它不起作用,因为你试图访问会话变量$ _SESSION [&#34; outbound&#34;],$ _ SESSION [&#34; inbound&#34;],$ _ SESSION [&#34; profilepic&#34 ;] ajax2.php中完全不同的会话
导致session.start()在chat.php中创建会话;与session_start()在ajax2.php中创建的会话不同;因为您正在通过CURL访问ajax2.php,这会在ajax2.php中创建一个新的部分,这将与chat.php中的会话不同