2

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

Sam Williams
  • 177
  • 2
  • 7
  • 15
  • I don't can give you a good answer because haven't used it by my self, but take a look for document.cookie Here someone with the same problem http://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie – demonking Dec 16 '13 at 05:28

2 Answers2

1

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.

Abhitalks
  • 27,721
  • 5
  • 58
  • 81
  • Thank you, it already does that i think i was just missing the point, thank you for the explanation much appreciated – Sam Williams Dec 16 '13 at 05:54
1

Try this simple way:

document.cookie ='cookie1=test; expires=Fri, 3 Aug 2013 20:47:11 UTC; path=/'
Mr.G
  • 3,413
  • 2
  • 16
  • 20