当条件之一不成立时,如何避免在 php 中发出通知

发布于 2024-09-02 20:30:25 字数 1500 浏览 10 评论 0原文

我注意到,当 php if 语句中的两个条件之一不成立时。您会收到不正确的语句的未定义索引通知。在我的例子中,结果是网页扭曲。 例如,以下代码:

<?php
session_start();

if (!isset($_SESSION['loginAdmin']) && ($_SESSION['loginAdmin'] != '')) {
    header ("Location: loginam.php");
} else {
    include('head2.php');
}



if (!isset($_SESSION['login']) && ($_SESSION['login'] != '')) {
    header ("Location: login.php");
} else {
    include('head3.php'); 
}
?>    

如果 if 语句之一不为 true。如果不正确,则会通知您它未定义。 就我而言,它表示会话“登录”未定义。如果使用会话“LoginAdmin”。为了避免这些未定义的索引通知,您可以建议我做什么。

编辑 对于那些提问的人,我在这里检查输入的信息是否正确。即使登录信息正确,它也总是重定向到登录页面:

$uname = mysql_real_escape_string($_POST['ausername']);
        $pword = mysql_real_escape_string($_POST['apassword']);
        $idnam= mysql_real_escape_string($_POST['aydi']);

        $SQL = "SELECT * FROM admin WHERE  ID= '$idnam' AND admin = '$uname' AND admin_password = '$pword'";
        $result = mysql_query($SQL);
        $num_rows = mysql_num_rows($result);



        if ($result) {
            if ($num_rows > 0) {
                session_start();
                $_SESSION['loginAdmin'] = "1";
                header ("Location: ampage.php");
            }
            else {
                session_start();
                $_SESSION['loginAdmin'] = "";
                header ("Location: loginam.php");
            }   
        }
        else {
            $errorMessage = "Error logging on, please try again.";
        }

I've notice that when one of the two conditions in a php if statement is not true. You get an undefined index notice for the statement that is not true. And the result in my case is a distorted web page.
For example, this code:

<?php
session_start();

if (!isset($_SESSION['loginAdmin']) && ($_SESSION['loginAdmin'] != '')) {
    header ("Location: loginam.php");
} else {
    include('head2.php');
}



if (!isset($_SESSION['login']) && ($_SESSION['login'] != '')) {
    header ("Location: login.php");
} else {
    include('head3.php'); 
}
?>    

If one of the if statements is not true. The one that is not true will give you a notice that it is undefined.
In my case it says that the session 'login' is not defined. If session 'LoginAdmin' is used. What can you recommend that I would do in order to avoid these undefined index notice.

EDIT
This is where I check if the entered information is correct, for those who are asking. It always redirects to the login page even if the login information is correct:

