douqiaolong0528 2017-01-01 05:30
浏览 48
已采纳

升级到PHP 5.5.12后的未定义索引“admin”(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?

  • 写回答

1条回答 默认 最新

  • douzhuo8312 2017-01-01 05:35
    关注

    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 />';
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器