php 中如何记住用户名和密码呢?
前台页面:
?前台html代码如下:
<div class="yhxx">
<div>
<label>
<span class="wz">用户名:</span>
<span class="wb">
<input type="text" name="user" value="" class="text">
</span>
</label>
</div>
<div><label><span class="wz">密码:</span>
<span class="wb"> <input type="password" name="password" class="text" onfocus="passwordFocus();">
</span></label>
</div>
<div id="remember_pass_div" style="height: 20px">
<!-- 记住密码 -->
<label> <input class="checkbox2" value="remember_pass" name="remember_pass" style="margin-right: 5px;" type="checkbox" label="记住密码" checked="checked">
<span class="checkbox2 cannot_select" style="margin-right: 40px;">记住密码</span>
</label>
<!-- 自动登录 -->
<label> <input class="checkbox2" value="auto_login" name="auto_login" style="margin-right: 5px;" type="checkbox" label="自动登录" onclick="dealAutoSubmit(this);">
<span class="checkbox2 cannot_select">自动登录</span>
</label>
</div>
<div style="display:none">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody><tr>
<td style="width:60px;"><span class="wz"> 验证码:</span></td>
<td style=" text-align:left!important; text-align:center; width:130px;"><label><span class="wb"><input type="text" name="yzm" class="text3"> </span></label>
</td>
<td valign="middle">
<img onclick="get_randfunc(this);" style="padding-top:3px; cursor:pointer;" src="../includes/rand_func.php">
</td>
</tr>
</tbody></table>
</div>
</div>
?
提交之后,在后台进行如下处理
接收前台提交的参数:
$user=trim($_POST["user"]);
$old_password=trim($_POST["password"]);
?如果记住用户名,则保存用户到cookie中:
$cookie_timeout=time()+3600*24*365;
if(!empty($_POST["remember_pass"]))
{
setcookie("username", $user,$cookie_timeout );
setcookie("password", $old_password, $cookie_timeout);
setcookie("remember_pass", true, $cookie_timeout);
// echo "记住我";
}else{
// echo "不记住";
setcookie("username", null, $cookie_timeout);
setcookie("password", null, $cookie_timeout);
setcookie("remember_pass", null, $cookie_timeout);
}
if(!empty($_POST["auto_login"]))
{
setcookie("auto_login", true, $cookie_timeout);
}else{
setcookie("auto_login", null, $cookie_timeout);
}
?
下次登录时,先从cookie获取用户和密码:
//获取cookie
var username="<?php echo $_COOKIE[‘username‘] ?>";
var password="<?php echo $_COOKIE[‘password‘] ?>";
var remember_pass="<?php echo $_COOKIE[‘remember_pass‘] ?>";
var auto_login="<?php echo $_COOKIE[‘auto_login‘] ?>";
if(com.whuang.hsj.isHasValue(remember_pass) && remember_pass==1){//是否记住密码
if(com.whuang.hsj.isHasValue(username)){//cookie中的用户名不为空
var usernameObj=com.whuang.hsj.$$one(‘user‘);
usernameObj.value=username;
}
if(com.whuang.hsj.isHasValue(password)){//cookie中的密码不为空
var passwordObj=com.whuang.hsj.$$one(‘password‘);
passwordObj.value=password;
}
$(‘div input[name=remember_pass]‘).attr("checked",‘true‘);
}else{
$(‘div input[name=remember_pass]‘).attr("checked",null);
}
if(com.whuang.hsj.isHasValue(auto_login) && auto_login==1){//是否自动登录
$(‘div input[name=remember_pass]‘).attr("checked",‘true‘);
$(‘div input[name=auto_login]‘).attr("checked",‘true‘);
// document.forms[0].submit();
timingLogin=setTimeout(function(){document.forms[0].submit();},2000);//1秒钟之后自动登录
}
?依赖的js方法:
var dealAutoSubmit=function(this22){
var isChecked=com.whuang.hsj.isCheckcheckbox(this22);
// console.log(isChecked);
if(isChecked){
if(!com.whuang.hsj.isCheckcheckbox("remember_pass")){
com.whuang.hsj.setCheckedCheckboxOne("remember_pass");
}
}
}
/******************************
* select the single checkbox
*/
com.whuang.hsj.setCheckedCheckboxOne = function(checkbox2233) {
if (typeof checkbox2233 == ‘string‘) {
checkbox2233 = com.whuang.hsj.$$one(checkbox2233);
if(checkbox2233==null ||checkbox2233==undefined){
checkbox2233=com.whuang.hsj.$$id(checkbox2233);
}
}
checkbox2233.checked = true;
};
/***
* if is radio ,please use com.whuang.hsj.$$arr
* @param name22
* @returns
*/
com.whuang.hsj.$$one = function(name22) {
if (com.whuang.hsj.isHasValue(name22)) {
var names222=document.getElementsByName(name22);
//alert("names222:"+names222);
//alert("typeof:"+(typeof names222 ));
var className=Object.prototype.toString.call(names222);
var boolean_isArray;
var ieHtmlCollection=‘[object HTMLCollection]‘;
if(isIEtest)//if browser is IE
{
boolean_isArray=( className=== ‘[object Object]‘) ||(className=== ieHtmlCollection) ||names222 instanceof Array ;
}else
{
boolean_isArray=( className=== ‘[object Array]‘) ||(className=== ‘[object NodeList]‘ )||(className==ieHtmlCollection)||names222 instanceof Array||names222 instanceof NodeList;
}
if(names222){
if(boolean_isArray){
return names222[0];
}else{
return names222;//why add [0] ??
}
}else{
return "";
}
} else {
return "";
}
};
?
说明:
com.whuang.hsj.isHasValue 是js方法,用于判断是否有值
原文:http://hw1287789687.iteye.com/blog/2170512