首页 > 解决方案 > 从其他服务器访问特定服务器的 $_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();
?>

标签: phpsessionwebsocketratchet

解决方案


推荐阅读