user3079308 user3079308 - 4 months ago 10
PHP Question

handle file using session but multiple session

i will handling scurity file with session, but i have problem.

i have filea.php with session.

session_start();
if ($_SESSION['login']){
include('fileb.php');
}


i have fileb.php with session.

session_start();
if ($_SESSION['login']){

} else {
// redirect to login
}


i will include fileb.php in filea.php using include file

my filea.php

session_start();
if ($_SESSION['login']){
include('fileb.php');
}


anyone can help me?

Ren Ren
Answer

Instead of just using session_start() on fileA and fileB, you could use:

if (session_status() === PHP_SESSION_NONE){session_start();}

This will ensure that session will be started only if it is not already set, else will be skipped.

Similarly to see if SESSION variables are set or not, consider using:

if(isset($_SESSION['login'])) {
    // Your codes here
}

Where, I assume that you have managed the session variables somewhere else and is working properly.