php开发记录多少天自动登录信息(2)

3301
<div id='login-form'>
    <div id='login-wrap'>
    <p>还没有微博帐号?<a href='{:U("register")}'>立即注册</a></p>
    <form action="{:U('login')}" method='post' name='login'>
        <fieldset>
            <legend>用户登录</legend>
            <p>
                <label for="account">登录账号:</label>
                <input type="text" name='account' class='input'/>
            </p>
            <p>
                <label for="pwd">密码:</label>
                <input type="password" name='pwd' class='input'/>
            </p>
            <p>
                <input type="checkbox" name='auto' checked='1' class='auto' id='auto'/>
                <label for="auto">下次自动登录</label>
            </p>
            <p>
                <input type="submit" value='马上登录' id='login'/>
            </p>
        </fieldset>
    </form>
    </div>
</div>


//登录控制器处理是否自动登录

//处理下一次自动登录
if (isset($_POST['auto'])) {
    $account = $user['account'];
    $ip = get_client_ip();
    $value = $account . '|' . $ip;
    $value = encryption($value);
    @setcookie('auto', $value, C('AUTO_LOGIN_TIME'), '/');
}


//检测是否自动登录

Public function _initialize () {
    //处理自动登录
    if (isset($_COOKIE['auto']) && !isset($_SESSION['uid'])) {
        $value = explode('|', encryption($_COOKIE['auto'], 1));
        $ip = get_client_ip();
        //本次登录IP与上一次登录IP一致时
        if ($ip == $value[1]) {
        $account = $value[0];
        $where = array('account' => $account);
        $user = M('user')->where($where)->field(array('id', 'lock'))->find();
        //检索出用户信息并且该用户没有被锁定时,保存登录状态
        if ($user && !$user['lock']) {
        session('uid', $user['id']);
        }
        }
        }
        //判断用户是否已登录
        if (!isset($_SESSION['uid'])) {
        redirect(U('Login/index'));
    }
}


/**
 * 异位或加密字符串
 * @param  [String]  $value [需要加密的字符串]
 * @param  [integer] $type  [加密解密(0:加密,1:解密)]
 * @return [String]         [加密或解密后的字符串]
 */
function encryption ($value, $type=0) {
    $key = md5(C('ENCTYPTION_KEY'));
    if (!$type) {
       return str_replace('=', '', base64_encode($value ^ $key));
    }
    $value = base64_decode($value);
    return $value ^ $key;
}