简体   繁体   中英

Undefined Index “admin” After Upgrading to PHP 5.5.12 (WAMP 2.5)

I got the following error after upgrading to PHP 5.5.12 (WAMP 2.5):

Notice: Undefined index: admin in C:\\wamp\\www\\MyWebsite\\check.php on line 83

The code in the " index.php ":

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

The code in the " 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();
}
?>

Usually I used " session_register() " to register the " admin " session variable, which worked well in PHP 5.1. But now in PHP 5.5 I got the " Undefined index: admin " error message.

How can I fix this issue?

Check that $_SESSION['admin'] is null

like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM