为什么会话变量为空以导航下一页? [英] why session variable is empty to navigate the next page?

查看:25
本文介绍了为什么会话变量为空以导航下一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在工作的一个网站我一直在处理一个问题,现在我知道它为什么会发生,但不知道如何解决它.请帮忙!!

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!!

第 1 页:在第一页中,登录页面设置$_SESSION['user_id'] 存储在数据库用户id 中获取的值.在同一页面中可以打印会话并且它可以正常工作($_SESSION['user_id'] 是打印)并且还可以导航下一页(用户主页).

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).

第 2 页:在第 2 页(用户主页)中,$_SESSION['user_id'] 变成了空值,为什么会发生这种情况?很可能看到这个问题,忘记设置会话开始,但我设置了会话开始两个页面...

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...

第 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>";
    }
}

?>

第 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?

推荐答案

在第一页的开始使用 session,像这样.希望这会奏效

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>";
        }
    }

?>

这篇关于为什么会话变量为空以导航下一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