think\Cookie::clear PHP Method

clear() public static method

Cookie清空
public static clear ( string | null $prefix = null ) : mixed
$prefix string | null cookie前缀
return mixed
    public static function clear($prefix = null)
    {
        // 清除指定前缀的所有cookie
        if (empty($_COOKIE)) {
            return;
        }
        !isset(self::$init) && self::init();
        // 要删除的cookie前缀,不指定则删除config设置的指定前缀
        $config = self::$config;
        $prefix = !is_null($prefix) ? $prefix : $config['prefix'];
        if ($prefix) {
            // 如果前缀为空字符串将不作处理直接返回
            foreach ($_COOKIE as $key => $val) {
                if (0 === strpos($key, $prefix)) {
                    if ($config['setcookie']) {
                        setcookie($key, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
                    }
                    unset($_COOKIE[$key]);
                }
            }
        }
        return;
    }

Usage Example

Example #1
0
 public function testClear()
 {
     $_COOKIE = [];
     $this->assertEquals(null, \think\Cookie::clear());
     $_COOKIE = ['a' => 'b'];
     \think\Cookie::clear();
     $this->assertEquals(null, $_COOKIE);
     $_COOKIE = ['a' => 'b', 'pre_abc' => 'c'];
     \think\Cookie::clear('pre_');
     $this->assertEquals(['a' => 'b'], $_COOKIE);
 }
All Usage Examples Of think\Cookie::clear