0

I am creating a php Rest API for my android application. I want to get all information of the logged user(logged in using his email and password). For this purpose I use $_SESSION['email'], but when I tried this using my app, didn't work. Can you please explain me how to use php session in android app. Thank you very much

function getLoggedUserInfo() {

    $sql = "SELECT  `id`,`name`, `gender` FROM users WHERE email=:email";

    try {

    $paramemail = $_SESSION['email'];

    $dbCon = getConnection();
        $stmt = $dbCon->prepare($sql);  
        $stmt->bindParam("email", $paramemail);
        $stmt->execute();
        $user = $stmt->fetchObject();  
        $dbCon = null;
        echo json_encode($user); 


    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
}

2 Answers 2

1

Can you please explain me how to use php session in android app.

You cannot.

Sessions stays on the PHP server, you cannot share them. That's in fact a Security breach.


Add the email to your statement:

 $sql = "SELECT  `id`,`name`, `gender`, `email` FROM users WHERE email=:email";
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your response. can you please tell me how to store the logged user email and retrieve it, in order to make the sql requet.
0

Do the following:

  1. Pass Json to your android app (when authenticated from the sever)

  2. On android application use that JSON and save some data that determine authenticated user . YOu can do this either by creating sqlite or using shared preference.

  3. Now whenever you like to perform authentic task, send the authentication status along with your data via POST to the server.
  4. On server check the POST data for authentication, if authenticate do your task.

I hope this helps. For better visualization see this

And one more hint:

instead of $_SESSION['email'] now you can use $_POST['email'](this is sent from android app), you might send some authentication parameter as POST then check for the authenticate then perform action as ,

if($_POST['authenticate'] == 1){
  $email = $_POST['email'];
  // Perform your function implementation
}

****  REMEMBER: All those POSTs are send from android app.

2 Comments

Thank you very much for your help. Is possible to explain me with piece of code how to use the shared preference? thanks again
Please, try searching Stackoverflow.There are 100s of questions regarding shared preference and answers with example. Here you can see the example tutorialspoint.com/android/android_shared_preferences.htm

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.