COOKIE 类代码

388 查看

1.Cookie.php

<?php
// if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package        CodeIgniter
 * @author        ExpressionEngine Dev Team
 * @copyright    Copyright (c) 2008 - 2009, EllisLab, Inc.
 * @license        http://codeigniter.com/user_guide/license.html
 * @link        http://codeigniter.com
 * @since        Version 1.0
 * @filesource
 */
class Cookie
{
    const COOKIE_PREFIX = 'jt018_';         // cookie ǰ׺
    const COOKIE_EXPIRE = '86400';
    const COOKIE_PATH   = '/';              // cookie 
    const COOKIE_DOMAIN = '.jt018.com';  // cookie

    /**
     * Cookie
     */
    static public function is_set($name)
    {
        $name   = md5($name);
        return isset($_COOKIE[self::COOKIE_PREFIX.$name]);
    }


    /**
     * Cookieֵ
     */
    static function get($name) {
        $value  = null;
        $name   = md5($name);
        if(isset($_COOKIE[self::COOKIE_PREFIX.$name])){
            $value  = $_COOKIE[self::COOKIE_PREFIX.$name];
            $value  =  unserialize(base64_decode($value));
        }

        return $value;
    }

    /**
     * Cookieֵ
     */
    static function set($name,$value='',$expire='',$path='',$domain='')
    {
        $name = md5($name);
        if(empty($expire)) {
            $expire =   self::COOKIE_EXPIRE;
        }
        if(empty($path)) {
            $path = self::COOKIE_PATH;
        }
        if(empty($domain)) {
            $defaultCookieDomain = '';
            $domain =   !empty($defaultCookieDomain) ? $defaultCookieDomain : self::COOKIE_DOMAIN;
        }
        $expire =   !empty($expire) ? time()+$expire : 0;

        if(!empty($value)) $value = base64_encode(serialize($value));
        setcookie(self::COOKIE_PREFIX.$name, $value,$expire,$path,$domain);
        $_COOKIE[self::COOKIE_PREFIX.$name] = $value;
    }

    /**
     * Cookieֵ
     */
    static function remove($name)
    {
        static::set($name,'',time()-3600);
        unset($_COOKIE[self::COOKIE_PREFIX.md5($name)]);
    }

    /**
     * Cookieֵ
     */
    static function clear() {
        unset($_COOKIE);
    }


    /**
     *=======================================================================
     * 
     *=======================================================================
     * Cookie::set('auth', Cookie::authcode("$password32-_-$username-_-$userid-_-$bbsid-_-$xj-_-$niname", 'ENCODE'), 86400*30);
     * $arr_cookie = explode('-_-',Cookie::authcode(Cookie::get('auth'),'DECODE'));
     */
    static public function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
    {
        $ckey_length = 4;
        $key = md5($key ? $key : md5('bafc1eVx2RzuEM46'));
        $keya = md5(substr($key, 0, 16));
        $keyb = md5(substr($key, 16, 16));
        $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

        $cryptkey = $keya.md5($keya.$keyc);
        $key_length = strlen($cryptkey);

        $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
        $string_length = strlen($string);

        $result = '';
        $box = range(0, 255);

        $rndkey = array();
        for($i = 0; $i <= 255; $i++) {
            $rndkey[$i] = ord($cryptkey[$i % $key_length]);
        }

        for($j = $i = 0; $i < 256; $i++) {
            $j = ($j + $box[$i] + $rndkey[$i]) % 256;
            $tmp = $box[$i];
            $box[$i] = $box[$j];
            $box[$j] = $tmp;
        }

        for($a = $j = $i = 0; $i < $string_length; $i++) {
            $a = ($a + 1) % 256;
            $j = ($j + $box[$a]) % 256;
            $tmp = $box[$a];
            $box[$a] = $box[$j];
            $box[$j] = $tmp;
            $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
        }

        if($operation == 'DECODE') {
            if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
                return substr($result, 26);
            } else {
                return '';
            }
        } else {
            return $keyc.str_replace('=', '', base64_encode($result));
        }
    }


    // if($_COOKIE['ooo_auth']!=''){
    //     $arr_cookie = explode('-_-',authcode($_COOKIE['ooo_auth'],'DECODE'));
    //     $username=$arr_cookie[1];
    //     $userid=intval($arr_cookie[2]);
    //     $password=$arr_cookie[0];
    //     $bbsid=$arr_cookie[3];
    //     $xj=$arr_cookie[4];
    //     $fx_sinaid=$arr_cookie[5];
    //     $fx_niname=$arr_cookie[6];

    //     # 2015-07-08 
    //     $userNickName = $arr_cookie[5];
    // }


}

2.引用

设置cookie

$this->load->library('Cookie');
$data;
Cookie::set('mobilecode', Cookie::authcode(serialize('$data'), 'ENCODE'), 86400*30);

拿取cookie

$this->load->library('Cookie');
$session_phone = unserialize(Cookie::authcode(Cookie::get('mobile'),'DECODE'));
$yanzheng_code =unserialize(Cookie::authcode(Cookie::get('mobilecode'),'DECODE'));