1. 程式人生 > >thinkphp——PHP異位或加密實現自動登陸

thinkphp——PHP異位或加密實現自動登陸

thinkphp php 自動登陸

最近做一個網站,需要實現自動登陸的功能,代碼如下:


HTML代碼

<form onsubmit="return false">
        <div class="pop-content accountPage">
            <div class="form_item">
                <div class="item_tip">用戶名/郵箱</div>
                <input tabindex="1" name="username" class="form_input" id="username" onblur="blurInputLoginBox($(this))" onfocus="focusInputLoginBox($(this))" autocomplete="off" type="text">
                <span class="error">不能為空</span>
                <i class="icon-loginright"></i>
                <span class="grey">允許用中英文、數字、下劃線,提交後不可修改</span>
            </div>
            <div class="form_item">
                <div class="item_tip">密碼</div>
                <input tabindex="3" name="pwd" class="form_input" id="pwd" onblur="blurInputLoginBox($(this))" onfocus="focusInputLoginBox($(this))" autocomplete="off" type="password">
                <span class="error">不能為空</span>
                <span class="grey">6 - 20位</span>
                <i class="icon-loginright"></i>
            </div>
            <div class="twoWeeks">
                <input class="ckeckBox" name="rememberme" id="rememberme" type="checkbox">
                <label for="rememberme">兩周內免登錄</label>
                <a class="a_underline" href="http://www.sucaihuo.com/forget.html">忘記密碼?</a>
            </div>
            <p class="notice_error" id="notice_error"></p>
            <input tabindex="7" value="登&nbsp;&nbsp;錄" class="btn_reg btn" id="btn_reg" onclick="subLogin($(this))" type="submit">
            <div class="co_login" style="margin:30px 0 0">
                聯合登錄
                <a class="a_underline" href="http://www.sucaihuo.com/Index/login/type/qq.html">騰訊QQ</a>
                <a class="a_underline" href="http://www.sucaihuo.com/Index/login/type/sina.html">新浪微博</a>
                <a class="a_underline" href="http://www.sucaihuo.com/Index/login/type/renren.html">人人網</a>
                綁定送<span class="red">200</span>積分
            </div>
        </div>
    </form>


Ajax提交登陸信息,提交地址自行修改

<script type="text/javascript">
    function subLogin(obj) {
        var username = $.trim($(‘#username‘).val());
        var pwd = $.trim($(‘#pwd‘).val());
        var remember;
        if($(‘#rememberme‘).is(":checked")){
            remember=1;
        }else{
            remember=2;
        }
        if (username == ‘‘) {
            showLoginError("請輸入用戶名或郵箱");
            return false;
        }
        if (pwd == ‘‘) {
            showLoginError("請輸入密碼");
            return false;
        }
        obj.addClass(‘disabled‘).val(‘登錄中...‘);
        $.post(controller+‘Ajax/checkLogin‘, {username: username, pwd: pwd,rememberme:remember}, function(data) {
            if (data.error != ‘‘) {
                showLoginError(data.error);
            } else {
                showSuccessTip(data.success);
                var url = data.url;
                setTimeout("goUrl(‘" + url + "‘)", 1000);
            }
            obj.removeClass(‘disabled‘).val(‘登錄‘);
        }, "json")
    }
    function showLoginError(tip) {
        $("#notice_error").text(tip).show();
        $("#notice_error").fadeOut(2500);
    }
</script>


在Common/function.PHP

/**  
 * 異位或加密字符串  
 * @param [String] $value [需要加密或解密的字符串]  
 * @param [integer] $type [0:加密 1:解密]  
 * @param [String]        [返回加密或解密的字符串]  
 */
function encryption($value,$type=0){
$key = md5(C(‘AUTO_LOGIN_KEY‘));
//$type==1則加密cookie
if($type==1){
return str_replace(‘=‘,‘‘,base64_encode($value ^ $key));
}
//解密
$value = base64_decode($value);
return $value ^ $key;
}

PHP

//是否要記錄cookie

if(I(‘rememberme‘)==1){

$value = $user[‘id‘].‘|‘.get_client_ip().‘|‘.$user[‘username‘];

$value = encryption($value,1);

cookie(‘sucai‘,$value,array(‘expire‘=>time()+3600*24*7*2,‘path‘=>‘/‘));

}


在Home/Controller/CommonController.class.php中

if(isset($_COOKIE(‘sucai‘)){
                        //解析cookie,生成session
$value = encryption(cookie(‘sucai‘));
$value = explode(‘|‘,$value);
if($value[‘1‘]==get_client_ip()){
session(‘uid‘,$value[0]);
session(‘username‘,$value[‘2‘]);
}
}


本文出自 “榕樹下的說書人” 博客,請務必保留此出處http://woaijorden.blog.51cto.com/5299493/1956045

thinkphp——PHP異位或加密實現自動登陸