public function index() { $action = I("post.action"); if ($action == "login") { $user = I("post.user"); $pwd = I("post.pwd"); if (strlen($user) < 3 || strlen($pwd) < 5) { $this->assign("alert", sweetAlert("温馨提示", "用户名或者密码格式不正确!", "warning")); } else { $pwd = md5Pwd($pwd); if ($user = $this->pdo->find("select uid from pre_users where (user=:user or email=:user) and pwd=:pwd limit 1", array(":user" => $user, ":pwd" => $pwd))) { $sid = getSid(); $this->pdo->execute("update pre_users set sid=:sid where uid=:uid limit 1", array(":uid" => $user['uid'], ":sid" => $sid)); Cookie::set("userSid", $sid, array("path" => "/", "expire" => 3600 * 24)); $this->assign("alert", sweetAlert("登录成功!", "欢迎回来,进入控制面板!", "success", U("/index/Panel/index"))); return $this->fetch("common/sweetAlert"); } else { $this->assign("alert", sweetAlert("温馨提示", "用户名或者密码不正确!", "warning")); } } } elseif ($action == "reg") { $this->assign("isReg", true); $user = I("post.user"); $pwd = I("post.pwd"); $email = strtolower(I("post.email")); $code = I("post.code"); if (strlen($user) < 3) { $this->assign("alert", sweetAlert("温馨提示", "用户名太短!", "warning")); } elseif (strlen($pwd) < 5) { $this->assign("alert", sweetAlert("温馨提示", "密码太简单!", "warning")); } elseif (strlen($code) != 4 || !isset($_COOKIE['verification']) || md5(strtolower($code)) !== $_COOKIE['verification']) { $this->assign("alert", sweetAlert("温馨提示", "验证码错误!", "warning")); } elseif (!preg_match('/^[a-zA-Z0-9\\-\\_]+@[a-zA-Z0-9\\-]+\\.[a-zA-Z]+$/', $email)) { $this->assign("alert", sweetAlert("温馨提示", "邮箱格式不正确!", "warning")); } elseif ($this->pdo->find("select uid from pre_users where user=:user limit 1", array(":user" => $user))) { $this->assign("alert", sweetAlert("温馨提示", "用户名已存在!", "warning")); } elseif ($this->pdo->find("select uid from pre_users where email=:email limit 1", array(":email" => $email))) { $this->assign("alert", sweetAlert("温馨提示", "邮箱账号已存在!", "warning")); } else { setCookie('verification', null, -1, '/'); //销毁验证码 $insert = array(); $insert[':user'] = $user; $insert[':pwd'] = md5Pwd($pwd); $insert[':email'] = $email; $insert['sid'] = getSid(); if ($this->pdo->execute("INSERT INTO `pre_users` (`user`, `email`, `pwd`, `sid`, `regtime`) VALUES (:user, :email, :pwd, :sid, NOW())", $insert)) { $this->assign("alert", sweetAlert("注册成功!", "马上登录!", "success", U("index"))); return $this->fetch("common/sweetAlert"); } else { $this->assign("alert", sweetAlert("温馨提示", "注册失败,请稍候再试!", "warning")); } } } $this->assign("webTitle", C("webTitle") ? C("webTitle") : "网站首页"); return $this->fetch(); }
/** * 获取登录用户信息 */ 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 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); }
/** * 语言检查 * 检查浏览器支持语言,并自动加载语言包 * @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); } }
public function testThinkVar() { $config['tpl_begin'] = '{'; $config['tpl_end'] = '}'; $template = new Template($config); $_SERVER['SERVER_NAME'] = 'server_name'; $_GET['action'] = 'action'; $_POST['action'] = 'action'; \think\Cookie::set('action', ['name' => 'name']); \think\Session::set('action', ['name' => 'name']); define('SITE_NAME', 'site_name'); $content = <<<EOF {\$Think.SERVER.SERVER_NAME}<br/> {\$Think.GET.action}<br/> {\$Think.POST.action}<br/> {\$Think.COOKIE.action}<br/> {\$Think.COOKIE.action.name}<br/> {\$Think.SESSION.action}<br/> {\$Think.SESSION.action.name}<br/> {\$Think.ENV.OS}<br/> {\$Think.REQUEST.action}<br/> {\$Think.CONST.SITE_NAME}<br/> {\$Think.LANG.action}<br/> {\$Think.CONFIG.action.name}<br/> {\$Think.NOW}<br/> {\$Think.VERSION}<br/> {\$Think.LDELIM}<br/> {\$Think.RDELIM}<br/> {\$Think.SITE_NAME} EOF; $data = <<<EOF <?php echo \$_SERVER['SERVER_NAME']; ?><br/> <?php echo \$_GET['action']; ?><br/> <?php echo \$_POST['action']; ?><br/> <?php echo \\think\\Cookie::get('action'); ?><br/> <?php echo \$_COOKIE['action']['name']; ?><br/> <?php echo \\think\\Session::get('action'); ?><br/> <?php echo \$_SESSION['action']['name']; ?><br/> <?php echo \$_ENV['OS']; ?><br/> <?php echo \$_REQUEST['action']; ?><br/> <?php echo SITE_NAME; ?><br/> <?php echo \\think\\Lang::get('action'); ?><br/> <?php echo \\think\\Config::get('action.name'); ?><br/> <?php echo date('Y-m-d g:i a',time()); ?><br/> <?php echo THINK_VERSION; ?><br/> <?php echo '{'; ?><br/> <?php echo '}'; ?><br/> <?php echo SITE_NAME; ?> EOF; $template->parse($content); $this->assertEquals($data, $content); }
/** * 自动侦测设置获取语言选择 * @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 $this */ protected function removeCookie() { Cookie::set($this->cookieName, null); return $this; }