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); }
function cookie($name, $value = '') { if (is_array($name)) { // 初始化 \think\Cookie::init($name); } elseif (is_null($name)) { // 清除 \think\Cookie::clear($value); } elseif ('' === $value) { // 获取 return \think\Cookie::get($name); } elseif (is_null($value)) { // 删除session return \think\Cookie::delete($name); } else { // 设置session return \think\Cookie::set($name, $value); } }
/** * Cookie管理 * @param string|array $name cookie名称,如果为数组表示进行cookie设置 * @param mixed $value cookie值 * @param mixed $option 参数 * @return mixed */ function cookie($name, $value = '', $option = null) { if (is_array($name)) { // 初始化 Cookie::init($name); } elseif (is_null($name)) { // 清除 Cookie::clear($value); } elseif ('' === $value) { // 获取 return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1), $option) : Cookie::get($name); } elseif (is_null($value)) { // 删除 return Cookie::delete($name); } else { // 设置 return Cookie::set($name, $value, $option); } }