Ratchet: [0.4.x] How to get cookies on the connection?

Created on 15 May 2016  路  3Comments  路  Source: ratchetphp/Ratchet

https://github.com/perchlayer/webird/blob/master/app/phalcon/modules/cli/library/Chat.php#L41

PHP Notice: Undefined property: stdClass::$request in /opt/webird/app/phalcon/modules/cli/library/Chat.php on line 44

This works on 0.3 but it no longer does on 0.4.x-dev. Is it implemented or did something change at Guzzle. I look at sources and documentation in Ratchet and Guzzle but I can't figure out if it changed or not yet implemented.

docs question

Most helpful comment

In 0.4 there are a few changes in this area to be aware of:

  • There is a clearer separation of protocols, so request is no longer attached to WebSocket
  • The Guzzle API has been replaced with PSR-7
  • There is no cookie parser due to the previous bullet point

So what does that mean? Your code will need to look a bit more like this:

$cookies = $conn->httpRequest->getHeader('Cookie');

Next, you will need to parse it. I haven't done much research into this you could look into dflydev/dflydev-fig-cookies

All 3 comments

In 0.4 there are a few changes in this area to be aware of:

  • There is a clearer separation of protocols, so request is no longer attached to WebSocket
  • The Guzzle API has been replaced with PSR-7
  • There is no cookie parser due to the previous bullet point

So what does that mean? Your code will need to look a bit more like this:

$cookies = $conn->httpRequest->getHeader('Cookie');

Next, you will need to parse it. I haven't done much research into this you could look into dflydev/dflydev-fig-cookies

If you just want to parse the cookie values I would recommend using GuzzleHttp\Psr7\parse_header:

$cookiesHeader = $conn->httpRequest->getHeader('Cookie');
if(count($cookiesHeader)) {
    $cookies = \GuzzleHttp\Psr7\parse_header($cookiesHeader)[0];
    if (array_key_exists('PHPSESSID', $cookies)) {
        // PHPSESSID exists
    }
    else {
        // PHPSESSID does not exit
    }
}
else {
    // No cookie header
}

@arosolino This works and I like using Guzzle since it is already included in the vendor directory.

Should we leave this open until the 0.4 docs are made and the new version is released?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Lugia101101 picture Lugia101101  路  7Comments

tasaif picture tasaif  路  6Comments

Rinum picture Rinum  路  6Comments

grappetite-ali picture grappetite-ali  路  5Comments

mhlz picture mhlz  路  9Comments