2012-04-28 78 views
0

恭喜我。我已经正式从asp.net切换到PHP。

现在,我卡住了。 :-)

我有一些代码,我发现在一些代码片段网站上,并有关于下面的一块问题。

if (isset($_POST['doRegCheck'])) 
    : if (intval($_POST['doRegCheck']) == 0 || empty($_POST['doRegCheck'])) 
    : redirect_to("index.php?do=users"); 
endif; 

endif; 

谁能告诉我到底是怎么回事?此代码是否检查doRegCheck POST变量的值以确保它不是空的?

在此先感谢。

回答

1

只有当HTTP头包含一个名为doRegCheck的变量并且该变量设置为0(?doRegCheck=0)或者什么都不是(?doRegCheck=)时,代码才会重定向。这似乎对我来说倒退了。我认为你会重定向,如果变量存在,它不会设置为falsy值,如0,false,F

该语法(冒号)通常用于模板(当您切换进出PHP模式时),使其更难阅读。

1

首先,让我们正确格式化的:

if (isset($_POST['doRegCheck'])) : 
    if (intval($_POST['doRegCheck']) == 0 || empty($_POST['doRegCheck'])) : 
     redirect_to("index.php?do=users"); 
    endif; 
endif; 

然后,让我们拿出的冗余。 empty基本上包括一个检查issetempty也将"0"识别为“空”。 empty承认任何虚假值或不存在的值为“空”。此代码可能尝试使用intval检查该值是否为数字。所以总结一下,这应该是更容易理解:

if (empty($_POST['doRegCheck']) || !is_numeric($_POST['doRegCheck'])) { 
    redirect_to("index.php?do=users"); 
} 
0

是的,你有正确的想法,如果“doRegCheck”设置,但等于“0”或没有价值“”那么你会重定向到'的index.php?做=用户'。因此,据我所知,基本上确保如果您在未经确认的情况下点击注册页面,则无法完成注册过程。

0

目前的形式,使可读性更难,因为它是,它应该是:

if (isset($_POST['doRegCheck'])): 
    if (intval($_POST['doRegCheck']) == 0 || empty($_POST['doRegCheck'])): 
    redirect_to("index.php?do=users"); 
    endif; 
endif; 

翻译为:

if (isset($_POST['doRegCheck'])){ 
    if (intval($_POST['doRegCheck']) == 0 || empty($_POST['doRegCheck'])){ 
    redirect_to("index.php?do=users"); 
    } 
} 

含义:

//is $_POST['doRegCheck'] set 
if (isset($_POST['doRegCheck'])){ 
    //intval will return (int)0 even if its abc, 
    // perhaps it should be is_numeric() as it will 
    // pass if the value is 0 or the value is abc or empty 
    if (intval($_POST['doRegCheck']) == 0 || empty($_POST['doRegCheck'])){ 
    //some user function to redirect 
    redirect_to("index.php?do=users"); 
    } 
}