Is is there any solution to log in $session[] php with MYSQL and JSON?

2022-03-01T06:26:44

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

About “Is is there any solution to log in $session[] php with MYSQL and JSON?” questions

Currently, I've got a problem with Session[]. When I'm log in (which is working fine). I gave Session[] &quot;thing&quot; but when I relocate to another window and then when I want to use it to get...
Currently, I've got a problem with Session[]. When I'm log in (which is working fine). I gave Session[] &quot;thing&quot; but when I relocate to another window and then when I want to use it to get...
am building a social network, i use php session to allow info to stay on the pages when the user goes to another page, however when the mysql script to update a value. it does reflect the change made
Here's my problem. I'm working on a food management system. Now I have it currently that when someone logs in, it puts their info into a loggedin table. Then, using that info, when the user logs...
So I have a log in php file ,where after it checks if the username and password correct, it redirects to main page. The point is I want to remember which user it is, so it will be his session only....
I have a file with this code: &lt;? include('../checkiflogged.php'); include('../../config.php'); if(!session_id()) session_start(); $klevel = $_SESSION['gradekreadings']; switc
Now I created a login form that is connected to a mysql database, and typed that code when the username and password is correct &lt;?php session_start(); if (mysql_num_rows($res)== 1) { $
I have here the code of my login and logout can you guys check if the session I placed is correct? Login code: &lt;?php session_start(); header("Cache-Control: no-store, no-cache, must-revalida...
$(document).ready(function() { $("#Calendar").eventCalendar({ eventsjson: 'event.default.json.php', jsonDateFormat: 'human' }); }); While loading the page,I am calling even
I found a problem with some results of a search on a DB. When, some fields have extra characters like "ü", the field return as null so it appear as null on the search. My code is like this: the php

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.