Android Login with Token Session: like user logins and stays in session until logout

随声附和 提交于 2020-01-22 10:04:47

问题


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/



来源:https://stackoverflow.com/questions/14005460/android-login-with-token-session-like-user-logins-and-stays-in-session-until-lo

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