[李景山php]每天TP5-20161230|thinkphp5-Cookie.php

來源:互聯網
上載者:User

標籤:thinkphp

<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <[email protected]>// +----------------------------------------------------------------------namespace think;// 根包 之下class Cookie{//Cookie 二次封裝    protected static $config = [// cookie 配置項        // cookie 名稱首碼        ‘prefix‘    => ‘‘,// cookie 名稱首碼        // cookie 儲存時間        ‘expire‘    => 0,// cookie 儲存時間        // cookie 儲存路徑        ‘path‘      => ‘/‘, // 儲存路徑        // cookie 有效網域名稱        ‘domain‘    => ‘‘, // 有效網域名稱        //  cookie 啟用安全傳輸        ‘secure‘    => false,// 啟用安全傳輸        // httponly設定        ‘httponly‘  => ‘‘,// httponly 設定        // 是否使用 setcookie        ‘setcookie‘ => true, // 使用 setcookie    ];    protected static $init;// 靜態初始化 變數    /**     * Cookie初始化     * @param array $config     * @return void     */    public static function init(array $config = [])// Cookie 初始化    {        if (empty($config)) {// 如果配置參數為空白            $config = Config::get(‘cookie‘);// 擷取配置選項        }        self::$config = array_merge(self::$config, array_change_key_case($config));// 配置項合并        if (!empty(self::$config[‘httponly‘])) {// 如果非空設定 httponly            ini_set(‘session.cookie_httponly‘, 1);// 使用設定 session 配置項        }        self::$init = true;// 設定初始化完成    }    /**     * 設定或者擷取cookie範圍(首碼)     * @param string $prefix     * @return string|void     */    public static function prefix($prefix = ‘‘)// 設定 或者 擷取 範圍 首碼    {        if (empty($prefix)) {            return self::$config[‘prefix‘];        }        self::$config[‘prefix‘] = $prefix;    }    /**     * Cookie 設定、擷取、刪除     *     * @param string $name  cookie名稱     * @param mixed  $value cookie值     * @param mixed  $option 選擇性參數 可能會是 null|integer|string     *     * @return mixed     * @internal param mixed $options cookie參數     */    public static function set($name, $value = ‘‘, $option = null)// 設定 擷取 刪除    {        !isset(self::$init) && self::init();// 確認初始化 這個用法也不錯        // 參數設定(會覆蓋黙認設定)        if (!is_null($option)) {// 存在選項            if (is_numeric($option)) {// 選項為數字                $option = [‘expire‘ => $option];// 則預設為到期時間            } elseif (is_string($option)) {// 字串                parse_str($option, $option);// 解析字串            }            $config = array_merge(self::$config, array_change_key_case($option));// 合并配置項        } else {            $config = self::$config;        }        $name = $config[‘prefix‘] . $name;// 設定cookie 儲存名字        // 設定cookie        if (is_array($value)) {// 儲存 值是 數組的資料,先格式化,然後輸出            array_walk_recursive($value, ‘self::jsonFormatProtect‘, ‘encode‘);            $value = ‘think:‘ . json_encode($value);        }        $expire = !empty($config[‘expire‘]) ? $_SERVER[‘REQUEST_TIME‘] + intval($config[‘expire‘]) : 0;// 設定到期時間        if ($config[‘setcookie‘]) {// 通過 setcookie 方式設定            setcookie($name, $value, $expire, $config[‘path‘], $config[‘domain‘], $config[‘secure‘], $config[‘httponly‘]);        }        $_COOKIE[$name] = $value;// 普通賦值方式    }    /**     * 判斷Cookie資料     * @param string        $name cookie名稱     * @param string|null   $prefix cookie首碼     * @return bool     */    public static function has($name, $prefix = null)    {        !isset(self::$init) && self::init();        $prefix = !is_null($prefix) ? $prefix : self::$config[‘prefix‘];        $name   = $prefix . $name;        return isset($_COOKIE[$name]);// 判讀是否有值    }    /**     * Cookie擷取     * @param string        $name cookie名稱     * @param string|null   $prefix cookie首碼     * @return mixed     */    public static function get($name, $prefix = null)    {        !isset(self::$init) && self::init();// 初始化        $prefix = !is_null($prefix) ? $prefix : self::$config[‘prefix‘];// 擷取 cookie 儲存 key        $name   = $prefix . $name;// 擷取完成        if (isset($_COOKIE[$name])) {//如有有資料            $value = $_COOKIE[$name];            if (0 === strpos($value, ‘think:‘)) {// 如果有 think: 並且是開頭位置                $value = substr($value, 6);                $value = json_decode($value, true);                array_walk_recursive($value, ‘self::jsonFormatProtect‘, ‘decode‘);// 返回資料            }            return $value;        } else {            return null;        }    }    /**     * Cookie刪除     * @param string        $name cookie名稱     * @param string|null   $prefix cookie首碼     * @return mixed     */    public static function delete($name, $prefix = null)    {        !isset(self::$init) && self::init();        $config = self::$config;        $prefix = !is_null($prefix) ? $prefix : $config[‘prefix‘];        $name   = $prefix . $name;        if ($config[‘setcookie‘]) {// 刪除輸出            setcookie($name, ‘‘, $_SERVER[‘REQUEST_TIME‘] - 3600, $config[‘path‘], $config[‘domain‘], $config[‘secure‘], $config[‘httponly‘]);        }        // 刪除指定cookie        unset($_COOKIE[$name]);// 刪除 資料    }    /**     * Cookie清空     * @param string|null $prefix cookie首碼     * @return mixed     */    public static function clear($prefix = null)    {        // 清除指定首碼的所有cookie        if (empty($_COOKIE)) {            return;        }// 為空白,自動清除        !isset(self::$init) && self::init();        // 要刪除的cookie首碼,不指定則刪除config設定的指定首碼        $config = self::$config;        $prefix = !is_null($prefix) ? $prefix : $config[‘prefix‘];        if ($prefix) {// 清除 全部帶首碼 的 cookie            // 如果首碼為空白字串將不作處理直接返回            foreach ($_COOKIE as $key => $val) {                if (0 === strpos($key, $prefix)) {                    if ($config[‘setcookie‘]) {// 清除自己的cookie                        setcookie($key, ‘‘, $_SERVER[‘REQUEST_TIME‘] - 3600, $config[‘path‘], $config[‘domain‘], $config[‘secure‘], $config[‘httponly‘]);                    }                    unset($_COOKIE[$key]);                }            }        }        return;    }    private static function jsonFormatProtect(&$val, $key, $type = ‘encode‘)    {// 弄個半天是假的        if (!empty($val) && true !== $val) {            $val = ‘decode‘ == $type ? urldecode($val) : urlencode($val);        }    }}// 總結,這個就關注了幾個事情//1 由於增加了 首碼,需要重新封裝一遍//2 根據 setcookie 函數 是否好用, 選擇不同的操作方式//3 設定了 httponly 的方式


本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1876451

[李景山php]每天TP5-20161230|thinkphp5-Cookie.php

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.