您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman的http服务-响应-重定向 - 发送cookie
发布时间:2021-12-06 23:33:42编辑:雪饮阅读()
发送cookie
同样的,发送cookie需要使用Workerman\Protocols\Http\Response响应类。
实例:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8484');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$response = new Response(200, [], 'this is body');
$response->cookie('name', 'tom');
$connection->send($response);
};
// 运行worker
Worker::runAll();
?>
实例运行:
[root@izj6c2jeancylo0ppo4vz5z workerman]# php test.php start
Workerman[test.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.22 PHP version:7.0.33
------------------------------------------ WORKERS ------------------------------------------
proto user worker listen processes status
tcp root none http://0.0.0.0:8484 1 [OK]
---------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
客户端可以看到请求头中有设置cookie:
[root@izj6c2jeancylo0ppo4vz5z ~]# wget http://127.0.0.1:8484 -S
--2021-12-06 17:00:08-- http://127.0.0.1:8484/
Connecting to 127.0.0.1:8484... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Server: workerman
Set-Cookie: name=tom
Connection: keep-alive
Content-Type: text/html;charset=utf-8
Content-Length: 12
Length: 12 [text/html]
Saving to: ‘index.html.14’
100%[======================================================================================================================================================================>] 12 --.-K/s in 0s
2021-12-06 17:00:08 (3.95 MB/s) - ‘index.html.14’ saved [12/12]
为什么是请求头设置cookie,明明是响应中设置cookie,怀疑是响应中设置cookie只是向请求端发送一个设置cookie的命令,实际设置cookie还是由请求端完成的。
关键字词:workerman,http,响应,重定向,发送cookie
相关文章
- workerman的http服务-响应-快捷响应
- workerman的http服务-响应-更改状态码
- workerman的http服务-响应-更改状态码 - Response类初
- workerman的http服务-响应-发送header
- workerman的http服务-响应-发送header - Response类初
- workerman的http服务-请求-获取请求sessionId
- workerman的http服务-请求-获取请求HTTP版本
- workerman的http服务-请求-获取请求queryString
- workerman的http服务-请求-获取请求路径
- workerman的http服务-请求-获取请求uri