如何设置Cookie

如何设置Cookie

Cookie算法:

"用户id" + "过期时间" + MD5("用户id" + "用户口令" + "过期时间" + "SecretKey")

代码实现:



define('SecretKey', 'mogublog');

function is_login(){
    if(!isset($_COOKIE['mogublog_login'])){
        return false;
    }
    $cookie = explode('-', $_COOKIE['mogublog_login']);
    if(count($cookie) != 3){
        return false;
    }
    $uid = intval($cookie[0]);
    $expire = intval($cookie[1]);
    $pwd = '123456';
    $cookie_secret = md5($uid.$pwd.$expire.SecretKey);
    if($cookie_secret === $cookie[2]){
        return true;
    }else{
        return false;
    }
}

function set_login_cookie($uid, $expire){
    $uid = intval($uid);
    $pwd = '123456';
    $expire = time() + $expire;
    $cookie = $uid.'-'.$expire.'-'.md5($uid.$pwd.$expire.SecretKey);
    setcookie('mogublog_login', $cookie, $expire, '/', '', '', true);
}