Server side PHP session is not working in android

877 views Asked by At

I am using login concept in android in that login one username login on one time only the same username can't login on second time. It is working fine. now i want to use session time out conept in server side but it is not working the value is storing into session but it is not able to getting on another php file.

my login php code

session_start();                
$_SESSION['user'] =1; 

second php code

session_start();
    if(isset($_SESSION['user']))
    {
    //my action
    }

My android code

  DefaultHttpClient httpClient = new DefaultHttpClient();
            httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
                    "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
                httpPost.setHeader("Accept", "text/html,application/xml," +
                    "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
                httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
1

There are 1 answers

1
fmt.Println.MKO On

is this your code you use for the first and second request ?

if so, the problem is that you always create a new client, which results in new session on serverside, because the sessionid wich is stored in a cookie is not available.

so create your httpClient once, and use the same instance for both requests. should solve the problem

like this:

 DefaultHttpClient httpClient = new DefaultHttpClient();
            httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);

            HttpPost httpPost1 = new HttpPost(url1);

        httpPost1.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
                "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
            httpPost1.setHeader("Accept", "text/html,application/xml," +
                "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
            httpPost1.setHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost1.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost1);
        HttpEntity httpEntity = httpResponse.getEntity();
        is1 = httpEntity.getContent();


        HttpPost httpPost2 = new HttpPost(url2);
        httpPost2.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
                "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
            httpPost2.setHeader("Accept", "text/html,application/xml," +
                "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
            httpPost2.setHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost2.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse2 = httpClient.execute(httpPost2);
        HttpEntity httpEntity2 = httpResponse2.getEntity();
        is2 = httpEntity2.getContent();