Unexpected end of json input

I am making a request to a php file, and the php file should send back a json encoded array, but I'm getting unexpected end of json input for some reason. Here is the JS and the PHP:
//Get Account Information
function GetAccountInfos()
{
fetch("account_functions.php?action=1", {
method: 'post',
headers: {
"Content-Type": "application/json"
}
})
.then(response => response.json())
.then(data => {
if (data !== null)
{
console.log(data);

document.getElementById('account_info_username').value = data["username"];
document.getElementById('account_info_website').value = data["website"];
document.getElementById('account_info_email').value = data["email"];
document.getElementById('account_info_realname').value = data["realname"];
}
})
.catch(error => {
console.error(error);
})
}
//Get Account Information
function GetAccountInfos()
{
fetch("account_functions.php?action=1", {
method: 'post',
headers: {
"Content-Type": "application/json"
}
})
.then(response => response.json())
.then(data => {
if (data !== null)
{
console.log(data);

document.getElementById('account_info_username').value = data["username"];
document.getElementById('account_info_website').value = data["website"];
document.getElementById('account_info_email').value = data["email"];
document.getElementById('account_info_realname').value = data["realname"];
}
})
.catch(error => {
console.error(error);
})
}
23 Replies
QWGames
QWGamesOP13mo ago
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

//Check the user is logged in or not.
if (session_status() === PHP_SESSION_NONE)
{
ini_set('session.cookie_lifetime', 30 * 24 * 60 * 60);
session_start();
}

//Logout
if (isset($_GET["logout"]))
{
session_destroy();
echo json_encode(true);
exit();
}

//MANAGE ACCOUNT
if ($_SERVER["REQUEST_METHOD"] === "POST")
{
$action = isset($_POST["action"]) ? $_POST["action"] : null;
$servername = "...";
$connect_username = "...";
$connect_password = "...";
$dbname = "...";
$conn = new mysqli($servername, $connect_username, $connect_password, $dbname);
$session_username = $_SESSION["username"];
$session_email = $_SESSION["email"];

if ($conn->connect_error)
{
echo "Connection failed: ".$conn->connect_error;
exit();
}

//Update User Information
if ($action === 0)
{
//.....
}
//Send Informations To Client
else if ($action === 1)
{
$verified = $_SESSION["account_verified"] ? $_SESSION["account_verified"]: null;

if ($verified === true)
{
//Remove verified status after visiting this page
$_SESSION["account_verified"] = false;
}
else {
Header("Location: auth");
exit();
}

//Send User Information
$datas = [
"username" => $_SESSION["username"],
"email" => $_SESSION["email"],
"website" => $_SESSION["website"],
"realname" => base64_decode($_SESSION["realname"])
];

echo json_encode($datas);
exit();
}

}
?>
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

//Check the user is logged in or not.
if (session_status() === PHP_SESSION_NONE)
{
ini_set('session.cookie_lifetime', 30 * 24 * 60 * 60);
session_start();
}

//Logout
if (isset($_GET["logout"]))
{
session_destroy();
echo json_encode(true);
exit();
}

