php存储与检索session、cookie的类

发布时间:2019-10-21编辑:脚本学堂
本文介绍下,php实现的存储与检索session、cookie的类,是学习php中session、cookie操作以及面向对象编程的好例子。有需要的朋友参考下吧。

php实现的操作session与cookie的类,可以对session、cookie进行加密与解密。

代码:

<?php 
/**
* session与cookie操作类
* by www.jb200.com
*/
class Session 
{ 
    private $use_cookie; 
    private $preStr; 
    private $maxCookie; 
    private $cookieLenght; 
    private $stringone; 
    private $duratacookie; 
    private $secret; 

    public function __construct ($cook = "zipcook") { 

        $this->use_cookie = $cook;    //choose mode 
        $this->preStr= "_KK_";        //prefix for cookies 
        $this->maxCookie=20;        //since cookie lenght is limited, I've limited the number of cookies 
        $this->cookieLenght=3096;    //max cookie length (it depends on browser) 
        $this->duratacookie=3600*24;//cookie life time 
        $this->secred="secret";        //secret keyword to crypt/decrypt, change this to customize encryption 
        if ($this->use_cookie=="yes") { 
        } elseif ($this->use_cookie=="zipcook") { 
            $this->stringone = $this->prelevaStringaTotale(); 
        } else { 
            ini_set("session.gc_maxlifetime","432000"); 
            ini_set("url_rewriter.tags",""); 
            ini_set("session.use_trans_sid", false); 
            session_start(); 
        } 
    } 

    /* --------------------------- */ 
    /* parse_str 变量             */ 
    /* -------------------------- */ 
    private function build_str($ar) { 
        $qs = array(); 
        foreach ($ar as $k => $v) { $qs[] = $k.'='.$v; } 
        return join('&', $qs); 
    } 

    /* ------------------------------ */ 
    /* 获取加密后的cookie列表  */ 
    /* cookies                       */ 
    /* ----------------------------- */ 
    private function prelevaStringaTotale() { 
        $cookiesSet = array_keys($_COOKIE); 
        $out = ""; 
        for ($x=0;$x<count($cookiesSet);$x++) { 
            if (strpos(" ".$cookiesSet[$x],$this->preStr)==1) 
                $out.=$_COOKIE[$cookiesSet[$x]]; 
        } 
        return $this->decrypta($out); 
    } 

    public function debug() { 
        // for debug 
        return $this->prelevaStringaTotale(); 
    } 

    /* ---------------------------- */ 
    /* 确定可用的cookies            */ 
    /* ---------------------------- */ 
    private function calcolaCookieLiberi() { 
        $cookiesSet = array_keys($_COOKIE); 
        $c=0; 
        for ($x=0;$x<count($cookiesSet);$x++) { 
            if (strpos(" ".$cookiesSet[$x],$this->preStr)==1) 
                $c+=1; 
        } 
        return $this->maxCookie - count($cookiesSet) + $c; 
    } 

    /* ------------------------ */ 
    /* 分割字符串存储进cookie   */ 
    /* ------------------------ */ 
    private function my_str_split($s,$len) { 
        $output = array(); 
        if (strlen($s)<=$len) { 
            $output[0] = $s; 
            return $output; 
        } 
        $i = 0; 
        while (strlen($s)>0) { 
            $s = substr($s,0,$len); 
            $output[$i]=$s; 
            $s = substr($s,$len); 
            $i++; 
        } 
        return $output; 
    } 

    /* ----------------------------- */ 
    /* 保存session与cookie值         */ 
    /* ----------------------------- */ 
    public function register($var,$value) { 
        $this->set($var,$value); 
    } 
    public function set($var,$value) { 
        if ($this->use_cookie=="yes") { 
            setcookie($var,$this->crypta($value),time()+$this->duratacookie,"/", $_SERVER['HTTP_HOST'] ); 
        } elseif ($this->use_cookie=="zipcook") { 
            if ($this->stringone!="") { 
                parse_str($this->stringone, $vars); 
            } else { 
                $vars=array(); 
            } 
            $vars[$var] = $value;    //aggiungo-modifico valore 
            $str = $this->crypta($this->build_str($vars)); 
            $arr = $this->my_str_split($str,$this->cookieLenght); 
            $cLiberi = $this->calcolaCookieLiberi(); 
            if (count($arr) < $cLiberi) { 
                // c'ho spazio, posso registrare 
                $this->stringone = $this->build_str($vars); 
                for ($i=0;$i<count($arr);$i++) { 
                    setcookie($this->preStr.$i,$arr[$i],time()+$this->duratacookie,"/", $_SERVER['HTTP_HOST'] ); 
                } 
            } else { 
                //cookie overflow 
                return "errore cookie overflow"; 
            } 
        } else { 
            session_register($var); 
        } 
    } 

