/** * 获取登录用户信息 */ protected function getLoginuUser() { $userSid = Cookie::get("userSid"); if (!empty($userSid)) { $this->userInfo = $this->pdo->find("select * from pre_users where sid=:sid limit 1", array(":sid" => $userSid)); } $this->assign("userInfo", $this->userInfo); }
public function testDelete() { $_COOKIE = ['a' => 'b', 'pre_abc' => 'c']; $this->assertEquals('b', \think\Cookie::get('a')); \think\Cookie::delete('a'); $this->assertEquals(null, \think\Cookie::get('a')); $this->assertEquals('c', \think\Cookie::get('abc', 'pre_')); \think\Cookie::delete('abc', 'pre_'); $this->assertEquals(null, \think\Cookie::get('abc', 'pre_')); }
/** * 语言检查 * 检查浏览器支持语言,并自动加载语言包 * @access private * @return void */ private function checkLanguage() { // 不开启语言包功能,仅仅加载框架语言文件直接返回 if (!C('LANG_SWITCH_ON')) { return; } // 默认语言 $langSet = C('DEFAULT_LANG'); // 启用语言包 // 自动侦查语言 if (C('LANG_AUTO_DETECT')) { // url中设置了语言变量 if (isset($_GET[C('VAR_LANGUAGE')])) { $langSet = $_GET[C('VAR_LANGUAGE')]; Cookie::set('think_language', $langSet, 3600); } elseif (Cookie::get('think_language')) { $langSet = Cookie::get('think_language'); // 自动侦测浏览器语言 } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { preg_match('/^([a-z\\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches); $langSet = $matches[1]; Cookie::set('think_language', $langSet, 3600); } // 非法语言参数,请用默认设置 if (false === stripos(C('LANG_LIST'), $langSet)) { $langSet = C('DEFAULT_LANG'); } } // 定义当前语言 define('LANG_SET', strtolower($langSet)); // echo LANG_PATH . LANG_SET . '/common.php'; // 读取项目公共语言包 if (is_file(LANG_PATH . LANG_SET . '/common.php')) { L(include LANG_PATH . LANG_SET . '/common.php'); } $group = ''; $lang_path = C('APP_GROUP_MODE') == 1 ? BASE_LIB_PATH . 'Lang/' . LANG_SET . '/' : LANG_PATH . LANG_SET . '/'; // 读取当前分组公共语言包 if (defined('GROUP_NAME')) { if (is_file($lang_path . GROUP_NAME . '.php')) { L(include $lang_path . GROUP_NAME . '.php'); } $group = GROUP_NAME . C('TMPL_FILE_DEPR'); } // 读取当前模块语言包 if (is_file($lang_path . $group . strtolower(CONTROLLER_NAME) . '.php')) { L(include $lang_path . $group . strtolower(CONTROLLER_NAME) . '.php'); } }
/** * 自动侦测设置获取语言选择 * @return void */ public static function detect() { // 自动侦测设置获取语言选择 $langCookieVar = Config::get('lang_cookie_var'); $langDetectVar = Config::get('lang_detect_var'); $langSet = ''; if (isset($_GET[$langDetectVar])) { // url中设置了语言变量 $langSet = strtolower($_GET[$langDetectVar]); \think\Cookie::set($langCookieVar, $langSet, 3600); } elseif (\think\Cookie::get($langCookieVar)) { // 获取上次用户的选择 $langSet = strtolower(\think\Cookie::get($langCookieVar)); } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { // 自动侦测浏览器语言 preg_match('/^([a-z\\d\\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches); $langSet = strtolower($matches[1]); \think\Cookie::set($langCookieVar, $langSet, 3600); } if (in_array($langSet, \think\Config::get('lang_list'))) { // 合法的语言 self::$range = $langSet; } }
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); } }
/** * 自动侦测设置获取语言选择 * @return void */ public static function detect() { // 自动侦测设置获取语言选择 if (isset($_GET[self::$var])) { $langSet = $_GET[self::$var]; // url中设置了语言变量 \think\Cookie::set('think_language', $langSet, 3600); } elseif (\think\Cookie::get('think_language')) { // 获取上次用户的选择 $langSet = \think\Cookie::get('think_language'); } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { // 自动侦测浏览器语言 preg_match('/^([a-z\\d\\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches); $langSet = $matches[1]; \think\Cookie::set('think_language', $langSet, 3600); } if (in_array($langSet, \think\Config::get('lang_list'))) { // 合法的语言 self::$range = $langSet; } }
/** * 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); } }
/** * 句柄测试 * @return mixed * @access public */ public function testGetInstance() { \think\Cookie::get('a'); $view_instance = \think\View::instance(); $this->assertInstanceOf('\\think\\view', $view_instance, 'instance方法返回错误'); }
/** * 自动侦测设置获取语言选择 * @return void */ public static function detect() { // 自动侦测设置获取语言选择 $langSet = ''; if (isset($_GET[self::$langDetectVar])) { // url中设置了语言变量 $langSet = strtolower($_GET[self::$langDetectVar]); Cookie::set(self::$langCookieVar, $langSet, 3600); } elseif (Cookie::get(self::$langCookieVar)) { // 获取上次用户的选择 $langSet = strtolower(Cookie::get(self::$langCookieVar)); } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { // 自动侦测浏览器语言 preg_match('/^([a-z\\d\\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches); $langSet = strtolower($matches[1]); Cookie::set(self::$langCookieVar, $langSet, 3600); } if (empty(self::$allowLangList) || in_array($langSet, self::$allowLangList)) { // 合法的语言 self::$range = $langSet; } }
/** * 判断是否是管理员 */ protected function isAdmin() { $webAdmin = Cookie::get("webAdmin"); if (empty($webAdmin) || $webAdmin !== C("webAdmin")) { $this->alert(sweetAlert("无权限", "请先登录管理员账号!", "warning")); } }
/** * 判断是否登录 */ protected function isAdmin() { $webAdmin = Cookie::get("webAdmin"); if (empty($webAdmin) || $webAdmin !== C("webAdmin")) { header("Location:" . U("index/Index/adminLogin")); exit; } }
/** * 检查 Cookie * * @return boolean true => 通过Cookie,false => 不存在Cookie */ protected function checkCookie() { // 获取 $cookie = Cookie::get($this->cookieName); $cookie = explode('|', $cookie); $cookie = array('email' => $cookie[0], 'password' => $cookie[1]); // 不存在cookie if (!$cookie) { return false; } // 不存在记录 if (!M('User')->where(array('email' => $cookie['email']))->find()) { return false; } // 哈希不正确 if ($cookie['password'] !== md5($data['email'] . $data['password'] . $data['password_salt'])) { return false; } // 记录Session $this->saveSession(); return true; }