使用 setGet 修改了参数, 参数是拼接上去的, 频繁访问的话, 参数会一直拼接

debm

问题描述

业务需要, 将传过来的用户名, 加一个前缀保存, 但是频繁访问的话, 这个前缀会一直加.
比如传的 username 是 test 前缀是 default_, 如果频繁访问, 这个 username 会变成 default_default_test 前缀会一直往前面加...是我写法有问题, 还是用的不对?

这是运行环境

-------------------------------------------- WORKERMAN ---------------------------------------------
Workerman/5.1.0 PHP/8.4.4 (Jit off) Darwin/23.6.0

$payload = $request->get();

$prefix = 'default_';
foreach ($payload as $key => $value) {
    if ('username' == $key) {
       $payload[$key] = $prefix . $value;
    }
}

$request->setGet($payload);

补充一下: 上面的修改代码是在中间件里写的, 使用这个参数的时候是在控制器里.

306 3 1
3个回答

Noah
if ('username' == $key) {
    // 如果值不以 $prefix 开头,才添加前缀
    if (strpos($value, $prefix) !== 0) {
        $payload[$key] = $prefix . $value;
    }
}
  • debm 6天前

    如果前端传的就是这个名字呢? 比如前缀写成 abc. 前端传的名字是 abcdefg , 这样也是要加前缀的.

  • Noah 6天前

    这就是你业务要规避的。你前缀可以设置个_开始啊。用户名肯定不能_开始的不就行了。反正让 prefix 和 username 互斥。

  • debm 6天前

    先不说业务是不是要规避, 单就这个问题本身来说, 我感觉不对.

  • Noah 6天前

    我觉得最简单的方法是 prefix 和 username 互斥,因为用代码去处理这种边界情况相对来说比较麻烦。谁知道用户名会注册各种奇怪的名字。为了几个特殊用户的名字去用代码完善划不来。

胡桃

复用 Request 对象时没有清理脏数据。

  • debm 6天前

    这怎么清? 我看文档上没有, 看了这个 Webman\Http\Request 代码, 看着也没有对应的方法.

  • 胡桃 6天前

    support/Request.php

    <?php
    /**
     * This file is part of webman.
     *
     * Licensed under The MIT License
     * For full copyright and license information, please see the MIT-LICENSE.txt
     * Redistributions of files must retain the above copyright notice.
     *
     * @author    walkor<walkor@workerman.net>
     * @copyright walkor<walkor@workerman.net>
     * @link      http://www.workerman.net/
     * @license   http://www.opensource.org/licenses/mit-license.php MIT License
     */
    
    namespace support;
    
    /**
     * Class Request
     *
     * @package support
     */
    class Request extends \Webman\Http\Request
    {
        public function clearGetData(): void
        {
            if (isset($this->data['get'])) {
                unset($this->data['get']);
            }
        }
    }

    app/middleware/ClearGetData.php

    <?php
    declare(strict_types=1);
    
    namespace app\middleware;
    
    use Webman\Http\Request;
    use Webman\Http\Response;
    use Webman\MiddlewareInterface;
    
    class ClearGetData implements MiddlewareInterface
    {
        public function process(Request $request, callable $handler): Response
        {
            // TODO: Implement process() method.
            $request->clearGetData();
            return $handler($request);
        }
    }

    app/config/middleware.php

    <?php
    /**
     * This file is part of webman.
     *
     * Licensed under The MIT License
     * For full copyright and license information, please see the MIT-LICENSE.txt
     * Redistributions of files must retain the above copyright notice.
     *
     * @author    walkor<walkor@workerman.net>
     * @copyright walkor<walkor@workerman.net>
     * @link      http://www.workerman.net/
     * @license   http://www.opensource.org/licenses/mit-license.php MIT License
     */
    
    return [
        '' => [
            \app\middleware\ClearGetData::class,
        ],
    ];
  • debm 6天前

    感谢, 我试了下, 可以这样处理.

damao
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class Test implements MiddlewareInterface
{
    public function process(Request $request, callable $handler) : Response
    {
        $payload = $request->get();
        $prefix = 'default_';
        foreach ($payload as $key => $value) {
            if ('username' == $key) {
                $payload[$key] = $prefix . $value;
            }
        }
        $request->setGet($payload);
        return $handler($request);
    }
}
<?php
return [
    '' => [
        \app\middleware\Test::class
    ]
];
<?php
namespace app\controller;
use support\Request;
class IndexController
{
    public function index(Request $request)
    {
        return json($request->get());
    }
}

截图

刷新了N次也没复现

  • debm 6天前

    不知道是不是我们安装的环境和版本不一样的原因, 我这里一直刷一直有...

×
🔝