【Laravel3.0.0源码阅读分析】会话载荷类payload.php
发布日期:2021-06-30 20:44:53 浏览次数:2 分类:技术文章

本文共 7994 字,大约阅读时间需要 26 分钟。

 

driver = $driver; } /** * Load the session for the current request. * 加载当前请求的会话。 * @param string $id * @return void */ public function load($id) { if ( ! is_null($id)) $this->session = $this->driver->load($id); // If the session doesn't exist or is invalid, we will create a new session // array and mark the session as being non-existent. Some drivers, such as // the database driver, need to know whether the session exists in storage // so they can know whether to insert or update the session. // 如果会话不存在或无效,我们将创建一个新的会话数组并将会话标记为不存在。 // 一些驱动程序,例如数据库驱动程序,需要知道会话是否存在于存储中,以便他们知道是插入还是更新会话。 if (is_null($this->session) or static::expired($this->session)) { $this->exists = false; $this->session = array('id' => Str::random(40), 'data' => array( ':new:' => array(), ':old:' => array(), )); } // A CSRF token is stored in every session. The token is used by the Form // class and the "csrf" filter to protect the application from cross-site // request forgery attacks. The token is simply a long, random string // which should be posted with each request to the application. // CSRF 令牌存储在每个会话中。 令牌由Form类和“csrf”过滤器使用,以保护应用程序免受跨站点请求伪造攻击。 // 令牌只是一个很长的随机字符串,应该随每个对应用程序的请求一起发布。 if ( ! $this->has(Session::csrf_token)) { $this->put(Session::csrf_token, Str::random(40)); } } /** * Deteremine if the session payload instance is valid. * 确定会话有效负载实例是否有效。 * The session is considered valid if it exists and has not expired. * 如果会话存在且未过期,则该会话被认为是有效的。 * @param array $session * @return bool */ protected static function expired($session) { $lifetime = Config::get('session.lifetime'); return (time() - $session['last_activity']) > ($lifetime * 60); } /** * Determine if the session or flash data contains an item. * 确定会话或闪存数据是否包含项目。 * @param string $key * @return bool */ public function has($key) { return ( ! is_null($this->get($key))); } /** * Get an item from the session. * 从会话中获取一个项 * The session flash data will also be checked for the requested item. * 还将针对请求的项检查会话闪存数据 * * // Get an item from the session * $name = Session::get('name'); * * // Return a default value if the item doesn't exist * $name = Session::get('name', 'Taylor'); * * * @param string $key * @param mixed $default * @return mixed */ public function get($key, $default = null) { $session = $this->session['data']; // We check for the item in the general session data first, and if it // does not exist in that data, we will attempt to find it in the new // and old flash data. If none of those arrays contain the requested // item, we will just return the default value. // 我们首先检查通用会话数据中的项目,如果该数据中不存在,我们将尝试在新旧闪存数据中找到它。 // 如果这些数组中没有一个包含请求的项目,我们将只返回默认值。 if ( ! is_null($value = array_get($session, $key))) { return $value; } elseif ( ! is_null($value = array_get($session[':new:'], $key))) { return $value; } elseif ( ! is_null($value = array_get($session[':old:'], $key))) { return $value; } return value($default); } /** * Write an item to the session. * 将一个项写入会话中 * * // Write an item to the session payload * Session::put('name', 'Taylor'); * * * @param string $key * @param mixed $value * @return void */ public function put($key, $value) { array_set($this->session['data'], $key, $value); } /** * Write an item to the session flash data. * 将一个项写入会话闪存数据中 * Flash data only exists for the current and next request to the application. * 闪存数据仅存在于应用程序的当前请求和下一个请求。 * * // Write an item to the session payload's flash data * Session::flash('name', 'Taylor'); * * * @param string $key * @param mixed $value * @return void */ public function flash($key, $value) { array_set($this->session['data'][':new:'], $key, $value); } /** * Keep all of the session flash data from expiring after the request. * 保持所有会话闪存数据在请求后过期。 * @return void */ public function reflash() { $old = $this->session['data'][':old:']; $this->session['data'][':new:'] = array_merge($this->session['data'][':new:'], $old); } /** * Keep a session flash item from expiring at the end of the request. * 防止会话闪存项在请求结束时过期。 * * // Keep the "name" item from expiring from the flash data * Session::keep('name'); * * // Keep the "name" and "email" items from expiring from the flash data * Session::keep(array('name', 'email')); * * * @param string|array $keys * @return void */ public function keep($keys) { foreach ((array) $keys as $key) { $this->flash($key, $this->get($key)); } } /** * Remove an item from the session data. * 从会话数据中移除一个项 * @param string $key * @return void */ public function forget($key) { array_forget($this->session['data'], $key); } /** * Remove all of the items from the session. * 从会话中移除所有的项 * The CSRF token will not be removed from the session. * * @return void */ public function flush() { $token = $this->token(); $session = array(Session::csrf_token => $token, ':new:' => array(), ':old:' => array()); $this->session['data'] = $session; } /** * Assign a new, random ID to the session. * 分配一个新的、随机的ID到会话中 * @return void */ public function regenerate() { $this->session['id'] = Str::random(40); $this->exists = false; } /** * Get the CSRF token that is stored in the session data. * 获取存储在会话数据中的CSRF令牌 * @return string */ public function token() { return $this->get(Session::csrf_token); } /** * Get the last activity for the session. * 获取会话的最后一个活动。 * @return int */ public function activity() { return $this->session['last_activity']; } /** * Store the session payload in storage. * 将会话有效负载存储在存储中。 * This method will be called automatically at the end of the request. * 此方法将在请求结束时自动调用。 * @return void */ public function save() { $this->session['last_activity'] = time(); // Session flash data is only available during the request in which it // was flashed and the following request. We will age the data so that // it expires at the end of the user's next request. // 会话刷新数据仅在刷新请求和后续请求期间可用。 我们将对数据进行老化,使其在用户下一个请求结束时过期。 $this->age(); $config = Config::get('session'); // The responsibility of actually storing the session information in // persistent storage is delegated to the driver instance being used // by the session payload. // 将会话信息实际存储在持久存储中的责任委托给会话有效负载使用的驱动程序实例。 // This allows us to keep the payload very generic, while moving the // platform or storage mechanism code into the specialized drivers, // keeping our code very dry and organized. // 这允许我们保持有效负载非常通用,同时将平台或存储机制代码移动到专门的驱动程序中,保持我们的代码非常干燥和有组织。 $this->driver->save($this->session, $config, $this->exists); // Next we'll write out the session cookie. This cookie contains the // ID of the session, and will be used to determine the owner of the // session on the user's subsequent requests to the application. // 接下来,我们将写出会话cookie。 该 cookie 包含会话的 ID,并将用于在用户对应用程序的后续请求中确定会话的所有者。 $this->cookie($config); // Some session drivers implement the Sweeper interface, meaning that // they must clean up expired sessions manually. If the driver is a // sweeper, we need to determine if garbage collection should be // run for the request. // 一些会话驱动程序实现了 Sweeper 接口,这意味着它们必须手动清理过期的会话。 // 如果驱动程序是清扫工,我们需要确定是否应该为请求运行垃圾收集。 $sweepage = $config['sweepage']; if ($this->driver instanceof Sweeper and (mt_rand(1, $sweepage[1]) <= $sweepage[0])) { $this->driver->sweep(time() - ($config['lifetime'] * 60)); } } /** * Age the session flash data. * 老化会话闪存数据。 * @return void */ protected function age() { $this->session['data'][':old:'] = $this->session['data'][':new:']; $this->session['data'][':new:'] = array(); } /** * Send the session ID cookie to the browser. * 将会话ID Cookie发送到浏览器。 * @param array $config * @return void */ protected function cookie($config) { extract($config, EXTR_SKIP); // 此处未定义的变量是bug $minutes = ( ! $expire_on_close) ? $lifetime : 0; Cookie::put($cookie, $this->session['id'], $minutes, $path, $domain, $secure); }}

github地址:    

转载地址:https://liushilong.blog.csdn.net/article/details/117429586 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【Laravel3.0.0源码阅读分析】APC会话类apc.php
下一篇:【Laravel3.0.0源码阅读分析】Redis缓存类redis.php

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月10日 07时12分28秒