简体   繁体   English

html5登录localstorage几天

[英]html5 login localstorage for days

Html5 login localstorage for days. Html5登录localstorage几天。 This HTML5 Login script uses localstorage to stores users that and everything works fine. 这个HTML5登录脚本使用localstorage来存储用户,一切正常。 But I need to add one more thing. 但我还需要补充一点。 I want once the user log in for the first time, the app will store the users info in html5 browser for like 30 days, when 30 days elapse, the user will be redirected back to login again. 我希望一旦用户第一次登录,该应用程序将用户信息存储在html5浏览器中30天,当30天过后,用户将被重定向回到再次登录。 Can someone help me with that? 有人可以帮助我吗? Thanks. 谢谢。

<script>

$(document).ready(function(){

    $('.send').click(function(){

        var uname=$('#uname').val();
        var pass=$('#pass').val();

        if(uname==""){

            $('.error').effect("bounce",500).fadeIn(400).html('Enter Your Username');

        }

        else if(pass==""){

            $('.error').effect("bounce",500).fadeIn(400).html('Enter your password');

        }

        else{


            $('.error').hide();
            var userdata= "uname="+uname+"&pass="+pass;


            window.localStorage["uname"] = uname;
            window.localStorage["pass"] = pass;

            $("#loader").show();
            $("#loader").fadeIn(400).html('<img src="loader.gif" align="absmiddle">&nbsp;<span class="loading">Checking your details</span>');

            $.ajax({

                type:"post",
                data:userdata,
                url:"http://localhost/login.php",
                cache:false,
                success:function(msg){

                    $("#loader").hide();
                    $('.error').fadeIn(200).html(msg);

                }

            });



        }
    });

});

</script>

As I mentioned in a comment (and Shomz mentioned in his answer), you need to set the current date in the local storage just as you store the username and password (are you sure you need to store the password in local storage -- doesn't seem necessary) and then check if that value is set and compare it to the current date. 正如我在评论中提到的(Shomz在他的回答中提到),你需要在存储用户名和密码的同时在本地存储中设置当前日期(你确定需要将密码存储在本地存储中吗?似乎没必要)然后检查是否设置了该值并将其与当前日期进行比较。

Update: fixing the code (forgot to convert the local storage date from a string back to a date object, plus I had a missing close bracket, oops!). 更新:修复代码(忘记将本地存储日期从字符串转换回日期对象,加上我有一个缺少关闭括号,哎呀!)。

jsfiddle with an example usage: http://jsfiddle.net/uqpywLv3/17/ jsfiddle的示例用法: http//jsfiddle.net/uqpywLv3/17/

<script>

$(document).ready(function(){

    $('.send').click(function(){

        var uname=$('#uname').val();
        var pass=$('#pass').val();

        if(uname==""){
            $('.error').effect("bounce",500).fadeIn(400).html('Enter Your Username');
        }

        else if(pass==""){
            $('.error').effect("bounce",500).fadeIn(400).html('Enter your password');
        }

        else{
            $('.error').hide();
            var userdata= "uname="+uname+"&pass="+pass;

            window.localStorage["uname"] = uname;
            window.localStorage["pass"] = pass;
            // is storing the password this necessary?

            $("#loader").show();
            $("#loader").fadeIn(400).html('<img src="loader.gif" align="absmiddle">&nbsp;<span class="loading">Checking your details</span>');

            $.ajax({
                type:"post",
                data:userdata,
                url:"http://localhost/login.php",
                cache:false,
                success:function(msg){
                    // store the current date
                    window.localStorage["ldate"] = new Date();

                    $("#loader").hide();
                    $('.error').fadeIn(200).html(msg);
                }
            });
        }
    }); // end .send click handler

    // check if the user is logged in
    if (typeof window.localStorage["ldate"] != "undefined") {

        // localstorage seems to store the date as a string
        // so convert it to a date object
        var memDate = new Date(window.localStorage["ldate"]);

        var expDate = new Date(); // current date.
        expDate.setDate(-30); // set to 30 days ago.

        if (memDate > expDate) {
            // user's last login still valid
            // do whatever you think is appropriate here
        } else {
            // user's last login was past 30 days ago
            // perhaps call function to log them out on server side
            // and show login form?
        }
    } else {
        // the user is not logged in.
        // put your code for showing the login form here
    }
});

</script>

只需在localStorage中设置数据时(通过其中的另一个变量),每次加载应用程序时,检查该值并将其与当前日期/时间进行比较。

1) localstorage data won't expire like cookie. 1)localstorage数据不会像cookie一样过期。 it will stay like it for years. 多年来它会像它一样。 2) if you want to set expiration date add expiration date as value to localstorage then retrieve this info every time and check you condition like number of days, or hours, or months. 2)如果要设置到期日期,请将到期日期作为值添加到localstorage,然后每次检索此信息,并检查您的条件,如天数,小时数或月数。 or 3) use cookie where you can set expiration date. 或3)使用cookie,您可以设置到期日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM