php - 从其他服务器访问特定服务器的 $_SESSION
问题描述
我有一个网站,我在其中使用 websocket- PHP Ratchet- using class Chat
。在我的网站上,$_SESSION['user_id']
定义明确。我希望在Chat
课堂上访问这个值。我如何在 websocket 服务器中访问此值,而此值驻留在网站服务器中?当我尝试访问它时,出现错误:(Notice: Undefined variable: _SESSION
错误行在代码中突出显示)
这是我的Chat.php
:
<?php
namespace dealspace_websocket;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
require_once __DIR__ . "/../Model/DBConnection.php";
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $from, $msg) {
$JSonMsgObject = json_decode($msg, true);
if($JSonMsgObject['type'] === 'updownvote') {
$connection = new \DBConnection();
$allowUpdate = false;
try {
$sql = 'SELECT `voters`
FROM `deals`
WHERE `id` = :id
LIMIT 1';
$stmt = $connection->dbh->prepare($sql);
$stmt->execute(array(
'id' => $JSonMsgObject['deal_data']['dealid']
));
$allowUpdate = $stmt->fetch(\PDO::FETCH_ASSOC);
if($allowUpdate !== false) {
$votersString = $allowUpdate['voters'];
if($votersString !== '')
$votersArray = explode(',', $votersString);
$votersArray[] = $_SESSION['user_id']; // --- Here I try to access --- \\\
$votersString = implode(',', $votersArray);
$allowUpdate = false;
try {
$sql = 'UPDATE `deals`
SET `votes_counter` = :votes_counter, `voters` = :voters
WHERE `id` = :id';
$stmt = $connection->dbh->prepare($sql);
$allowUpdate = $stmt->execute(array(
'votes_counter' => $JSonMsgObject['deal_data']['votes'],
'voters' => $votersString,
'id' => $JSonMsgObject['deal_data']['dealid']
));
if($allowUpdate === true) {
$deal_dataOBJ->dealid = $JSonMsgObject['deal_data']['dealid'];
$deal_dataOBJ->votes = $JSonMsgObject['deal_data']['votes'];
$returnMsg->type = 'updownvote';
$returnMsg->deal_data = $deal_dataOBJ;
foreach($this->clients as $client) {
$client->send(json_encode($returnMsg));
}
}
} catch(\PDOException $e) {}
}
} catch(\PDOException $e) {}
$connection->disconnect();
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}
我的服务器shell脚本:
<?php
require __DIR__ . '/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use dealspace_websocket\Chat;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
?>
解决方案
推荐阅读
- macos - 在 mac os 上安装了“go1.13.3.darwin-amd64.pkg”。设置 GOPATH、PATH、GOROOT 环境变量。但是似乎没有安装Go
- javascript - 如何使用“while”循环使用 JavaScript 制作反向星号三角形
- reactjs - ReactJS componentDidMount 获取单元测试
- php - PHP如何从csv文件的所有列中获取数据并将其存储在数组中
- azure-cognitive-services - 从 PDF 文件的所需页面中提取文本
- indexing - Cosmos 重新索引所需的时间比预期的要长
- swift - 'JSONEncoder' 类型的默认参数值无法转换为'Encoder' 类型(TopLevelEncoder)
- flutter - Cloud Firestore 子集合访问权限
- java - 使“数据”瞬态或可序列化 SonarQube 错误
- html - HTML5 创建隐藏文本的字段,但可以复制/粘贴