2

Forgive me in advance but I've read all topics related to this and this newbie is still confused. Let me start my giving a brief run-down of what I'm trying to achieve... I've created a quiz. Since I'm adding a leaderboard I require a user to create a username and a password or email address so as to be unique. I've managed to create register and login functions using phpmysql and it all works beautifully in my app. However, I don't wish the user to have to login in each time the app is opened but rather it remember the user details. Do I use the account manager function in the android sdk and scrap the mysql? Or should I just modify what I already have? Do I even need to use phpmysql for a login? I realise I will have to use it for the leaderboard. Any help to point me in the right direction would be much appreciated.

Cheers

2 Answers2

3

Us a session system, similar to how you'd use cookies.

When the android device connects, in PHP:

  • create a simple "session" variable.
  • record the phone signaure against the session variable in a database
  • return session variable to android.

In Android:

  • store the session variable in a local data file on the phone.

For subsequent calls:

  • Android sends the session variable (from local file storage) along with the phone ID to PHP
  • PHP checks the sessions variable is valid, checks the phone signature matches. If they do, you have the same session. If they don't match, create a new session (and send back to phone).

When the user "logs on"

  • Store the user_id and fact they have logged on against the session variable in PHP.
  • Then, when the session variable is sent from the phone, you know the user_id without interaction.
  • If a user logs off, just record that in the db (unlink the user_id from the session)

As the file storage is persisent, even when the app closes, and the phone ID won't change, PHP will always know who the user is on connection, even after the app is closed.

Robbie
  • 17,605
  • 4
  • 35
  • 72
  • Thanks for your help. That sums up what I need to do... but how to do it? Sorry but I am totally new to this. I've studied tutorials to get as far as I've got so if you know of any to carry out your suggestions I'd be grateful. – user1556260 Jul 27 '12 at 01:27
  • I can't specifically answer that. The Android Apps I've written have been in Titanium/Appcellerator, not in Java, so I can't give you a snippet of code. You can find session examples for PHP all over the web - but as oppsed to "set cookie" and using $_COOKIE, return the value (echo, possibly as JSON) to the Android App and get the session ID from a parameter. – Robbie Jul 27 '12 at 01:55
  • Thanks anyway. I can look up the php. But how to record a phone signature? I'm not sure what that means. – user1556260 Jul 27 '12 at 02:43
0

Even though Robbies answer is great, It sounds like an overkill for your application, at least in short term. An easier solution would be to save a boolean with Shared Preferences each session:

http://developer.android.com/guide/topics/data/data-storage.html#pref

Shared Preferences stores variables betweens sessions and are simple to fetch. In your case you would save a boolean true if a login was sucessful. Next time the application is started, fetch the boolean and if true, login is'nt needed. If there are no such variable or if it is set to false(previous login failed/user logout), demand login!

Good luck

Lucas Arrefelt
  • 3,879
  • 5
  • 41
  • 71