Javascript登录,cookie [英] Javascript login, cookies

查看:274
本文介绍了Javascript登录,cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我完成家庭作业?

can anyone help me with my homework please?

<div id="header">
    <form id="login" onsubmit="return member()">
        <p>
            <label for="txtuser">User:</label>
            <input type="text" class="text" name="txtuser" id="txtuser"/><br/>
            <label for="txtpass">Password:</label> 
            <input type="text" class="text" name="txtpass" id="txtpass"/><br/>
            <input type="submit" class="buttons" id="btnlogin" name="btnlogin" value="Login"/>
        </p>
    </form>
</div>

这是我的家庭作业,我有一个登录表单,其中包含用户名和密码textbox,验证表单,如果其有效,一旦用户成功登录,网站的每一页都将显示用户的登录名,以及允许他们随时注销的链接。我们应该怎么办?cookies还需要...
感谢反正

this is my homework, i have got one login form , which contains username and password textbox, i have to validate the form, if its valid, Once the user logs in successfully, every page of the site will display the user's login name, along with a link allowing them to logout at any time. what should i do?cookies also required... thanks anyway

推荐答案

访问具有其id的元素,然后获取值,并最终检查它们是否有效:

Inside member() function, you can access the elements with their ids, then get the values and finally check for them being valid:

function member() {
    // Get the elements
    var user_input = document.getElementById("txtuser");
    var pass_input = document.getElementById("txtpass");

    // Get their values
    var user_value = user_input.value;
    var pass_value = pass_input.value;

    // Validate values, this is up to you
    if ( /* this is your homework */ ) {
        // Here values are OK, save a cookie with the username
        saveTheCookie(user_value);
        return true; // Form is OK
    }
    else {
        // Form is wrong
        return false;
    }
}

您可以使用以下功能存储Cookie: / p>

You can store cookies with some function like this:

function saveTheCookie(value) {
    var today = new Date(); // Actual date
    var expire = new Date(); // Expiration of the cookie

    var cookie_name = "username_form"; // Name for the cookie to be recognized
    var number_of_days = 10; // Number of days for the cookie to be valid (10 in this case)

    expire.setTime( today.getTime() + 60 * 60 * 1000 * 24 * number_of_days ); // Current time + (60 sec * 60 min * 1000 milisecs * 24 hours * number_of_days)

    document.cookie = cookie_name + "=" + escape(value) + "; expires=" + expire.toGMTString();
}

要获取cookie的值:

To get the value of the cookie:

function getTheCookie() {
    var cookie_name = "username_form";
    var return_value = null;

    var pos_start = document.cookie.indexOf(" " + cookie_name + "=");
    if (pos_start == -1) document.cookie.indexOf(cookie_name + "=");

    if (pos_start != -1) { // Cookie already set, read it
        pos_start++; // Start reading 1 character after
        var pos_end = document.cookie.indexOf(";", pos_start); // Find ";" after the start position

        if (pos_end == -1) pos_end = document.cookie.length;
        return_value = unescape( document.cookie.substring(pos_start, pos_end) );
    }

    return return_value; // null if cookie doesn't exist, string otherwise
}

不测试这个,这是一个想法,你开始。您仍然必须检查表单,设置cookie并在加载页面时检索它(包括在 onload 事件中设置DOM元素的HTML > body 标记)。祝你好运!

Note that I DID NOT test this, it's an idea for you to start. You still have to check the form, set the cookie and retrieve it when loading pages (including setting the HTML of a DOM element in the onload event in the body tag). Good luck!

这篇关于Javascript登录,cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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