user962206
user962206

Reputation: 16127

PHP Sessions and redirecting

<?php require_once('./includes/connection.php'); ?>
<?php require_once('./includes/sql_func.php');?>
<?php 
    if(isset($_POST['submit']))
    {
        $isValid = verify_account($_POST['username'],$_POST['password']);
        if($isValid){
            session_name($_POST['username']);
            session_start();
            $_SESSION['isLoggedIn'] = true;
            session_write_close();
            redirect_to("user_panel.php");

        }else{
            echo "Invalid Username and password";
        }
    }
?>
<?php require_once('./includes/header.php');?>

Assuming that the user has entered a valid username and password it will create a session variable and will store different values in the session array. but how come when I redirect a user to another page the

<?php require_once('./includes/connection.php'); ?>
<?php require_once('./includes/sql_func.php');?>
<?php 
    session_start();
?>
<?php require_once('./includes/header.php');?>
    <?php 
        if(isset($_SESSION['isLoggedIn'])){
            echo "Hello User";
        }
    ?>

<?php require_once('./includes/footer.php');?>

This Line on the code above

 isset($_SESSION['isLoggedIn'])

is not being read as true?

Upvotes: 1

Views: 156

Answers (1)

John C
John C

Reputation: 8415

You are creating a session with session_name($_POST['username']); in your first page, then starting the session without a name in the second page. This essentially means that the second page is using a different session to the first.

Have a look at the PHP manual for session_name for more information, but the easiest fix is probably to drop that line altogether.

Upvotes: 1

Related Questions