ImFabien75 ImFabien75 - 27 days ago 12
PHP Question

PHP sessions don't work in Android?

I have an issue with my Android app and my php files.

I have two php files:
* the first one is connecting after a long if/else blocks:

[...]
else{

$response["message"]="Connexion OK";

session_start();
$_SESSION['id'] = $id;
$_SESSION['pseudo'] = $pseudo;
$_SESSION['options'] = $options;
$response['session'] = $_SESSION['id'];
}
[...]
echo json_encode($response);


My second file only return if connected or not:

<?php

session_start();
$response =0;

if(isset($_SESSION['id'])){
$response= 1;
}



echo $response;


?>

When I execute URL of files in Firefox, in this order:
1/ the second one, I have 0
2/ The fist one
3/ The second one again, I get 1

The Php files are well construct!

Now, I want to do this in AsyncTasks Android: wherever the activity in which I am, the first thing I want to test the user connexion with the second file: but the return is always 0...not connected, even after i have been logged with the first one which is called in an Connexion Activity.

Is there something moreover in Android to open Php sessions?

Thanks for answers,




Here is my code:
I have an AsyncTask for connexion:

private class AddDataAsyncTask extends AsyncTask<Void, Void, Void> {
String message = "";
[...] //preExecute
@Override
protected Void doInBackground(Void... params) {


Log.i("add", " start doInBackground");

// Making a request to url and getting response

String urlt = url;
if (nameValuePair != null) {
String paramString = URLEncodedUtils.format(nameValuePair, "utf-8");

}
String response = "";
try {

DefaultHttpClient httpClient = new DefaultHttpClient();

CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

HttpGet post = new HttpGet(urlt);
HttpResponse rp = httpClient.execute(post);
if (rp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
// Server is unavailable
}
response = EntityUtils.toString(rp.getEntity()); //here is your response

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/* } catch (UnsupportedEncodingException e) {
e.printStackTrace();
*/
if (response != null) {
try {

JSONObject JsonResponse = new JSONObject(response);
message = JsonResponse.getString("message");

if (message.equals("Connexion OK"))
session = Integer.parseInt(JsonResponse.getString("session"));
Log.i("connnnnnexionC", response);
} catch (JSONException e) {

e.printStackTrace();
}
}

return null;


[...] //postExecute
}

And my AsyncTask where I want to check connexion:

private class VerifierConnexionAsyncTask extends AsyncTask<Void, Void, Void> {
[...] //preExecute

@Override
protected Void doInBackground(Void... params) {

String response = "";
try {

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;


CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

HttpPost httpPost = new HttpPost(url);
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
Log.i("connnnnexion",response);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

[...]//postExecute
}


First I enter the check the result is:

I/connnnnexion´╣Ľ 0

I go to the Connexion Activity and I logged using the first AsyncTask: I got:

I/connnnnnexionC´╣Ľ {"message":"Connexion OK","session":"27"}

So it well connect

I reenter the check, and again I have:

I/connnnnexion´╣Ľ 0




SOLUTION
I've written a new Class Connecte:

import org.apache.http.client.CookieStore;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
public class Connecte {
static CookieStore cookieStore;
static HttpContext localContext;

public static void makeCookie(){
cookieStore = new BasicCookieStore();
localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}


}


In connexion AsyncTask I add: Connexion.makeCookie():

HttpGet post = new HttpGet(urlt);
Connecte.makeCookie();
HttpResponse rp = httpClient.execute(post, Connecte.localContext);


And in the check asynctask I just add:

HttpPost httpPost = new HttpPost(url);
httpResponse = httpClient.execute(httpPost, Connecte.localContext);

Answer

You have to make sure that the connection you are using is reusing the same cookies. PHP manages sessions by given each client a PHP_SESSION cookie and keeps track of various values based on your PHP_SESSION. If you don't keep track of that between each request then php will think you're a different client each time.

CookieHandler.setDefault( new CookieManager( null, CookiePolicy.ACCEPT_ALL ) );


DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;

CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

HttpPost httpPost = new HttpPost(url);

httpResponse = httpClient.execute(httpPost, localContext);
httpEntity = httpResponse.getEntity();

Make sure to reuse the same localContext object in all future requests so they all share the same cookies and can persist PHP sessions.