如何在不使用javascript或jquery中的表单的情况下在登录时创建记住我的功能 [英] How to create a remember me function in login without using form in javascript or jquery

查看:61
本文介绍了如何在不使用javascript或jquery中的表单的情况下在登录时创建记住我的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎完成了我的项目,但是登录规范尚未100%完成. 记住我的功能尚不起作用.

I'm almost done with my project, but the specifications for my login is not yet 100% done. Remember me function is not yet working.

HTML

<input type="text" id="signinId" class="email-input" placeholder="Email">
<input type="password" id="signinPwd" class="password-input" placeholder="Password">


<input id="rememberChkBox" type="checkbox">
<button id="Sign" class="button">Login</button>

jQuery脚本

$(document).ready(function(){
    $("#Sign").click(function(){

         //Script for login is here...

    });
});

我不在此登录页面中使用表格. 规范是,在登录时选中该复选框时,必须记住该用户.注销后,该文本框应为空,并且在单击 #signinId 输入时,必须显示其用户名.

I am not using form in this login page. The specs is that when the checkbox is checked upon login, the user must be remebered. Once he logout, the textbox should be empty and when clicked on the #signinId input his username must be displayed.

如果可能的话,它应该看起来像这样:

It should look like this, if it is possible:

场景快照1: 单击电子邮件输入时

截屏2:自动填充

非常感谢您的帮助. 预先谢谢你.

Any help is very much appreciated. Thank you in advance.

推荐答案

自动登录系统.

记住我"实际上并不用于在登录表单上显示用户名和密码,因为任何浏览器均已提供此功能.但是它旨在在用户重新访问网络时自动登录.

'Remember me' is not actually used to display user name and password on the login form because this feature is already provided by any browser. But it is intended for automatic login when users revisit the web.

要使自动登录系统不在cookie上存储登录凭证,是通过添加存储userid和login_string的数据库表来实现的.

To make auto login system without storing login credential on cookie is by adding database table that store userid and login_string.

在每次用户登录时插入/更新login_string.

Insert/Update the login_string every user login.

当用户使用记住我登录时(按单选按钮),然后将login_string存储在cookie中.

And when user login using remember me (press the radio button), then store the login_string on the cookie.

-------------------------------------------------------------------
id (ai)  | userid   | login_string (128 chars) 
--------------------------------------------------------------------
1        |  1       | 86faabe4142269e2274df911a....
--------------------------------------------------------------------
2        |  2       | 013835e194d75c183dc914c9e....

Login_string必须是唯一的字符串,并且可以通过以下方式创建:

Login_string must be a unique string and can be created by this way :

$login_string = hash('sha512', $userid . $_SERVER['HTTP_USER_AGENT'] . time());

结果是唯一的128字符串长度.

The result is a unique 128 string length.

要将login_string存储在cookie上:

To store login_string on the cookie :

if(isset(post('remember_me')) {

     $cookie_name  = 'user_str_session';  
     $cookie_value = $login_string;
     setcookie($cookie_name, $cookie_value, time() + (86400 * 1), "/"); // one day example

}

下次用户关闭浏览器并重新访问Web(通常登录会话已过期)时,则在第一次加载页面时,系统将找到login_string并返回用户ID.通过使用该用户标识,系统将创建用于自动登录的登录会话.

Next time the user close the browser and revisit the web ( where normally login session has expired ), then at the first page load, system will find the login_string and return the userid. By using this userid, system will create login session for automatic login.

示例脚本:

if(!isset($_SESSION['id'])) {

        if(isset($_COOKIE['user_str_session'])) {
            $user_string = $_COOKIE['user_str_session'] ;

            $sql   = "SELECT `userid` FROM `users_log` WHERE `string` =? ";
            $query = $this->db->query($sql, $user_string);

            $userid = $query->row()->userid;

            if($userid) {                                                 
               $_SESSION['id'] = $userid;                     
            }   
           }
     }

要防止在公共计算机上使用login_string,请通过删除cookie上的login_string来应用用户注销.

To prevent the login_string on a public computer, apply user logout by deleting the login_string on cookie.

function logout()
   {            
      // remove cookie
      setcookie("user_str_session", "", time() - 3600);

      session_destroy(); 
      redirect('/');    
   }

这只是创建自动登录系统的简短描述.实际上,您可以根据自己的风格和需求进行游戏.

This is only a brief description to create an automatic login system. Practically you can play with your own style and needs.

这篇关于如何在不使用javascript或jquery中的表单的情况下在登录时创建记住我的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