Apps $_SESSION not working with Android application

cdubs

Newbie
I am trying to store the logged in user in a new session, get the info from the database, put it to a json array, then get the array in android and display the results in a layout. When I run the app however, the array being returned from the php file is empty. If I am to test the php file however in a different program such as Postman, I get the correct response which is,
{"result":[{"id":"12","fname":"C","lname":"Wei","username":"cw","email":"werrs.‌com"}]}
I found out that in order to retrieve a previously initiated session on server side, my request has to contain the session id, but how am I supposed to get the session id without the session id? Here is my getSessionId class and getUserData methods I am using as well as the PHP files they are referencing. I am currently getting an empty result from my stringRequest.

getSessionId java
Code:
public class SessionId extends AppCompatActivity{

    String id;

    public String getSessionId(){
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.SERVER_ADDRESS + "GetSessionId.php",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        JSONObject jsonObject = null;
                        try{
                            jsonObject = new JSONObject(response);

                            id = response;
                        }catch (JSONException e){
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

        return id;
    }

}

getUserData java
Code:
SessionId sessionId = new SessionId();
private void getUserData() {
    StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.SERVER_ADDRESS + "GetUserData.php?PHPSESSID=" + sessionId.id,
            new Response.Listener<String>() {
                public void onResponse(String response) {
                    JSONObject jsonObject = null;
                    try {
                        //json string to jsonobject
                        jsonObject = new JSONObject(response);

                        //get json sstring created in php and store to JSON Array
                        result = jsonObject.getJSONArray(Config.json_array);

                        //get username from json array
                        getUserInfo(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                public void onErrorResponse(VolleyError error) {

                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

getUserData PHP
Code:
<?php
session_start();
    if($_SERVER['REQUEST_METHOD'] == 'GET'){

        $username = $_SESSION['username'];

        $sql = "SELECT * FROM USER WHERE username = '$username'";

        require_once('connect.inc.php');

        $run = mysqli_query($conn, $sql);
        $result = array();

        while($row = mysqli_fetch_array($run)){
            array_push($result, array(
                'id' => $row['id'],
                'fname' => $row['fname'],
                'lname' => $row['lname'],
                'username' => $row['username'],
                'email' => $row['email'],
            ));
        }

        echo json_encode(array('result'=>$result));

        mysqli_close($conn);
    }
?>

getSessionId PHP
Code:
<?php

    if($_SERVER['REQUEST_METHOD'] == 'GET'){
        session_start();
        $sessionid = session_id();
        echo json_encode(['sessionId' => $sessionid]);
    }

?>
 
Last edited:
Top