i started session at the start of my index.php
as per below
<?php
if ( !isset($_SESSION) ) {
ini_set( 'session.save_path', realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/tmp'));
umask(0);
// session_status(); >> 1
session_start();
}
and use 2 classes which first one sets session and second one reads session
Login.php
<?php
class Login
{
public function getUser() {
if ( isset($_POST['username']) && $_POST['username'] === "test" ) {
$_SESSION['current_user'] = "test";
// session_status(); >> 2
?>
{
"status": true
}
<?php
} else {
?>
{
"status": false
}
<?php
}
by running this script will create a new session file as sess_to43...
in mentioned path and its contains current_user|i:test;
so far i have a session file
then in second class which calls by frontend immediately after Login class response:
Auth.php
<?php
class Auth
{
public function getSession() {
var_dump($_SESSION);
}
which returns NULL
and when i look at the session directory on server, can see new session file which is empty and means php runs session_start()
again, so
even thought i used session_start()
at start and before both of classes, the php can not recognize current existed session
additionally as you can see i set different path for session files and all new sessions store in this directory correct but this simple script can not handle the sessions
is there any reason in php.ini
or Apache server causes unset created session per new requests?
P.S. PHP version: 7.4
Your problem is right here:
You're sending output to the browser BEFORE starting your session. Try: