升级到PHP 5.5.12后的未定义索引“admin”(WAMP 2.5)

时间:2017-01-01 05:30:46

标签: php session localhost wamp

升级到PHP 5.5.12(WAMP 2.5)后出现以下错误:

  

注意:未定义的索引:第83行的C:\ wamp \ www \ MyWebsite \ check.php中的admin

index.php ”中的代码:

<?php
include_once("connect.php");
include_once "check.php";
echo "You are logged in."
?>

check.php ”中的代码:

<?php
session_start();
include_once("connect.php");

$error_msg = "";

if (isset($_POST['username']))
{
    include_once("connect.php");

    $username = $_POST['username'];
    $password = $_POST['password'];

    $username = preg_replace('#[^a-z 0-9?!]#i', '', $username);
    $username = strip_tags($username);
    $username = $mysqli->real_escape_string($username);
    $username = preg_replace("#`#i", "", $username);

    $password = preg_replace('#[^a-z 0-9?!]#i', '', $password);
    $password = strip_tags($password);
    $password = $mysqli->real_escape_string($password);
    $password = preg_replace("#`#i", "", $password);

    if($result = $mysqli->query("SELECT * FROM password"))
    {
        while($row = $result->fetch_array())
        { 
            $admin =  $row["username"];
            $adminpass =  $row["password"];

        }
    }

    if (($username != $admin) || ($password != $adminpass))
    {
        $error_msg = ': <font color="#FF0000">Your login information is incorrect</font>';
    }
    else
    {
        //session_register('admin'); // deprecated as of PHP 5.3.0
        session_start();
        $_SESSION['admin'] = $username;
        require_once "index.php";
        exit();
    }
}

if($result = $mysqli->query("SELECT * FROM password"))
{
    while($row = $result->fetch_array())
    { 
        $admin =  $row["username"];
        $adminpass =  $row["password"];

    }
}

if ($_SESSION['admin'] != $admin)
{
    echo '
    <h3>Only the administrator can view this directory</h3><br />

    <table width="340" border="0">
        <form action="check.php" method="post" target="_self">
              <tr>
                <td colspan="2">Please Log In Here' . $error_msg . '</td>
              </tr>
              <tr>
                <td width="96">Username:</td>
                <td width="234"><input type="text" name="username" id="username" style="width:98%" /></td>
              </tr>
              <tr>
                <td>Password:</td>
                <td><input type="password" name="password" id="password" style="width:98%" /></td>
              </tr>
              <tr>
                <td colspan="2" align="center"><input type="submit" name="button" id="button" value="Log In Now" /></td>
              </tr>
        </form> 
    </table>

    <br /><br /><br />
    ';

    exit();
}
?>

通常我使用“ session_register()”来注册“ admin ”会话变量,这在PHP 5.1中运行良好。 但现在在PHP 5.5中,我收到了“ Undefined index:admin ”错误消息。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

检查$_SESSION['admin']是否为空

像这样

if (!isset($_SESSION['admin']) || $_SESSION['admin'] != $admin)
{
    echo '
    <h3>Only the administrator can view this directory</h3><br />';
    ...
}