您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman的Http服务-请求-获取cookie
发布时间:2021-12-04 23:59:10编辑:雪饮阅读()
获取cookie
获取整个cookie数组
$cookies = $request->cookie();
如果请求没有cookie参数则返回一个空的数组。
获取cookie数组的某一个值
$name = $request->cookie('name');
如果cookie数组中不包含这个值则返回null。
与get方法一样,你也可以给cookie方法第二个参数传递一个默认值,如果cookie数组中没找到对应值则返回默认值。例如:
$name = $request->cookie('name', 'tom');
实例:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8484');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
// $request为请求对象,这里没有对请求对象执行任何操作直接返回hello给浏览器
var_dump("cookie数组:");
echo "\r\n";
var_dump($request->cookie());
echo "\r\n";
var_dump("cookie参数name:");
echo "\r\n";
var_dump($request->cookie("name"));
echo "\r\n";
var_dump("cookie参数age:");
echo "\r\n";
var_dump($request->cookie('age', '暂无age'));
echo "\r\n";
$connection->send("hello\r\n");
};
// 运行worker
Worker::runAll();
?>
实例运行并有curl发送了cookie:
[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.
string(15) "cookie数组:"
array(3) {
["name"]=>
string(9) "snowDrink"
["gender"]=>
string(3) "男"
["id"]=>
string(1) "1"
}
string(19) "cookie参数name:"
string(9) "snowDrink"
string(18) "cookie参数age:"
string(9) "暂无age"
curl客户端发送cookie:
[root@izj6c2jeancylo0ppo4vz5z ~]# curl --location --request POST 'http://127.0.0.1:8484' --data '' --cookie "name=snowDrink; gender=男; id=1"
hello
关键字词:workerman,Http,cookie,获取cookie
相关文章
- workerman的Http服务-请求-获得原始请求post包体
- workerman的Http服务-请求-获得请求对象
- workerman的Http服务-请求-获得请求参数post
- workerman的Http服务-请求-获得请求参数get
- workerman的AsyncUdpConnection类的send方法
- workerman的AsyncUdpConnection类的__construct构造方
- workerman的AsyncTcpConnection类的transport属性都利
- workerman的AsyncTcpConnection类的reConnect 方法实
- workerman的AsyncTcpConnection类的connect 方法实现
- workerman类TcpConnection的接口send