$ _POST的未定义索引 [英] Undefined index with $_POST

查看:68
本文介绍了$ _POST的未定义索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新学习一些用于编写简单登录脚本的PHP基础知识,但是遇到了以前从未收到的错误(我在一年多以前制作了相同的脚本,并且从未遇到过此错误.我简化了代码我尽可能地测试一下哪个区域出了问题,这就是问题所在:

I am trying to relearn some PHP basics for making a simple login script, however I get an error I have not received before(I made the same script a little over a year ago and never had this error. I simplified the code as much as I could to test to see which area was problematic and here is the issue:

<?php
$user = $_POST["username"];
if($user != null)
{
    echo $user;
    echo " is your username";
}
else
{
    echo "no username supplied";
}
?>

现在,当我向脚本发送变量时,此代码可以正常工作,但是如果未提供任何变量,它将发出错误消息.从理论上讲,这会很好,因为如果没有提供用户名/密码,则可能会出现错误.在将代码发送到脚本之前,我将进行检查以确保这一点,但是我担心空白字符串可能会泄漏并吐出一些未知错误.这是我得到的错误:

Now this code works fine when I send a variable to the script, but when no variable is supplied it spits out an error. In theory this will be fine because if no username/pass is supplied then an error is expected. I will be checking to make sure of this before the code is send to the script, however I fear that somehow a blank string may leak through and spit out some unknown error. Here is the error I get:

( ! ) Notice: Undefined index: username in C:\wamp\www\verify_login.php on line 2

Call Stack

    Time    Memory  Function    Location
1   0.0003  668576  {main}( )   ..\verify_login.php:0

未提供用户名

如您所见,代码寄存器没有提供任何变量,但是它给出了错误,我认为这意味着未找到某个变量是预期的或类似的情况.有人可以帮我澄清一下吗?

as you can see the code registers that no variable was supplied, but it gives out and error that I assume means that a variable was not found were one was expected or something like that. Can someone please clarify this for me?

推荐答案

在PHP中,从未设置的变量或数组元素不同于其值为null的变量或数组元素.尝试访问这样的 unset 值是运行时错误.

In PHP, a variable or array element which has never been set is different from one whose value is null; attempting to access such an unset value is a runtime error.

这就是您遇到的问题:数组$_POST在键"username"上没有任何元素,因此解释器会在进行无效测试之前中止您的程序.

That's what you're running into: the array $_POST does not have any element at the key "username", so the interpreter aborts your program before it ever gets to the nullity test.

幸运的是,您可以测试变量或数组元素的存在,而无需实际尝试访问它.这就是特殊运算符isset的作用:

Fortunately, you can test for the existence of a variable or array element without actually trying to access it; that's what the special operator isset does:

if (isset($_POST["username"]))
{
  $user = $_POST["username"];
  echo $user;
  echo " is your username";
} 
else 
{
  $user = null;
  echo "no username supplied";
}

当PHP尝试获取$_POST["username"]的值作为参数传递给函数isset()时,它看起来将以与代码完全相同的方式爆炸.但是,isset()根本不是一个函数,而是在评估阶段之前就已识别的特殊语法,因此PHP解释程序将检查该值是否存在,而无需实际尝试对其进行检索.

This looks like it will blow up in exactly the same way as your code, when PHP tries to get the value of $_POST["username"] to pass as an argument to the function isset(). However, isset() is not really a function at all, but special syntax recognized before the evaluation stage, so the PHP interpreter checks for the existence of the value without actually trying to retrieve it.

还值得一提的是,随着运行时错误的发展,缺少的数组元素被视为次要元素(分配为E_NOTICE级别).如果更改error_reporting级别以便忽略通知,则原始代码将按编写的方式实际工作,而尝试的数组访问将返回null.但这被认为是不好的做法,尤其是对于生产代码.

It's also worth mentioning that as runtime errors go, a missing array element is considered a minor one (assigned the E_NOTICE level). If you change the error_reporting level so that notices are ignored, your original code will actually work as written, with the attempted array access returning null. But that's considered bad practice, especially for production code.

侧面说明:PHP进行字符串插值,因此if块中的echo语句可以组合为一个:

Side note: PHP does string interpolation, so the echo statements in the if block can be combined into one:

echo "$user is your username";

这篇关于$ _POST的未定义索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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