$ _POST无法正常工作。 “注意:未定义索引:用户名...” [英] $_POST not working. "Notice: Undefined index: username..."

查看:162
本文介绍了$ _POST无法正常工作。 “注意:未定义索引:用户名...”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

PHP:注意:未定义变量和通知:未定义索引

所以,我目前正在学习PHP,正在读一本关于密码的md5函数的书,所以我决定尝试一下,看看它是怎么回事。我还决定使用POST方法而不是GET,因为我看到有人说它更安全,并且不会让变量出现在URL上。

So, I am currently learning PHP and was reading on a book about the md5 function for passwords, so I decided to give it a try and see how it goes. I also decided to use the POST method rather than the GET, since I saw people saying that it is safer and doesn't let the variables appearing on the URL.

For我的测试项目我做了一个非常简单的表格,如下:

For my testing project I made a very simple form, which follows:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form action="dologin" method="POST">
            <table border="0">
                <tbody>
                    <tr>
                        <td>Username:</td>
                        <td><input type="text" name="username"></td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><input type="password" name="password"></td>
                    </tr>
                </tbody>
            </table>
            <input type="submit" value="Login">
        </form>
    </body>
</html>

问题是,当我在BOTH字段中输入值并点击Login时,我得到以下内容另一个PHP文件的输出。

The problem is that when I enter the values on BOTH fields and click "Login" I get the following output on the other PHP file.

Notice: Undefined index: username in C:\xampp\htdocs\md5\home\dologin\index.php on line 11
Username non existent

这里遵循/dologin/index.php文件的代码

Here follows the code for the "/dologin/index.php" file

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
            mysql_connect("localhost", "root") or die(mysql_error());
            mysql_select_db("test") or die(mysql_error());
            $query = "SELECT password FROM users WHERE username='".$_POST['username']."'";
            $result = mysql_query($query);
            $row = mysql_num_rows($result);
            if(isset($row)){
                $password = (mysql_result($result,$row - 1));
                $enteredpassword = md5("w@#$@#".$_GET['password']."^#3f%#^");
                if($password == $enteredpassword){
                    if(!isset($_COOKIE['PHPSESSID'])){
                        session_start();
                    }
                    $_SERVER['LOGIN_STATUS'] = true;
                } else {
                    die("Access denied");
                }    
            } else {
                die("Username non existent");
            }

        ?>
    </body>
</html>

非常感谢我对此问题的任何帮助,感谢您阅读。

Any help at all on my issue is very much appreciated, thank you for reading.

推荐答案

未定义索引意味着在$ _POST数组中的某个地方,没有索引(键)键用户名

undefined index means that somewhere in the $_POST array, there isn't an index (key) for the key username.

您应该将发布的值设置为变量以获得更清晰的解决方案,这是一个很好的习惯。

You should be setting your posted values into variables for a more clean solution, and it's a good habit to get into.

如果我遇到类似的错误,我会这样做:

If I was having a similar error, I'd do something like this:

$username = $_POST['username']; // you should really do some more logic to see if it's set first
echo $username;

如果用户名没有出现,那就意味着我搞砸了某个地方。你也可以,

If username didn't turn up, that'd mean I was screwing up somewhere. You can also,

var_dump($_POST);

查看您发布的内容。就调试而言,var_dump非常有用。看看: var_dump

To see what you're posting. var_dump is really useful as far as debugging. Check it out: var_dump

这篇关于$ _POST无法正常工作。 “注意:未定义索引:用户名...”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