delete_cookie('name') 不工作 codeigniter 4

delete_cookie('name') not working codeigniter 4

我试图删除我在用户登录时创建的 cookie,但不知为何 delete_cookie() 函数没有删除我创建的 cookie。我检查了文档和所有内容,但无法正常工作

这是我的代码

    public function __construct()
{
    helper('cookie');
}

    public function login() {
    $data = [];
    $session = session();
    $model = new AdminModel();
    $username = $this->request->getPost('username');
    $password = $this->request->getPost('password');
    $remember = $this->request->getPost('agree');

    $rules = [
        'username' => 'required',
        'password' => 'required',
    ];


    if(!$this->validate($rules)) {
        $data['validation'] = $this->validator;
    } else {
        $admin = $model->where('username', $username)->where('password', $password)->first();
        if($admin) {
            $session->set('uid', $admin['id']);

            if($remember) {
                set_cookie([
                    'name' => 'id',
                    'value' => $admin['id'],
                    'expire' => '3600',
                    'httponly' => false
                ]);
            }

        } else {
            $session->setFlashdata('msg', 'Incorrect Username or Password');
            return redirect()->to('admin/login');
        }
    }

    return view('admin/login', $data);
}

public function logout() {
    $session = session();
    $session->destroy();
    delete_cookie('id');

    return redirect()->to('admin/login')->withCookies();
}

编辑: 我修好了它。我不得不使用 withCookies();

重定向

使用这个库

使用Config\Services;

服务::响应()->deleteCookie('id');

参考这个link https://codeigniter.com/user_guide/libraries/cookies.html