Currently, I've got a problem with Session[].
When I'm log in (which is working fine). I gave Session[] "thing" but when I relocate to another window and then when I want to use it to get data from the MYSQL database.
Is there any way to work with the SESSION[] after login?
The relocated page .js:
betoltProfil();
function betoltProfil() {
//get data from database
var formData = new FormData();
formData.append("funkcio", "AdoIntBe");
fetch(baseUrl + "/ajax/AdomanyInterfacePHP.php", {
method: "POST",
body: formData,
})
.then(response => response.text())
.then(request => {
let szervadat = JSON.parse(request); //json data to let
console.log(szervadat); //usually gives the <Empty string> log int the browser
oldalEpit(szervadat);
})
}
function oldalEpit(adatTomb) {
console.log(asd);
}
The function php from formData:
<?php
require_once "../app/functions.php";
session_start();
if (isset($_POST["funkcio"])) {
if ($_POST["funkcio"] == "AdoIntBe") {
if (isset($_SESSION["userId"])) {
AdoIntBe($conn, $_SESSION["userId"]);
}
// AdoIntBe($conn);
}
}
The main function of PHP what should get the data from the MYSQL based in the SESSION[]
function AdoIntBe($conn, $userId)
{
session_start();
//the select to Database
$stmt = $conn->prepare("SELECT nev, leiras, email FROM adomanyszerv WHERE id =?");
$stmt->execute([
$userId
]);
$eredmeny = $stmt->fetch(PDO::FETCH_ASSOC);
//This should give back to formData
echo json_encode($eredmeny);
}
This is the LOGIN PHP function where I declare the SESSION[].
function beEllenoriz($AEmail, $AJelszo, $conn)
{
$stmt = $conn->prepare("SELECT * from adomanyszerv where email =? and jelszo=?");
$stmt->execute([
$AEmail,
hash("sha512", $AJelszo)
]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($stmt->rowCount() == 1) {
session_start();
$_SESSION["userID"] = $row["id"];
// $_SESSION["felhasz"] = true;
echo json_encode(true);
} else {
echo json_encode(false);
false;
}
}
Overall, how to use the SESSION[] to identify the user who logs in and get the data from the database?
Sorry for the heavy and hard-to-understand code!
Copyright License:
Author:「Cincennes」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/71301718/is-is-there-any-solution-to-log-in-session-php-with-mysql-and-json