php登录注册表单中未定义的索引错误


Undefined index error in php login-registration form

我最近用PHP为我的网站开发了一个登录注册表单,但是出现了以下错误:

Notice: Undefined index: action in C:'xampp'htdocs'login'login.php on line 3
Notice: Undefined index: reg in C:'xampp'htdocs'login'login.php on line 24
Notice: Undefined index: action in C:'xampp'htdocs'login'login.php on line 29
Notice: Undefined index: msg in C:'xampp'htdocs'login'login.php on line 40

这段代码工作正常,从数据库发送和检索数据,但我真的不知道为什么他给我这些错误。下面是代码

<?php
require_once('load.php');
if ( $_GET['action'] == 'logout' ) {
    $loggedout = $j->logout();
}
$logged = $j->login('index.php');
?>
<html>
<head>
    <title>Login Form</title>
    <style type="text/css">
        body { background: #c7c7c7;}
    </style>
</head>
<body>
    <div style="width: 960px; background: #fff; border: 1px solid #e4e4e4; padding: 20px; margin: 10px auto;">
        <?php if ( $logged == 'invalid' ) : ?>
            <p style="background: #e49a9a; border: 1px solid #c05555; padding: 7px 10px;">
                The username password combination you entered is incorrect. Please try again.
            </p>
        <?php endif; ?>
        <?php if ( $_GET['reg'] == 'true' ) : ?>
            <p style="background: #fef1b5; border: 1px solid #eedc82; padding: 7px 10px;">
                Your registration was successful, please login below.
            </p>
        <?php endif; ?>
        <?php if ( $_GET['action'] == 'logout' ) : ?>
            <?php if ( $loggedout == true ) : ?>
                <p style="background: #fef1b5; border: 1px solid #eedc82; padding: 7px 10px;">
                    You have been successfully logged out.
                </p>
            <?php else: ?>
                <p style="background: #e49a9a; border: 1px solid #c05555; padding: 7px 10px;">
                    There was a problem logging you out.
                </p>
            <?php endif; ?>
        <?php endif; ?>
        <?php if ( $_GET['msg'] == 'login' ) : ?>
            <p style="background: #e49a9a; border: 1px solid #c05555; padding: 7px 10px;">
                    You must log in to view this content. Please log in below.
                </p>
        <?php endif; ?>
        <h3>Login</h3>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <table>
                <tr>
                    <td>Username:</td>
                    <td><input type="text" name="username" /></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="password" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Login" /></td>
                </tr>
            </table>
        </form>
        <p>Not a member? <a href="register.php">Register here</a></p>
      </div>
    </body>
 </html>

您正在发送您的表单作为POST,但您正在阅读GET的内容,您应该将表单的动作更改为GET,或从$_POST而不是$_GET检索您的数据。

你可以修改:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">

或将所有$_GET更改为$_POST