why session variable is empty to navigate the next page?

穿精又带淫゛_ 提交于 2021-02-17 07:05:22

问题


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?


回答1:


use session in the start in first page, like this. Hopefully this will work

 <?php
session_start(); 
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['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>";
        }
    }

?>


来源:https://stackoverflow.com/questions/58899846/why-session-variable-is-empty-to-navigate-the-next-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!