为什么我的PHP会话变量不起作用?

时间:2017-06-25 14:05:21

标签: php session

突然间,我的PHP会话变量停止了存储。以前,它工作正常,但没有任何明显的变化,变量不再存储。

我尝试使用adminlogin.php上的代码来显示所有现有的会话变量,实际上,当使用登录表单时,它们并未存储。重新加载页面后,它们将不再存储。

我还创建了一个新文件random.php,其中我将一个随机数存储到$ _SESSION ['Number']。完成后,我回到adminlogin.php,填写该页面上的表单,然后重新加载页面。同样,表单应存储的会话变量不在会话数组中,但显示了随机数$ _SESSION ['Number']。

connect.php(header)

<?php 

session_start();

$user = 'root';
$password = 'root';
$db = 'sharkcard';
$host = 'localhost';
$port = 3306;

$link = mysqli_init();
$connection = new mysqli(

   $host, 
   $user, 
   $password, 
   $db
);

if(!(empty($_SESSION['LoggedIn'])) && !empty($_SESSION['StudentID']))
{
    $loggedin = 1;
}
else{
    $loggedin = 0;
}


?>

adminlogin.php

<?php include 'connect.php';
    if($_SESSION['LoggedIn'] == 1)
{
        echo "Welcome back! You are: " . $_SESSION['StudentID'] . " " . $_SESSION['Row']['FirstName'] . " " . $_SESSION['Row']['LastName'] . "<br>";
    }

else{

if(!($_SERVER["REQUEST_METHOD"] == "POST")){
?>

<form action="adminlogin.php" method="POST">
    Password: <input type="password" name="password" autofocus="autofocus"><br>
    sharkcard ID: <input type="text" name="id"><br>
    <input type="submit">
</form>

<?php
}

else if(empty($_POST['id']) || empty($_POST['password'])){
    echo "You left something blank! Reload the page and try again.";
}

else {

    $id = ($_POST['id']);
    $password = md5($_POST['password']);
    $checklogin = $connection->query("SELECT * FROM students WHERE StudentID = '".$id."' AND AdminPassword = '".$password."' AND AdminPrivileges = 1");
    $num = mysqli_num_rows($checklogin);
    if(mysqli_num_rows($checklogin) == 1)
    {
        $row = mysqli_fetch_array($checklogin);

        $_SESSION['StudentID'] = $id;
        $_SESSION['Row'] = $row; // added
        $_SESSION['LoggedIn'] = 1;


        echo "<h1><font color=\"green\">Success</font></h1>";

        echo "<b>Account Information</b><br>" . $_SESSION['StudentID'] . " " . $_SESSION['Row']['FirstName'] . " ". $_SESSION['Row']['LastName'];

    }
    else
    {
        echo "<h1><font color=\"red\">Failure</font></h1>";
        echo "<p>Sorry, your account could not be found. Please reload and try again.</a>.</p>";
    }
}
}
?>

1 个答案:

答案 0 :(得分:0)

我认为问题是adminlogin.php的第2行  Sub marine() Dim r As Range, s As String Set r = Range("N6:N125") s = "=IF(N6:N125="""",""MyText"",N6:N125)" r.Value = Evaluate(s) End Sub 应为if($_SESSION['LoggedIn'] == 1)

此外,if($loggedin)$_SESSION['LoggedIn']不是数组,因此您应该在第19行的connect.php中使用$_SESSION['StudentID']而不是isset()