I have been working is a website I have been dealing with a problem from a while, and now I know why it is happening, but not how to solve it. Please help!!
Page 1:
In the first page, login page set the $_SESSION['user_id'] is stored the value that are fetch in database user id. In same page can print session and it work properly(the $_SESSION['user_id'] is print) and also navigate the next page(user home).
page 2:
In page 2(user home) the $_SESSION['user_id'] is turned into null value why this happen?
most probably see this problem in, forgot to set the session start but  I was set session start both page...
page 1
<?php
if (isset($_POST['sub'])) {
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $con  = mysqli_connect("localhost", "root", "");
    $db   = mysqli_select_db($con, "Database");
    $qry  = "select * from TABLE where username='$user' and password='$pass'";
    $res = mysqli_query($con, $qry) or die("could not connect to mysql");
    $row = mysqli_fetch_array($res);
    $len = mysqli_num_rows($res);
    if ($len <= 0) {
        echo "<script>";
        echo "alert('Oops.Username Or Password Incorrect!');window.location.href='login.php';";
        echo "</script>";
    } else {
        session_start();
        $_SESSION['id']      = $row['id'];
        $_SESSION['message'] = $user;
        $_SESSION['logout']  = "";
        $id                  = $_SESSION['id'];
        echo "<script>";
        echo "alert('log in Success $id ');window.location.href='login.php';"; //$id is print correctly 
        echo "</script>";
    }
}
?>
page 2
<?php
ob_start();
session_start();
if (isset($_SESSION['id'])) {
    $id = $_SESSION['id'];
    echo "$user"; // not printed
}
if (isset($_SESSION['message'])) {
    $msg = $_SESSION['message'];
    $_SESSION['message'] = "";
}
if (isset($_SESSION['logout'])) {
    $msg = $_SESSION['logout'];
    if ($msg == 'logout') {
        header("location:login.php");
        $_SESSION['message'] = "you must login first";
        exit(0);
    }
}
?>
    <?php
echo "welcome"; // only print this string the above session are not work  
?>
I also use this code before some project and it work correctly then why this time the session value not working?
 
    