I have reduced the code in both files to try and figure out what is going on.
In my first file loginuser.php, I am running the following lines:
<?php
session_start();
$_SESSION['user_id'] = "test";
// more code follows; nothing else that involved user id or session
To double check that $_SESSION variable is indeed set, I ran the following modified approach:
<?php
session_start();
$_SESSION['user_id'] = "test";
$response['success'] = false;
$response['message'] = $_SESSION['user_id'];
echo json_encode($response);
exit;
When $response[‘success’] is false, the message is printed. So at this point I know that the $_SESSION variable was indeed set and I am getting a body response.
In my second file addrelease.php, I then run the following lines to check if I am seeing the session variable even before I do anything else:
<?php
session_start();
$response['message'] = $_SESSION['user_id'];
// more code would follow but again i will force message to pop and exit
$response['success'] = false;
echo json_encode($response);
exit;
The app will toast "Error, no response" if there is no body provided in the response (I am using retrofit) and this is exactly what happens. Given this, I know that addrelease.php does not even echo the response.
To confirm my suspicion that something is going on with the $_SESSION variable, I replaced $_SESSION[‘user_id’] in addrelease.php with "test2" and now I am getting a body with the message that I forced.
Most posts that I am finding seem to resolve their issue by ensuring the session_start() is the very first thing that is called and doubling checking the session_save_path. I have looked at both of these and can’t find any issues.
Hoping someone can help me, thanks in advance!
P.S. this is all new to me so please excuse if I am making a very basic mistake somewhere.
How exactly are you calling your addrelease.php script? Sounds like
whatever is making that background(?) request, is probably not sending
the session id.
Here is how i understand it to happen
NewReleaseActivity.java
Call<ReleaseModel> releaseModelCall = apiInterface.addRelease(
owner.trim(),
binding.textInputEditTrackName.getText().toString().trim(),
binding.textInputEditArtist.getText().toString().trim(),
label.trim(),
binding.textInputEditISRC.getText().toString().trim(),
binding.textInputEditUPC.getText().toString().trim(),
releaseDate,
TextUtils.join(",", buttonStates),
percentage
);
ApiInterface.java
@FormUrlEncoded
@POST("addrelease.php")
Call<ReleaseModel> addRelease(@Field("owner") String owner,
@Field("track_name") String trackName,
@Field("artist") String artist,
@Field("label") String label,
@Field("isrc") String isrc,
@Field("upc") String upc,
@Field("release_date") LocalDate date,
@Field("button_states") String buttonStates,
@Field("release_progress") float releaseProgress);
Additional Context
My goal is to get the session variable to bind with owner in my prepared statement:
$sql = "INSERT INTO releases (owner, track_name, artist,
label, isrc, upc,
release_date, button_states, progress) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt->bind_param("ssssssssd",
/*$_POST["owner"]*/ $_SESSION['user_id'],
$_POST["track_name"],
$_POST["artist"],
$_POST["label"],
$_POST["isrc"],
$_POST["upc"],
$_POST["release_date"],
$_POST["button_states"],
$_POST["release_progress"]
);
2
Answers
Just to give this post an answer, a comment by Oliver mentioned I should use CookieJar and that seemed to solve my issue of having the session variable work across .php files.
I then ran into an issue where my CookieJar implementation was non-persistent, however I have also found a solution to this and you may read about it here.
Thank you all for the help.
echo session_id();
After you start the session, the value of the above should be the same for both pages. If not, it is highly likely that there is some issue with your .htaccess file where some rule is impacting on the session.