0

I am trying to get User data from a database, and display it on an account page in my app. In my php, I am storing the logged in user in a session. I found out that to access the user session I have to add the sessionId to my string request like:

    private void getUserData() {

    SharedPreferences sharedPreferences = getSharedPreferences(Config.sharedPref, Context.MODE_PRIVATE);
    String sessionId = sharedPreferences.getString(Config.SID, "SessionID");

    StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.SERVER_ADDRESS + "GetUserData.php?PHPSESSID=" + sessionId,

            new Response.Listener<String>() {

with sessionId being added to the request. However I am getting an unexpected response code 500 error and I believe it has to do with managing cookies. I am trying to do something like

     CookieManager manager = new CookieManager();
    CookieHandler.setDefault(manager);

but not sure how to do this or where to put the cookie manager in the method. Thank you.

1

1 Answer 1

0

There are 2 ways you can pass SessionID to your server :

  1. Send SessionID in URL like you mentioned.

    StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.SERVER_ADDRESS + "GetUserData.php?PHPSESSID=" + sessionId,  ...
    

    In this case you wont require any Cookie to be added in request . So dont need to set CookieManager. On php side make sure you have configuration to accept PHPSESSID in URL , look here for refrence.

  2. Put SessionID inside Cookie header field of your http request. In this case you need to set CookieManager to manage cookies which will do the work for you. Here you dont need to send sessionId as URL params.

You seem to be mixing up both these ways.

1
  • Thank you so much for the response. How do I accept the PHPSESSID on the PHP side? I looked at the link you provided and it said to make sure session.use_trans_sid is enabled in the php.ini file. However I am unable to edit this file on the server I am using.
    – cdub
    Commented Mar 10, 2016 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.