How to save cookies [duplicate]

纵饮孤独 提交于 2020-05-14 12:21:37

问题


How would I go about having people that say they directly login to my website, How would I make it to where, Once they login they are actually loged in via cookies? To say something like this. "If you would like to actually completely access the website you signup and login, But once you login it saves your cookies, Intill loging out." At the moment, I have the signup and the login but it's not saving the cookies. :\


回答1:


<script type="text/javascript">

        function setCookie(key, value) {
            var expires = new Date();
            expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
            document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
        }

        function getCookie(key) {
            var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
            return keyValue ? keyValue[2] : null;
        }
</script>

You can set the cookies as like

setCookie(test_cookie,5);

You can get the cookies as like

getCookie(test_cookie);

Hope it may helps to someone else. . .




回答2:


You don't need jQuery to read/write cookies. You can use plain javascript. Some good examples are here: http://www.quirksmode.org/js/cookies.html

You can use this jQuery plugin to make getting and setting cookies a little easier:

http://plugins.jquery.com/project/Cookie



来源:https://stackoverflow.com/questions/20225161/how-to-save-cookies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!