php 注意:未定义索引:登录 C:\xampp\htdocs\index.php 第 55 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11908115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:26:05  来源:igfitidea点击:

php Notice: Undefined index: login in C:\xampp\htdocs\index.php on line 55

php

提问by Cocoa Dev

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

可能重复:
PHP:“注意:未定义变量”和“注意:未定义索引”

I am getting warnings. I would like to take care of it. Here is the offending code

我收到警告。我想照顾它。这是违规代码

if ($_REQUEST['login']) $user = $_REQUEST['login'];
elseif ($_COOKIE['user']) $user = $_COOKIE['user'];

if ($_REQUEST['encpas']) $pass = $_REQUEST['encpas'];
elseif ($_COOKIE['pass']) $pass = $_COOKIE['pass'];

if ($_REQUEST['randsess']) $sess = $_REQUEST['randsess'];
elseif ($_COOKIE['sess']) $sess= $_COOKIE['sess'];

I am not sure how to get rid of them.

我不知道如何摆脱它们。

Thanks

谢谢

回答by Zoltan Toth

Check your variables to see if they set and have a value or not:

检查您的变量以查看它们是否设置并具有值:

if ( isset($_REQUEST['login']) ) { 
    $user = $_REQUEST['login'];
} elseif ( isset($_COOKIE['user']) ) {
    $user = $_COOKIE['user'];
}

回答by Abraham Brookes

Try using isset() to check if the index has been initialized before you check if it's true or not. http://au.php.net/manual/en/function.isset.php

尝试使用 isset() 检查索引是否已初始化,然后再检查它是否为真。 http://au.php.net/manual/en/function.isset.php

回答by JorgeeFG

You should use array_key_exists, or isset. The last is faster according to some benchmarks.

您应该使用 array_key_exists 或 isset。根据一些基准,最后一个更快。

if( isset( $_REQUEST['login'] ) ) {
  $user = $_REQUEST['login'];
}

isset doesn't count NULL as value.

isset 不将 NULL 计为值。

Be careful with this because the following:

小心这一点,因为以下几点:

index.php?login=&otherstuff

Will set login with empty value. But it will be set.

将登录设置为空值。但它会被设置。