    /* ------------------------------------------- */ 
    /* 获取加密后的cookie值,未加密,请直接取得  */ 
    /* ------------------------------------------- */ 
    public function get($var) { 
        if ($this->use_cookie=="yes") { 
            global $_COOKIE; 
            return $this->decrypta($_COOKIE[$var]); 
        } elseif ($this->use_cookie=="zipcook") { 
            if ($this->stringone!="") { 
                parse_str($this->stringone, $vars); 
            } else { 
                return ""; 
            } 
            if(!isset($vars[$var])) { 
                return ""; 
            } 
            return $vars[$var]; 
        } else { 
            if ($this->is_registered($var)) { 
                $this->$var=$GLOBALS[$var]; 
            } 
            else 
                if(isset($GLOBALS[$var])) 
                    $this->$var = $GLOBALS[$var]; 
                else 
                        $this->$var=""; 
            return($this->$var); 
        } 
    } 

    /* --------------------------- */ 
    /* 清空session或cookie        */ 
    /* -------------------------- */ 
    public function finish() { 
        if ($this->use_cookie=="yes") { 
            $cookiesSet = array_keys($_COOKIE); 
            for ($x=0;$x<count($cookiesSet);$x++) { 
                //echo $cookiesSet[$x]."<br/>"; 
                setcookie($cookiesSet[$x],"",time()-3600*24,"/", $_SERVER['HTTP_HOST'] );    //faccio scadere il cookie 
            } 

        } elseif ($this->use_cookie=="zipcook") { 
            $cookiesSet = array_keys($_COOKIE); 
            for ($x=0;$x<count($cookiesSet);$x++) { 
                if (strpos(" ".$cookiesSet[$x],$this->preStr)==1) 
                    setcookie($cookiesSet[$x],"",time()-3600*24,"/",$_SERVER['HTTP_HOST']); 
                $this->stringone=""; 
            } 
        } else { 
            session_destroy(); 
            $_SESSION = array(); 
        } 
    } 

    /* 加密 */ 
    private function crypta($t){ 
        if ($t=="") return $t; 
        $r = md5(10); $c=0; $v=""; 
        for ($i=0;$i<strlen($t);$i++){ 
            if ($c==strlen($r)) $c=0; 
            $v.= substr($r,$c,1) . (substr($t,$i,1) ^ substr($r,$c,1)); 
            $c++; 
        } 
        return (base64_encode($this->ed($v))); 
    } 
    /* 解密 */ 
    private function decrypta($t) { 
        if ($t=="") return $t; 
        $t = $this->ed(base64_decode(($t))); 
        $v = ""; 
        for ($i=0;$i<strlen($t);$i++){ 
            $md5 = substr($t,$i,1); 
            $i++; 
            $v.= (substr($t,$i,1) ^ $md5); 
        } 
        return $v; 
    } 

    /* used to crypt/decrypt */ 
    private function ed($t) { 
        $r = md5($this->secret); $c=0; $v=""; 
        for ($i=0;$i<strlen($t);$i++) { 
            if ($c==strlen($r)) $c=0; 
            $v.= substr($t,$i,1) ^ substr($r,$c,1); 
            $c++; 
        } 
        return $v; 
    } 


} 
?>

分享两个实例,学习这个类的用法。
例1:

<? 
//test1.php 
include("session.class.php"); 
$s = new Session(); 
$s->set("userid","27"); 
?>

例2:

<? 
//test2.php 
include("session.class.php"); 
$s = new Session(); 
echo $s->get("userid"); //27 
?>