skip to Main Content

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

2

Answers


  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();
    
    Login or Signup to reply.
  2. 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();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search