I
I
isgenderli2021-01-04 00:06:46
PHP
isgenderli, 2021-01-04 00:06:46

Why does the session start anew every time and it is not saved?

I am learning frontend development. I am building a spa with react. There was a problem with the session. The code worked, but suddenly stopped working. For 2 weeks I've been racking my brains to find a mistake, but it turns out. Problem in the following: Session is not saved. And each time it starts over. I am convinced of this for two reasons. session_start() from any request returns 2 as well as different session_id() . I ask you to help me. My code:
router.php

<?php
session_start();
header("Access-Control-Allow-Origin : *");
header("Access-Control-Allow-Credentials",true);
require_once('db.php');
require_once('Requests.php');

$uri = explode("/",$_SERVER['REQUEST_URI']);
if($uri[1] == "registrationForm"){
exit(Requests::regUser($_POST['userFullName'],$_POST['userLocation'],$_POST['userEmail'],$_POST['userPassword']));
}
else if($uri[1] == "loginForm"){
    exit(Requests::authUser($_POST['userEmail'],$_POST['userPassword']));
}
?>

- accepts requests and redirects to functions from requests.php :
<?php
  class Requests{
     static function regUser($userFullName,$userLocation,$userEmail,$userPassword){
         global $mysqli;
        $userEmail = mb_strtolower(trim($userEmail));
        $userPassword = password_hash(trim($userPassword), PASSWORD_DEFAULT);
        $stmt = $mysqli->prepare("SELECT * FROM `users` WHERE `userEmail`=?");
        $stmt ->bind_param("s",$userEmail);
        $stmt->execute();
        $result = $stmt->get_result();
        $row=$result->fetch_assoc();
        $stmt->close();
        if($result->num_rows){
            return 'exist';
        }else{
        $stmt = $mysqli->prepare("INSERT INTO `users`(`userFullName`, `userLocation`, `userEmail`, `userPassword`) VALUES (?,?,?,?)");
        $stmt->bind_param("ssss",$userFullName,$userLocation,$userEmail,$userPassword);
        $stmt->execute();
        $stmt->close();
        $stmt = $mysqli->prepare("SELECT * FROM `users` WHERE `userEmail` = ?");
        $stmt->bind_param("s",$userEmail);
        $stmt->execute();
        $result = $stmt->get_result();
        $row = $result->fetch_assoc();
        $stmt->close();
        $_SESSION["id"] = $row["id"];
        session_write_close();
        return $_SESSION["id"];
        }
      }
      static function authUser($userEmail,$userPassword){
          global $mysqli;
        $userEmail = mb_strtolower(trim($userEmail));
        $userPassword = trim($userPassword);
        $stmt = $mysqli->prepare("SELECT * FROM `users` WHERE `userEmail` = ?");
        $stmt->bind_param("s",$userEmail);
        $stmt->execute();
        $result = $stmt->get_result();
        $row = $result->fetch_assoc();
        $stmt->close();
        if(isset($row["userEmail"])){
        if(password_verify($userPassword,$row["userPassword"])){
         return $_SESSION["id"];
        }else{
            return "false";
        }
        }else{
            return 'false double';
        }
      }
      
  }
?>

I make requests using fetch:
function registr(event){
    event.preventDefault();
    const data = new FormData();
    data.append("userFullName","Sinan Isgenderli");
    data.append("userLocation","sinanLand");
    data.append("userEmail","[email protected]");
    data.append("userPassword","4");
    fetch("http://myWebSite.com/registrationForm",{
      method: 'POST',
      mode: 'cors',
      cache: 'default',
      credentials:"same-origin",
      body: data
    })
      .then(res=>res.text())
      .then(
        (res) =>console.log(res),
        (error) =>console.log(error)
      )
  }
  function login(event){
    event.preventDefault();
    const data = new FormData();
    data.append("userEmail","[email protected]");
    data.append("userPassword","4");

    fetch("http://myWebSite.com/loginForm",{
      method: 'POST',
      credentials:"same-origin",
      body: data
    })
      .then(res=>res.text())
      .then(
        (res) => console.log(res),
        (error) =>console.log("00")
      )
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
runapa, 2021-01-04
@runapa

Most likely you are starting a session every time you refresh the page. Try checking:

if(!isset($_SESSION['session_id']){
    session_start();
}

F
FanatPHP, 2021-01-05
@FanatPHP

The session is a cookie.
You open the developer console, the network tab, and see what ku-ku php puts on you, what the browser sends.
That way you can at least figure out who's screwing up.
Although, of course, the most likely reason is that Bom does not allow the session to start, and you do not see this because errors in php are crushed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question