-3

i got this error on my pages

#1

PHP Warning:  session_name(): Cannot change session name when session is active in /dir/login.php on line xx
PHP Notice:  session_start(): A session had already been started - ignoring in /dir/login.php on line xx

#2

PHP Warning:  session_name(): Cannot change session name when session is active in /dir/other_pages.php on line xx

and the codes were like this on pages to check the SessionData

<?php
//set session name for each pages and start the session
session_name('AppName');
session_start();

...

// check if session is exist, if it exist then set the data, if its not then redirect to login pages
if(!isset($_SESSION['SessionData'])){
 //return to login page if session doesnt exist
 header("location:/login");
 exit();
}else{
 // get and set data from $_SESSION['SessionData'];
 $Data = $_SESSION['SessionData'];
}
?>

on login page (#1) it can set the session name and get the data, but when i go to other pages (#2) , there is no session data from $_SESSION['SessionData']. and when the session name supposed to be "AppName", it become "PHPSESSID". and it keeps redirecting to login pages even if the login is successfully

is there anything wrong with the codes or is it the php version? because iam using PHP 7.3 or is it the problem from the web server?



**#EDIT:** i tried to create a new file on ``/filename.php`` with this code
session_name('SessionExample');
session_start();

$_SESSION['example'] = "hello";
echo $_SESSION['example'];

and it can echoing "hello"

then i create another file /subdir/filename2.php with

session_name('SessionExample');
session_start();

print_r($_SESSION);
echo $_SESSION['example'];

but unfortunately it echoing nothing, and it only print null/empty array

while the session name was also PHPSESSID not SessionExample

Tunku Salim
  • 167
  • 1
  • 9

2 Answers2

1

Check if the wrong session is already started. If it is, destroy the session and start the one you want.

if (session_status() == PHP_SESSION_ACTIVE && session_name() != 'AppName') {
    session_destroy();
}
session_name('AppName');
session_start();
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • PHP Warning: Use of undefined constant SESSION_STATUS_ACTIVE - assumed 'SESSION_STATUS_ACTIVE' (this will throw an Error in a future version of PHP) in /dir/other_pages.php on line 2 PHP Warning: session_name(): Cannot change session name when session is active in /dir/other_pages.php on line 5 – Tunku Salim Sep 02 '20 at 22:04
1

I think you cant open two session, so the error "A session had already been started" is because you try to open another session, you only have one session for the entire pages, but, you can have diferents session variable.

    if(session_status() == PHP_SESSION_NONE)
    {
        session_name('AppName');
        session_start();
    }

try to do this. PHP_SESSION_NONE check if the session has been started yet

the other thing i see in this code you dont set the session

if(!isset($_SESSION['SessionData']))
{
 //return to login page if session doesnt exist
 header("location:/login");
 exit();
}

so you need to set someting to the variable, or the program will continue go to login all the time beacause the session variable doesnt exists

if(!isset($_SESSION['SessionData'])){
//return to login page if session doesnt exist
 $_SESSION['SessionData'] = 'Data';
 header("location:/login");
 exit();
}
  • i dont really know whats the problem, at login page it can set the session and show it, but when it move to other pages the session is gone, i tried to print the session ``print_r($_SESSION)`` and it only show null array, but it can print the session on the login page – Tunku Salim Sep 02 '20 at 22:15
  • try to do `print_r($_SESSION['SessionData'])`, but put `$_SESSION['SessionData'] = 'Data';` in the if and then tell me if dont work – Luis Monsalve Sep 02 '20 at 22:28
  • you can try to erase the cache of your browser that make some problem sometimes or you can try this. before you do a header location. if(headers_sent()) { echo ''; echo ''; die(); } – Luis Monsalve Sep 02 '20 at 22:32
  • ive cleared my browser cache and do some test, but i still have the same problem – Tunku Salim Sep 02 '20 at 22:35