Skip to content

Commit d736267

Browse files
committed
Add description and example
1 parent ed421fd commit d736267

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ For more details about the request object, check out the documentation of
195195
and
196196
[PSR-7 RequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface).
197197

198-
> Currently the cookies and uploaded files are not added by the
198+
> Currently the uploaded files are not added by the
199199
`Server`, but you can add these parameters by yourself using the given methods.
200200
The next versions of this project will cover these features.
201201

@@ -348,6 +348,43 @@ Allowed).
348348
can in fact use a streaming response body for the tunneled application data.
349349
See also [example #21](examples) for more details.
350350

351+
The `getCookieParams(): string[]` method can be used to
352+
get all cookies sent with the current request.
353+
354+
```php
355+
$http = new Server($socket, function (ServerRequestInterface $request) {
356+
$key = 'react\php';
357+
358+
if (isset($request->getCookieParams()[$key])) {
359+
$body = "Your cookie value is: " . $request->getCookieParams()[$key];
360+
361+
return new Response(
362+
200,
363+
array('Content-Type' => 'text/plain'),
364+
$body
365+
);
366+
}
367+
368+
return new Response(
369+
200,
370+
array(
371+
'Content-Type' => 'text/plain',
372+
'Set-Cookie' => urlencode($key) . '=' . urlencode('test;more')
373+
),
374+
"Your cookie has been set."
375+
);
376+
});
377+
```
378+
379+
The above example will try to set a cookie on first access and
380+
will try to print the cookie value on all subsequent tries.
381+
Note how the example uses the `urlencode()` function to encode
382+
non-alphanumeric characters.
383+
This encoding is also used internally when decoding the name and value of cookies
384+
(which is in line with other implementations, such as PHP's cookie functions).
385+
386+
See also [example #6](examples) for more details.
387+
351388
### Response
352389

353390
The callback function passed to the constructor of the [Server](#server)

examples/06-cookie-handling.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\Socket\Server;
5+
use React\Http\Response;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
8+
require __DIR__ . '/../vendor/autoload.php';
9+
10+
$loop = Factory::create();
11+
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
12+
13+
$server = new \React\Http\Server($socket, function (ServerRequestInterface $request) {
14+
$key = 'react\php';
15+
16+
if (isset($request->getCookieParams()[$key])) {
17+
$body = "Your cookie value is: " . $request->getCookieParams()[$key];
18+
19+
return new Response(
20+
200,
21+
array('Content-Type' => 'text/plain'),
22+
$body
23+
);
24+
}
25+
26+
return new Response(
27+
200,
28+
array(
29+
'Content-Type' => 'text/plain',
30+
'Set-Cookie' => urlencode($key) . '=' . urlencode('test;more')
31+
),
32+
"Your cookie has been set."
33+
);
34+
});
35+
36+
echo 'Listening on http://' . $socket->getAddress() . PHP_EOL;
37+
38+
$loop->run();

0 commit comments

Comments
 (0)