[英]Return always return false
The problem is that this function always return me 0. Why? 问题在于此函数总是返回0。为什么?
public function valid_token ()
{
if (!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
{
$this->errors[] = "Formulario incorrecto";
}
return count($this->errors)? 0 : 1;
}
Ignore my previous answer. 忽略我以前的答案。 Stupid falsy values.
愚蠢的虚假价值观。 The reason you are always getting a
0
in return is simply because...you have a value inside of your array. 您总是得到
0
的原因仅仅是因为...您在数组中有一个值。 As @Orangepill states in the comments, dump the values of $this->token
and $_SESSION['token]
to see what's going on. 正如@Orangepill在评论中指出的,转储
$this->token
和$_SESSION['token]
以查看发生了什么。
count()
returns the number of elements inside an array. count()
返回数组中元素的数量。 Right now you are just running count()
. 现在,您只是在运行
count()
。 You need to compare it to an integer value ie: 您需要将其与整数值进行比较,即:
count($this->errors)>0 ? 0 : 1;
Token is a mt_rand function, that why I think the problem is at the return statement. 令牌是一个mt_rand函数,这就是为什么我认为问题出在return语句上。 Also because I get error even when I call another function called valid_data(), that has the same return statement.
同样是因为即使我调用另一个名为valid_data()的函数时也会出错,该函数具有相同的return语句。 $token = $_SESSION['token'] = md5(uniqid(mt_rand(), true));
$ token = $ _SESSION ['token'] = md5(uniqid(mt_rand(),true));
Did you initialize $this->errors in your class before using it in valid_token()? 在valid_token()中使用它之前,您是否在类中初始化了$ this-> errors? If not, you may be counting something that is not set, which would return false.
如果没有,您可能正在计算未设置的内容,这将返回false。 Make sure you initialize the member like this:
确保像这样初始化成员:
protected $error = array(); 受保护的$ error = array();
or 要么
public $error = array(); 公共$错误= array();
Also, you used a ternary expression but you didn't wrap the condition in parentheses. 另外,您使用了三元表达式,但没有将条件包装在括号中。 Therefore, the statement might not have evaluated properly.
因此,该语句可能没有正确评估。 Try this:
尝试这个:
$isValidForm = (count($this->errors) > 0) ? false : true;
return $isValidForm;
I agree that you should "dump" the variables to see what your getting back. 我同意您应该“转储”变量,以查看返回的内容。 I would do this by logging their string values to the Apache error.log:
我可以通过将其字符串值记录到Apache error.log中来做到这一点:
error_log("this token: " . print_r($this->token, true)); error_log(“ this token:”。print_r($ this-> token,true));
error_log("session token: " . print_r($_SESSION['token'], true)); error_log(“会话令牌:”。print_r($ _ SESSION ['token'],true));
error_log("this error: " . print_r($this->error, true)); error_log(“此错误:”。print_r($ this-> error,true));
In GNU / Linux or OSX You can tail the log from the console like this: 在GNU / Linux或OSX中,您可以像这样从控制台拖尾日志:
tail -f /var/log/apache2/error.log 尾巴-f /var/log/apache2/error.log
This way you don't have to interrupt the flow of the program to debug it. 这样,您不必中断程序的流程即可对其进行调试。
Finally, and this is just a suggestion -- valid_token() is not a good method name, it sounds like a variable name. 最后,这只是一个建议-valid_token()不是一个好的方法名称,它听起来像一个变量名称。 validate_token() or validateToken() are better names because they use verbs to signify that they're actions.
validate_token()或validateToken()是更好的名称,因为它们使用动词来表示它们是动作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.