I'm really having a hard time dealing with "fetch user-specific data". Thing is I planned to log into my Android Application and when I go to a different activity (ProfileActivity), I can see my own profile data (in this case, "Full Name" and "Job") directly from PHP and MySQL Database. That was what I initially wanted. I have my own SharedPreference and it works perfectly BUT somehow it only stores my Username and Full Name (because I have RegisterActivity) and not my "Job". I've tried reading several solutions like storing in SQLite or tokens but mostly I can't get to that point and I can't get it to work.
But then I thought of using the $_SESSION in PHP. What's in my way of thinking was:
First, I start my Android App and input my credential to login (Username and Password);
Then, the inputted variable (Username) is stored and processed in PHP $_SESSION (loginget.php
);
After that, I planned to use that $_SESSION to be used to another PHP files (e.g.c.php
) and display it in either Androidtextview
orrecyclerview
for future needs.;
That was I initially wanted it to work, but couldn't.
I have this PHP login file that accept username and passwordloginget.php
.
<?php
$response = array();
include 'koneksi.php';
include 'functions.php';
session_start();
//Get the input request parameters
$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE); //JSON decode convert JSON into array. (JSON is from android input, array is for php)
//Check for Mandatory parameters
if(isset($input['username']) && isset($input['password'])){
$username = $input['username'];
$password = $input['password'];
$query = "SELECT full_name, job, password_hash, salt FROM member WHERE username = ? ";
if($stmt = $con->prepare($query)){
$stmt->bind_param("s",$username);
$stmt->execute();
$stmt->bind_result($fullName,$job,$passwordHashDB,$salt);
if($stmt->fetch()){
//Validate the password
if(password_verify(concatPasswordWithSalt($password,$salt),$passwordHashDB)){
$response["status"] = 0;
$response["message"] = "Login successful";
$response["full_name"] = $fullName;
$response["job"] = $job;
}
else{
$response["status"] = 1;
$response["message"] = "Invalid username and password combination";
}
}
else{
$response["status"] = 1;
$response["message"] = "Invalid username and password combination";
}
$stmt->close();
}
}
else{
$response["status"] = 2;
$response["message"] = "Missing mandatory parameters";
}
$_SESSION['yes_msg'] = $response["full_name"];
echo json_encode($response);
?>
And thisc.php
used to fetch the Profile Data ("Full Name" and "Job") from MySQL Database:
<?php
session_start();
$SESSION = $_SESSION['yes_msg'];
include 'koneksi.php';
include 'functions.php';
//if everything is fine then create an array for storing the data
$resp = array();
$sql = "SELECT full_name,job FROM member WHERE username = '$SESSION'";
//creating an statment with the query
$stmt = $con->prepare($sql);
//executing that statment
$stmt->execute();
//binding results for that statment
$stmt->bind_result($full_name,$job);
//looping through all the records
while($stmt->fetch()){
//pushing fetched data in an array
$temp = [
//'user_id'=>$user_id,
//'username'=>$username,
'full_name'=>$full_name,
'job'=>$job
];
//pushing the array inside the hero array
array_push($resp, $temp);
}
//displaying the data in json format
echo json_encode($resp);
?>
It looked like I placed the $_SESSION function onloginget.php
at the wrong place and can't get it to work.
Inc.php
, the GET function worked fine when I tested with static variable and it shows on the Android Activity.
But when I tried to connect it withloginget.php
, the data is not showing or fetched, but there are no errors as well.
I really appreciated hint or answer, for this is probably beginner's mistake as I am a beginner as well and this is my first time writing and asking a question.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.