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(!emptyempty($_POST["remember_pass"]))
- {
- setcookie("username", $user,$cookie_timeout );
- setcookie("password", $old_password, $cookie_timeout);
- setcookie("remember_pass", true, $cookie_timeout);
-
- }else{
-
- setcookie("username", null, $cookie_timeout);
- setcookie("password", null, $cookie_timeout);
- setcookie("remember_pass", null, $cookie_timeout);
- }
- if(!emptyempty($_POST["auto_login"]))
- {
- setcookie("auto_login", true, $cookie_timeout);
- }else{
- setcookie("auto_login", null, $cookie_timeout);
- }
下次登录时,先从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)){
- var usernameObj=com.whuang.hsj.$$one('user');
- usernameObj.value=username;
- }
- if(com.whuang.hsj.isHasValue(password)){
- 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);
- }
依赖的js方法:
- var dealAutoSubmit=function(this22){
- var isChecked=com.whuang.hsj.isCheckcheckbox(this22);
-
- if(isChecked){
- if(!com.whuang.hsj.isCheckcheckbox("remember_pass")){
- com.whuang.hsj.setCheckedCheckboxOne("remember_pass");
- }
-
- }
- }
-
-
-
- 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;
- };
-
-
-
-
-
-
- com.whuang.hsj.$$one = function(name22) {
- if (com.whuang.hsj.isHasValue(name22)) {
- var names222=document.getElementsByName(name22);
-
-
- var className=Object.prototype.toString.call(names222);
- var boolean_isArray;
- var ieHtmlCollection='[object HTMLCollection]';
- if(isIEtest)
- {
- 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;
- }
- }else{
- return "";
- }
- } else {
- return "";
- }
- };
说明:
com.whuang.hsj.isHasValue 是js方法,用于判断是否有值
php中判断是否是移动端访问网页
是否是手机
- function is_mobile()
- {
- $user_agent = $_SERVER['HTTP_USER_AGENT'];
- $mobile_agents = Array("240x320","acer",
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_33670713/article/details/90618730
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
-
发表于 2020-03-01 19:54:04
- 阅读 ( 957 )
- 分类: