Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP sessions with my android application to login

I am trying to make a login script for my android application, the script will send my email and password to the PHP server, verify the login and then create a PHP session so that the user stays logged in. This is my code,

HttpPost httppost = new HttpPost("http://server.com/login.php");
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();

public String login() {

    String userID = "";

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("email", "[email protected]"));
        nameValuePairs.add(new BasicNameValuePair("password", "admin"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        userID = EntityUtils.toString(response.getEntity());
        //Log.v("Login response", "" + userID);



    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

    return userID;
} 

This script successfully sends data to my server and my PHP successfully logs the user on. I have placed "HttpClient httpclient = new DefaultHttpClient();" outside my main login method. This has helped store the session until I call upon another class, then it just resets the session again. So I am wondering how I can alter the code so that "httpclient" is somehow stored so I can keep the session and stay logged into my server. Thank you!

like image 476
rusty009 Avatar asked Jul 29 '12 23:07

rusty009


People also ask

Can we use PHP in Android app?

You might not hear or not that you can use PHP for your Android App Development. We can use PHP as the back-end for our Android Applications, and trust me they work flawlessly with each other. Any Android app that needs account log-in and registration can efficiently utilize PHP on its back-end.

What is the use of PHP sessions in web application?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

What is session management Android?

Android App Development for BeginnersSession help you when want to store user data outside your application, so that when the next time user use your application, you can easily get back his details and perform accordingly.


2 Answers

Android Http get Session Cookie

Get the cookie session ID and use that cookie in the next requests to the server.

like image 178
Fernando André Avatar answered Nov 02 '22 11:11

Fernando André


Another way is to make your php code echo a string containing the session_id in its response to login.Let the android app retrieve this id and store it. Any future requests can be made by using post method with sess_id=stored id

<?php
if(isset($_POST['sess_id']))
{
session_id($_POST['sess_id']); //starts session with given session id
session_start();
$_SESSION['count']++;
}
else {
session_start(); //starts a new session
$_SESSION['count']=0;
}
echo session_id();
?>
like image 34
Karishma Sharma Avatar answered Nov 02 '22 10:11

Karishma Sharma