$uname = mysql_real_escape_string($_POST['ausername']);
        $pword = mysql_real_escape_string($_POST['apassword']);
        $idnam= mysql_real_escape_string($_POST['aydi']);

        $SQL = "SELECT * FROM admin WHERE  ID= '$idnam' AND admin = '$uname' AND admin_password = '$pword'";
        $result = mysql_query($SQL);
        $num_rows = mysql_num_rows($result);



        if ($result) {
            if ($num_rows > 0) {
                session_start();
                $_SESSION['loginAdmin'] = "1";
                header ("Location: ampage.php");
            }
            else {
                session_start();
                $_SESSION['loginAdmin'] = "";
                header ("Location: loginam.php");
            }   
        }
        else {
            $errorMessage = "Error logging on, please try again.";
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

烟花易冷人易散 2024-09-09 20:30:25

你的条件是错误的,应该是:

if (!isset($_SESSION['loginAdmin']) || ($_SESSION['loginAdmin'] == '')) {
    header ("Location: loginam.php");
} else {
    include('head2.php');
}

And:

if (!isset($_SESSION['login']) || ($_SESSION['login'] == '')) {
    header ("Location: login.php");
} else {
    include('head3.php'); 
}

|| 替换 && ,用 ==< 替换 != /代码>

您收到未定义的索引通知
该说法不属实。

情况并非如此,当某些变量未定义时,您会收到通知消息。您可以使用抑制运算符 @ 来避免这种情况,但使用它不是一个好主意,因为它会降低性能。

您还可以通过将其放在脚本顶部来隐藏通知消息:

error_reporting(E_ALL ^ E_NOTICE);

但是在开发过程中,您必须了解所有错误和通知,因此这不是一个好主意,除非您确定自己在做什么。

Your condition is wrong, it should be:

if (!isset($_SESSION['loginAdmin']) || ($_SESSION['loginAdmin'] == '')) {
    header ("Location: loginam.php");
} else {
    include('head2.php');
}

And:

if (!isset($_SESSION['login']) || ($_SESSION['login'] == '')) {
    header ("Location: login.php");
} else {
    include('head3.php'); 
}

replaced && with || and != with ==

You get an undefined index notice for
the statement that is not true.

That's not the case, you get notice messages when you have some variables undefined. You can avoid this be using suppression operator @ but it is not a good idea to use that because it slows down performance.

Also you can hide notice messages by putting this on top of your script:

error_reporting(E_ALL ^ E_NOTICE);

But again while on development, you must be aware of all errors and notices so this is not a good idea again unless you are sure what you are doing.

夏天碎花小短裙 2024-09-09 20:30:25

我想你的意思是这样的:

if (isset($_SESSION['loginAdmin']) && $_SESSION['loginAdmin'] != '') {
    //variable exists and is not empty
    include('head2.php');
} else {
    //variable does not exist or is empty
    header ("Location: loginam.php");
}

I think you meant this:

if (isset($_SESSION['loginAdmin']) && $_SESSION['loginAdmin'] != '') {
    //variable exists and is not empty
    include('head2.php');
} else {
    //variable does not exist or is empty
    header ("Location: loginam.php");
}
陪你搞怪i 2024-09-09 20:30:25

你的条件很奇怪。
例如: !isset($_SESSION['login']) && ($_SESSION['登录'] != '')
首先,检查会话变量中是否未设置“login”!isset($_SESSION['login'])
因此,如果未设置,您还将检查第二个条件,其中您尝试访问“登录”索引,未设置

也许你指的是这个条件?
if (!isset($_SESSION['login']) || ($_SESSION['login'] == ''))
如果登录名未设置,或者为空,则重定向到登录名
否则重定向到 head 3。

Your conditions are weird.
for example: !isset($_SESSION['login']) && ($_SESSION['login'] != '')
First, you check wether 'login' is NOT set in the session variable !isset($_SESSION['login'])
So if it is not set you will also check the second condition, in which you try to access the 'login' index, which isn't set.

Maybe you mean this condition?
if (!isset($_SESSION['login']) || ($_SESSION['login'] == ''))
If login is not set, OR it is empty, redirect to login
otherwise redirect to head 3.

纸短情长 2024-09-09 20:30:25

1)使用|| (OR) 代替 && (并且)。
2)将 != 改为 ==

1) Use || (OR)instead of && (AND).
2) change != to ==

过期以后 2024-09-09 20:30:25

您不想使用 isset($_SESSION['login']) (不带“!”)吗?

