用Javascript设置一个cookie [英] Setting a cookie with Javascript

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

问题描述

我正在尝试构建我的第一个需要连接到我的mysql数据库并使用json返回数据的移动应用程序。这很好,目前我有一个登录系统,一旦它确定了用户名和密码的存在,然后返回一个成功消息。



对于下一步我想在我的页面上使用ajax连接到我的数据库并返回一些数据。现在我想要返回的数据只需要该用户的数据。



所以我的想法和这是我需要一些建议的地方,是当成功消息返回时我将输入到登录表单中的用户名设置为cookie,然后我可以使用该cookie来构成我的查询的一部分吗?



是可能的还是存在的这是一个更好的方法吗?,我是新开发的JavaScript,通常只是使用PHP的工作,所以任何建议都会得到很好的回应。 b $ b

解决方案

您的问题有两个部分: $ b


(1)使用JavaScript来管理cookies:


首先,您可以创建一些辅助函数,这将使您更容易set / get cookies无论你想要什么:

 函数setCookie(name,value,exp_y,exp_m,exp_d){
var cookie_string = name +=+ escape(value);
var expires = new Date(exp_y,exp_m,exp_d);
cookie_string + =; expires =+ expires.toGMTString();
document.cookie = cookie_string;
}

函数getCookie(cookie_name){
var results = document.cookie.match('(^ |;)?'+ cookie_name +'=([^;] *)(; | $)');
返回结果? unescape(results [2]):null;
}

现在,您可以根据需要调用这些帮助函数。在您的用例中,当您的身份验证返回' success '标志时,您可以使用 userName 来设置cookie:

  setCookie('myCookie',userName,2050,1,1); 

请注意,在上例中,我们将2050作为任意高值传递,以便此cookie不会不会过期。否则,您可以将 expires 标志完全保留以创建仅会话cookie。



每当需要回忆 em> userName 传递给AJAX调用,您将获得cookie:
$ b $

  var userName = getCookie('的myCookie'); 




(2)...有没有更好的方法来做到这一点?


是的。一旦验证成功,您可以在服务器端的会话变量中保存 userName 。现在,只要您在查询中需要当前登录的 userName ,就可以使用该会话变种。一旦用户注销,您将清除会话。一旦会话超时,会话将自动清除。所以,你不用管家。



希望有帮助。


I am trying to build my first mobile app which needs to connect to my mysql database and return data using json. This is fine, at the moment I have a login system that once it establishes that the username and password exists it then returns a success message.

For the next step I want to use ajax on my pages to connect to my database and return some data. Now the data I want returned needs to be only data for that user.

So my idea and this is where I need some advice, is that when the success message is returned I set the username that was entered in to the login form as a cookie and then can I use that cookie to form part of my query?

Is thIS possible or is there a better way of doing this?, I am new to developing with Javascript and normally just work with php so any advice would be well received.

Thanks

解决方案

There are two parts to your question.

(1) Using JavaScript to manage cookies:

First, you may create a couple of helper functions which will make it easier for you to set/get cookies whereever you want to:

function setCookie(name, value, exp_y, exp_m, exp_d) {
    var cookie_string = name + "=" + escape(value);
    var expires = new Date(exp_y, exp_m, exp_d);
    cookie_string += "; expires=" + expires.toGMTString();
    document.cookie = cookie_string;
}

function getCookie(cookie_name) {
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
    return results ? unescape(results[2]) : null;
} 

Now, you may call these helper functions as needed. In your use-case, when your authentication returns a 'success' flag, you set the cookie with "userName" like this:

setCookie('myCookie', userName, 2050, 1, 1);

Notice, in the above example, we are passing 2050 as an arbitrary high value so that this cookie doesn't expire. Otherwise, you may leave the "expires" flag altogether to create a session-only cookie.

Whenever, you need to recall that "userName" to pass to an AJAX call, you get the cookie:

var userName = getCookie('myCookie');

(2) ... is there a better way of doing this?

Yes. You save the "userName" in a session var on server-side once authentication is successful. Now, whenever you need the current logged-on "userName" in your queries, you just use that session var. Once, the user logs out, you clear the session. Once, the session times-out the session is automatically cleared. So, you save yourself from housekeeping.

Hope that helps.

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

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