$ _Session变量无法在创建它们的同一页面上访问

时间:2016-06-17 19:43:06

标签: php mysql sql session mysqli

我的问题是由于某些原因在我的网站的login.php页面上,我从相应数据库的users表中初始化了一些$ _Session变量,但是这些变量似乎在其他页面上正常运行我不能在初始化之后立即对它们做任何事情,因为它们似乎不存在。这是一个问题,因为我想引用我已定义的变量来创建其他会话变量以提高效率,因为我想将它用于欢迎消息,如我的代码中显示的简单示例。以下是有问题的代码:

if(isset($_POST['user_email']) && !empty($_POST['user_email']) AND isset($_POST['user_pass']) && !empty($_POST['user_pass']))
{
    $email = mysqli_real_escape_string($link, $_POST['user_email']); // Set variable for the username
    $password = mysqli_real_escape_string($link, sha1($_POST['user_pass'])); // Set variable for the password and convert it to an sha1 hash.

    $result = mysqli_query($link, "SELECT user_email, user_pass, user_active FROM users WHERE user_email='".$email."' AND user_pass='".$password."' AND user_active='1'") or die(mysqli_error($link)); 
    if(!$result)
    {
        //something went wrong, display the error
        echo 'Something went wrong while signing in. Please try again later.';
             //echo mysql_error(); //debugging purposes, uncomment when needed
    }
    else
    {
        //the query was successfully executed, there are 2 possibilities
        //1. the query returned data, the user can be signed in
        //2. the query returned an empty result set, the credentials were wrong      
        if(mysqli_num_rows($result) == 0)
        {
            echo 'You have supplied a wrong user/password combination. Please try again.';
        }
        else
        {
            //set the $_SESSION['signed_in'] variable to TRUE
            $_SESSION['signed_in'] = true;

            //we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages
            while($row = mysqli_fetch_assoc($result))
            {
                $_SESSION['user_id']    = $row['user_id'];
                $_SESSION['user_uuid']  = $row['user_uuid'];
                $_SESSION['user_level'] = $row['user_level'];
                $_SESSION['user_rank'] = $row['user_level'];                  
            }

            echo 'Welcome, ' . $_SESSION['user_uuid'] . '. <br /><a href="index.php">Proceed to the forum overview</a>.';
        }
    }
}

我有session_start();在我的connect.php的顶部,它包含在signin.php文档的顶部。

此页面上此代码的结果如下:

Welcome, .
Proceed to the forum overview.

但是,如果我在网站的另一页上运行它,它会正确打印,即:

Welcome, username.
Proceed to the forum overview

感谢。

0 个答案:

没有答案