Thinkphp的cookie的怎么玩?

在使用COOKIE的时候,首先要对COOKIE进行加密,加密方式采用:异位或的方式进行加密:

// 异位或加密 1是加密 0 是解密
function encrytion($value,$type=0){
    $key = md5(C('COOKIE_KEY'));
    //加密
    if($type){
        return str_replace('=','',base64_encode($value ^ $key));
    };
    $value = base64_decode($value);
    return $value ^ $key;
}

常用配置:

// 加密Cookie
'COOKIE_KEY' => md5('Md)guW`X7jREqCb|F%?!UhO_KPIY-:oBV^=(AyD['),
'COOKIE_LOGIN_TIME' => time() +3600*24*7,

简单尝试:

public function index(){
  $value = 'cj|昆明';
  //加密
  $key = encrytion($value,1);
  echo $key;
  echo "<hr>";
  //解密
  $val = encrytion($key,0);
  echo $val;
}
public function cun(){
  $city = 'cj';
  $city = encrytion($city,1);
  echo $city;
  cookie('name','value',3600);
  $res = @setcookie('city',$city,C('COOKIE_LOGIN_TIME'),'/');
  echo $res;
}
public function qu(){
  //$city = $_COOKIE['city']; 可用
  $city = cookie();
  $city = $city['city'];
  p($city);
  $city = encrytion($city,0);
  p($city);
}

 

posted @ 2017-08-27 22:13  帅到要去报警  阅读(946)  评论(0编辑  收藏  举报