//MANAGE ACCOUNT
if ($_SERVER["REQUEST_METHOD"] === "POST")
{
$action = isset($_POST["action"]) ? $_POST["action"] : null;
$servername = "...";
$connect_username = "...";
$connect_password = "...";
$dbname = "...";
$conn = new mysqli($servername, $connect_username, $connect_password, $dbname);
$session_username = $_SESSION["username"];
$session_email = $_SESSION["email"];

if ($conn->connect_error)
{
echo "Connection failed: ".$conn->connect_error;
exit();
}

//Update User Information
if ($action === 0)
{
//.....
}
//Send Informations To Client
else if ($action === 1)
{
$verified = $_SESSION["account_verified"] ? $_SESSION["account_verified"]: null;

if ($verified === true)
{
//Remove verified status after visiting this page
$_SESSION["account_verified"] = false;
}
else {
Header("Location: auth");
exit();
}

//Send User Information
$datas = [
"username" => $_SESSION["username"],
"email" => $_SESSION["email"],
"website" => $_SESSION["website"],
"realname" => base64_decode($_SESSION["realname"])
];

echo json_encode($datas);
exit();
}

}
?>
Jochem
Jochem13mo ago
you'll have to check the network request in the network tab of the devtools, it's almost always a PHP error when you get unexpected end of json data in javascript you should be able to see the actual body it's sending back, and determine where the problem is also, as a general tip, using numbers as a value that means something other than the number it represents is a bad practice. $action can just have a descriptive value, like update_user and load_data or whatever. It'll make your code infinitely more readable, cause right now I'd have to go find where $action is set, figure out what the ternary expression is doing to $_POST['action'], then remember what I think the value is going to be and find where I was looking at the code when I wondered wtf $action was in the first place
QWGames
QWGamesOP13mo ago
At the request response I dont see anything
Jochem
Jochem13mo ago
then check your PHP error logs you're also making account_functions.php do way to much. It logs people out, gives you lists of data, updates user information... that should really be three separate files, it makes debugging much easier reading the code, I think maybe your request is failing at the $verified part, maybe try debugging around that, make sure it's actually passing verification? Also, why are you storing the real name base64 encoded?
base64_decode($_SESSION["realname"])
base64_decode($_SESSION["realname"])
QWGames
QWGamesOP13mo ago
It's returning the same error if I set the verified variable true Error logs are disabled for my php configuration. how can I enable it?
Jochem
Jochem13mo ago
hm, this should really already have taken care of showing the errors
error_reporting(E_ALL);
ini_set("display_errors", 1);
error_reporting(E_ALL);
ini_set("display_errors", 1);
what do you see when you open account_functions.php?action=1 in your browser?
QWGames
QWGamesOP13mo ago
nothing an empty 200 response I enabled error logs for php
Jochem
Jochem13mo ago
is there anything in the logs?
QWGames
QWGamesOP13mo ago
I think I have to wait a couple of minutes to process the changes, beacuse I think my php files are not working now. So its saying
[21-Dec-2023 12:49:56 UTC] PHP Warning: Undefined array key "username" in /home//domains/qwgamers.net/public_html/session_info.php on line 9
[21-Dec-2023 12:49:56 UTC] PHP Warning: Undefined array key "email" in /home/u/domains/qwgamers.net/public_html/session_info.php on line 10
[21-Dec-2023 12:50:03 UTC] PHP Warning: Undefined array key "website" in /home/u/domains/qwgamers.net/public_html/login.php on line 108
[21-Dec-2023 12:50:03 UTC] PHP Warning: Undefined array key "first_name" in /home/u/domains/qwgamers.net/public_html/login.php on line 109
[21-Dec-2023 12:50:03 UTC] PHP Warning: Undefined array key "last_name" in /home/u/domains/qwgamers.net/public_html/login.php on line 109
[21-Dec-2023 12:49:56 UTC] PHP Warning: Undefined array key "username" in /home//domains/qwgamers.net/public_html/session_info.php on line 9
[21-Dec-2023 12:49:56 UTC] PHP Warning: Undefined array key "email" in /home/u/domains/qwgamers.net/public_html/session_info.php on line 10
[21-Dec-2023 12:50:03 UTC] PHP Warning: Undefined array key "website" in /home/u/domains/qwgamers.net/public_html/login.php on line 108
[21-Dec-2023 12:50:03 UTC] PHP Warning: Undefined array key "first_name" in /home/u/domains/qwgamers.net/public_html/login.php on line 109
[21-Dec-2023 12:50:03 UTC] PHP Warning: Undefined array key "last_name" in /home/u/domains/qwgamers.net/public_html/login.php on line 109
I think the problem is with the session variables, and I don't know how, but another php file is having the same problem now. The other error is
[21-Dec-2023 12:58:43 UTC] PHP Warning: Undefined array key "query" in /home/u/domains/qwgamers.net/public_html/get_titles.php on line 5
[21-Dec-2023 12:58:43 UTC] PHP Warning: Undefined array key "query" in /home/u/domains/qwgamers.net/public_html/get_titles.php on line 5
line 5:
$query = $_GET["query"] ? $_GET["query"]: null;
$query = $_GET["query"] ? $_GET["query"]: null;
Jochem
Jochem13mo ago
that just means you're trying to access an array key that doesn't exist, it's not necessarily an error those errors also aren't referencing the PHP file you're having problems with, right?
QWGames
QWGamesOP13mo ago
I didnt see these are different files, but they are
Jochem
Jochem13mo ago
you'll probably have to start doing the janky PHP debugging thing where you add die("got to here"); on lots of lines and then rerun the code until it doesn't print that out
QWGames
QWGamesOP13mo ago
my file name is account_functions.php
Jochem
Jochem13mo ago
to be clear, put it in there once and move the statement down until you don't get it printing, then you know where things go wrong
QWGames
QWGamesOP13mo ago
yeah but I dont understand, beacuse these files worked before
Jochem
Jochem13mo ago
that usually means it's related to state, so in this case your session like, there's apparently a bug in your code that's triggered by not having a certain piece of state set, and because you had that piece of state set when you wrote it, it never occured. Now it's not set, and you are running into the bug oh, I missed something btw, your code only runs in a POST (which you should only use to store data, generally not to request it), so just loading in the browser wouldn't work, sorry
QWGames
QWGamesOP13mo ago
So I cant get response from a post request?
Jochem
Jochem13mo ago
ah, and also: You're posting to ?action=1. That sets $_GET['action'] to 1, not $_POST['action'] you can, it's just bad practice to only fetch data from a POST request the HTTP verbs have meaning, GET is to fetch data, POST is to send data, PUT and DELETE are to update and remove respectively generally you only use GET and POST, where GET is for fetching and POST is for actions that cause changes (or in rare circumstances are expensive and really cannot be done by bots crawling your site) This is probably actually the problem. $_POST['action'] isn't set. Try using $_REQUEST['action'] instead (this is partially caused by trying to have one file do many things btw. Files are free, if you had a fetch_user_data.php, you wouldn't have to check an action parameter with unclear values and could just return data after your verification checks)
QWGames
QWGamesOP13mo ago
Yeah, my JS file is the same. A lot of request functions to the server. I would probably need to make multiple files for each request. By the way its not working with $_GET or $_REQUEST, too
Jochem
Jochem13mo ago
I mean, you put the name of the file in the fetch, right? then you'll have to do the die() trick, I don't know what's wrong then without being able to run the code myself (and that's too much setup, I don't have time for that)
QWGames
QWGamesOP13mo ago
yes I understand. I will try to fix the issues. Thanks for the tips.
Jochem
Jochem13mo ago
if you find what line the code deviates from what you expect it to do, I'd be happy to help more
QWGames
QWGamesOP13mo ago
Thank you I fixed the original issue, I switched to get method, the problem was the Header("Location: auth"), instead this I'm redirecting the user client side:
//Send Informations To Client
else if (isset($_GET["get_info"]))
{
$verified = isset($_SESSION["account_verified"]) ? $_SESSION["account_verified"]: null;

if ($verified === true)
{
//Remove verified status after visiting this page
$_SESSION["account_verified"] = false;
}
else {
die(json_encode("auth"));
}

//Send User Information
$datas = [
"username" => $_SESSION["username"],
"email" => $_SESSION["email"],
"website" => $_SESSION["website"],
"realname" => base64_decode($_SESSION["realname"])
];

echo json_encode($datas);
exit();
}
//Send Informations To Client
else if (isset($_GET["get_info"]))
{
$verified = isset($_SESSION["account_verified"]) ? $_SESSION["account_verified"]: null;

if ($verified === true)
{
//Remove verified status after visiting this page
$_SESSION["account_verified"] = false;
}
else {
die(json_encode("auth"));
}

//Send User Information
$datas = [
"username" => $_SESSION["username"],
"email" => $_SESSION["email"],
"website" => $_SESSION["website"],
"realname" => base64_decode($_SESSION["realname"])
];

echo json_encode($datas);
exit();
}
function GetAccountInfos()
{
fetch("account_functions.php?get_info=true")
.then(response => response.json())
.then(data => {
if (data !== null)
{
//Redirect to auth
if (data === "auth")
{
window.location.href = "auth";
return;
}

//Success
document.getElementById('account_info_username').value = data["username"];
document.getElementById('account_info_website').value = data["website"];
document.getElementById('account_info_email').value = data["email"];
document.getElementById('account_info_realname').value = data["realname"];
}
})
.catch(error => {
console.error(error);
})
}
function GetAccountInfos()
{
fetch("account_functions.php?get_info=true")
.then(response => response.json())
.then(data => {
if (data !== null)
{
//Redirect to auth
if (data === "auth")
{
window.location.href = "auth";
return;
}

//Success
document.getElementById('account_info_username').value = data["username"];
document.getElementById('account_info_website').value = data["website"];
document.getElementById('account_info_email').value = data["email"];
document.getElementById('account_info_realname').value = data["realname"];
}
})
.catch(error => {
console.error(error);
})
}
The auth file sets the session "account_verified" true if the user enters his password and sends back the user to the manage-account file where the user datas will be send for the user and the session "account_verified" will be set to false again. The remain errors about I didnt use isset(), and I think the site is working again now And the account managment done with verification
Want results from more Discord servers?
Add your server