public function getUserLanguage() { // $serverArray = explode(".", SERVER); if (isset($_GET['l'])) { $l = $_GET['l']; \session\Session::set('lang', $l); // } else if(in_array($serverArray[0], $this->valid_languages)) { // $l = $serverArray[0]; } else { if (\session\Session::issetKey('lang')) { $l = \session\Session::get('lang'); } else { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $l = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); } else { $l = "es"; } } } return $l; }
/** * 生成验证码 * @param string $id 唯一标识 */ public function entry($id = '') { $now_time = time(); #验证码长宽设置 $this->imageW || ($this->imageW = $this->length * $this->fontSize * 1.5 + $this->length * $this->fontSize / 2); $this->imageH || ($this->imageH = $this->fontSize * 2.5); # 新建画布 $this->image = imagecreate($this->imageW, $this->imageH); # 设置背景色 imagecolorallocate($this->image, $this->bg[0], $this->bg[1], $this->bg[2]); # 验证码使用随机字体 $ttfPath = dirname(__FILE__) . '/ttfs'; if (empty($this->fontttf)) { $dir = dir($ttfPath); $ttfs = array(); while (false !== ($file = $dir->read())) { if ($file[0] != '.' && substr($file, -4) == '.ttf') { $ttfs[] = $file; } } $dir->close(); $this->fontttf = $ttfs[array_rand($ttfs)]; } $this->fontttf = $ttfPath . '/' . $this->fontttf; # 开始绘制验证码 $code = array(); $codeNX = 0; for ($i = 0; $i < $this->length; $i++) { # 验证码字体随机颜色 $color = imagecolorallocate($this->image, mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150)); $code[$i] = $this->codeSet[mt_rand(0, strlen($this->codeSet) - 1)]; $codeNX += mt_rand($this->fontSize * 1.2, $this->fontSize * 1.3); imagettftext($this->image, $this->fontSize, mt_rand(-20, 20), $codeNX, $this->fontSize * 1.5, $color, $this->fontttf, $code[$i]); } #干扰线 if ($this->useLine) { $this->writeLine(); } # 保存验证码 $key = $this->authcode($this->keySet); $code = $this->authcode(implode('', $code)); $secode = array(); $secode['verify_code'] = $code; $secode['verify_time'] = $now_time; Session::set($key . $id, $secode); header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); header("content-type: image/png"); # 输出图像 imagepng($this->image); imagedestroy($this->image); }
protected function pushError($error) { $errors = Session::get('errors'); if (!$errors) { $errors = array(); } if (is_array($error)) { foreach ($error as $e) { $errors[] = $e; } } else { $errors[] = $error; } Session::set('errors', $errors); }
public function flash($key, $value) { $flash = Session::issetKey(self::$flash_key) ? Session::get(self::$flash_key) : array(); $flash[$key] = $value; Session::set(self::$flash_key, $flash); }
/** * 保存认证信息 * * @param mixed $content * @return void */ public function write($content) { \session\Session::regenerateId(); \session\Session::set($this->namesapce, $content); }
/** * @param $tpl * * @return mixed|string */ public function fetch($tpl) { $view_dir = $this->getViewDir(); $view = $view_dir . '/' . $tpl . '.php'; ob_start(); if (!empty($this->header)) { $header = $this->header . '.php'; if (is_file($header)) { require $header; } } require $view; if (!empty($this->footer)) { $footer = $this->footer . '.php'; if (is_file($footer)) { require $footer; } } $data = ob_get_contents(); ob_end_clean(); if ($this->ob_data != '') { $data = $this->ob_data . $data; } #form添加token,防止表单重复提交 $token = md5(uniqid('token')); $sess_token = Session::get('token'); if (!$sess_token) { $sess_token = array($token); } elseif (is_array($sess_token)) { $sess_token[] = $token; } else { $sess_token = array($sess_token, $token); } Session::set('token', $sess_token); $data = preg_replace('/<\\/form>/', "<input type=\"hidden\" name=\"token\" value=\"{$token}\" />\n</form>", $data); return $data; }
function login() { $this->errors = false; if (isset($_POST['username']) && isset($_POST['password'])) { $username = $_POST['username']; $password = $_POST['password']; $remember = false; // TODO isset($_POST['remember']) && $_POST['remember']; $usertable = Doctrine::getTable("User"); $user = $usertable->findOneByUsername($username); if ($user && $user->password == md5($password)) { if ($remember) { $lifetime = 3600 * 24 * 365; ini_set('session.cookie_lifetime', $lifetime); ini_set('session.gc_maxlifetime', $lifetime + 600); session_set_cookie_params($lifetime, "/"); } session_regenerate_id(); Session::set('id_user', $user->id); if ($this->redirectTo) { $this->redirect($this->redirectTo); } else { $this->redirect(""); } } else { $this->addError(_("Wrong username or password")); } } }