Android Login with Token Session: like user logins

2019-03-17 00:39发布

问题:

After login in android app, how do I create a token session in the php api?

Like this:

I would like to make sure that when user log in it will stay in session no matter what happens (crashed, shut down/power down/reboot, leaving the app) at same time the user info data will be sending with all the activities in the app to the webserver.

Do I simply use:

session_start();
$_SESSION['username'] = $user;
$_SESSION['auth'] = "true";

If so how do I pass this session into the android application?

Login Authentification in Android App:

HttpClient client = new DefaultHttpClient();
String url = "http://www.somewebsite.com/login.php?username="+username+"&password="+password;
HttpGet request = new HttpGet(url);
// Get the response
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response_str = client.execute(request, responseHandler);
php android authentication login session-cookies

回答1:

  • When your Android application first calls your Login API, retrieve the token fields from the validated response
  • Store these token fields locally, in the app, using SharedPreferences

http://developer.android.com/reference/android/content/SharedPreferences.html

  • (this storage persists outside of the app/os lifecycle, it will still be there after a relaunch of the app or device reboot)
  • Whenever you call your web service (php), append the token parameters

This looks like some relevant information here:

http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/