if (!isset($_SESSION['login']) && ($_SESSION['login'] != '')) {

如果您已经知道 $_SESSION['login'] 尚未设置,那么测试它是否没有多大意义。如果删除“!”,则会消除该通知,因为 isset 会使其静音,并且在未设置索引时永远不会评估第二个条件。

Don't you want isset($_SESSION['login']) instead (without "!")?

if (!isset($_SESSION['login']) && ($_SESSION['login'] != '')) {

It doesn't make much sense to test if $_SESSION['login'] if you already know it's not set. If you remove the "!", it eliminates the notice because isset silences it and the second condition will never be evaluated when the index is not set.

荭秂 2024-09-09 20:30:25

如果未设置会话 (!isset($_SESSION['loginAdmin'])),则 if 语句中的第二个条件实际上是不必要的,因为它们永远不会为真。未定义的变量(第一个条件)不能带有值(第二个条件)。

发生错误的原因是您在第二个条件(&& 之后)中测试的变量不存在。

试试这个:

<?php
session_start();

if (!isset($_SESSION['loginAdmin'])) {
    header ("Location: loginam.php");
    exit;
} else {
    include('head2.php');
}



if (!isset($_SESSION['login'])) {
    header ("Location: login.php");
    exit;
} else {
    include('head3.php'); 
}
?>

我在标头调用后包含了 exit ,否则代码将继续运行,可能会导致未定义的行为。

If the session is not set (!isset($_SESSION['loginAdmin'])) then the second condition in the if statements are really unnecessary, because they will never be true. You cannot have an undefined variable (the first condition) with a value (the second condition).

The error occurs because the variable you're testing in the second condition (after the &&) does not exist.

Try this:

<?php
session_start();

if (!isset($_SESSION['loginAdmin'])) {
    header ("Location: loginam.php");
    exit;
} else {
    include('head2.php');
}



if (!isset($_SESSION['login'])) {
    header ("Location: login.php");
    exit;
} else {
    include('head3.php'); 
}
?>

I included exit after your header calls because otherwise the code will continue running, probably causing undefined behavior.

赢得她心 2024-09-09 20:30:25

如果未设置会话变量loginAdmin,那么您不想评估检查它是否为空字符串的第二个条件。您可以使用短路评估来帮助您。将您的代码更改为

<?php
session_start();

if (!isset($_SESSION['loginAdmin']) || ($_SESSION['loginAdmin'] == '')) {
    header ("Location: loginam.php");
} else {
    include('head2.php');
}



if (!isset($_SESSION['login']) || ($_SESSION['login'] == '')) {
    header ("Location: login.php");
} else {
    include('head3.php'); 
}
?>

if the sesion variable loginAdmin is not set then you don't want to evaluate the second condition where you check if it is an empty string. You can use short-circuit evaluation to help you here. Change your code to

<?php
session_start();

if (!isset($_SESSION['loginAdmin']) || ($_SESSION['loginAdmin'] == '')) {
    header ("Location: loginam.php");
} else {
    include('head2.php');
}



if (!isset($_SESSION['login']) || ($_SESSION['login'] == '')) {
    header ("Location: login.php");
} else {
    include('head3.php'); 
}
?>
凉薄对峙 2024-09-09 20:30:25

运算符求值的一个简单示例:

function returnFalse() { echo "returning false\n"; return false; }
function returnTrue() { echo "returning true\n"; return true; }

echo "false && true\n";
if (returnFalse() && returnTrue()) {
    echo "True!\n";
} else {
    echo "False!\n";
}

echo "\ntrue && false\n";
if (returnTrue() && returnFalse()) {
    echo "True!\n";
} else {
    echo "False!\n";
}

echo "\ntrue && true\n";
if (returnTrue() && returnTrue()) {
    echo "True!\n";
} else {
    echo "False!\n";
}

此实验演示了 && 运算符仅在第一个操作数为 false 时才能短路。让我们检查一下代码哪里失败了。

if (!isset($_SESSION['login']) && ($_SESSION['login'] != '')) {
    header ("Location: login.php");
} else {
    include('head3.php'); 
}

简而言之,条件是:如果 $_SESSION['login'] 不存在,并且 $_SESSION['login'] 不是空字符串。我们永远不会想要访问 $_SESSION 数组中不存在的键,并且我们知道 && 运算符只会在以下情况下短路:左操作数为假。

左操作数$_SESSION['login']不存在。如果 $_SESSION['login'] 不存在,则为 true,这意味着 && 运算符必须查找右侧操作数以查看它是否也为 true。现在 && 查看条件 $_SESSION['login'] 不是空字符串。哎呀!此条件访问我们已经确定的 $_SESSION['login'] ,此时它不存在!

A quick example of operator evaluation:

function returnFalse() { echo "returning false\n"; return false; }
function returnTrue() { echo "returning true\n"; return true; }

echo "false && true\n";
if (returnFalse() && returnTrue()) {
    echo "True!\n";
} else {
    echo "False!\n";
}

echo "\ntrue && false\n";
if (returnTrue() && returnFalse()) {
    echo "True!\n";
} else {
    echo "False!\n";
}

echo "\ntrue && true\n";
if (returnTrue() && returnTrue()) {
    echo "True!\n";
} else {
    echo "False!\n";
}

This experiment demonstrates that the && operator can only short-circuit if the first operand is false. Let's examine where the code fails.

if (!isset($_SESSION['login']) && ($_SESSION['login'] != '')) {
    header ("Location: login.php");
} else {
    include('head3.php'); 
}

The condition, in words, is this: if $_SESSION['login'] does not exist, and $_SESSION['login'] is not an empty string. We can never, ever want to access a key that does not exist in the $_SESSION array, and we know the && operator will only short-circuit if its left operand is false.

The left operand is $_SESSION['login'] does not exist. This is true if $_SESSION['login'] does not exist, meaning the && operator must look to the right operand to see if it is also true. Now && looks at the condition $_SESSION['login'] is not an empty string. Whoops! This condition accesses $_SESSION['login'] which we have already, at this point, determined doesn't exist!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文