我正在开发一个登录系统。我有我的登录表单的帖子检查。php。
如果用户详细信息不正确,我正在尝试编写一个函数来测试失败登录尝试的次数。
<?php
//from check.php
if (blah)
{
$_SESSION['error'] = "<strong>Details not correct.</strong> Please try again.";
//$_SESSION['email'] = $email;
$_SESSION['attempts'] = 1; //first attempt
if(isset($_SESSION['attempts']))
{
$_SESSION['fail'] = $_SESSION['attempts']++; //increment
}
//$_SESSION['fail'] is echo'd on my login form
}
这段代码除了逻辑
没有任何错误。如果下面的代码在任何函数中。显然,它会在增加到2
后停止。因此,$_SESSION['attempts']
在每次函数调用中都被设置为1
。你必须在函数调用之前设置$_SESSION['attempts'] = 1
。
<?php
if (blah)
{
$_SESSION['error'] = "<strong>Details not correct.</strong> Please try again.";
if(isset($_SESSION['attempts']))
{
$_SESSION['fail'] = $_SESSION['attempts']++; //increment
}
}
登录表单发送到check.php,其中包含函数如果生成错误,则将重定向回登录表单。 ~ @user3464091
如果是这种情况,那么请修改我的代码如下。
说明:在这种情况下,每次提交登录凭据后,它都会来到这个函数。并且,如果$_SESSION['attempts']
已经定义。然后,它会增加到1。并且,如果没有设置$_SESSION['attempts']
。然后,它将初始值设为1。
[注意:登录成功后不要忘记unset($_SESSION['attempts']);
]
<?php
if (blah)
{
$_SESSION['error'] = "<strong>Details not correct.</strong> Please try again.";
if(isset($_SESSION['attempts']))
{
$_SESSION['fail'] = $_SESSION['attempts']++; //increment
} else {
$_SESSION['attempts'] = 1;
}
}
在将会话值存储在变量中之后对其进行递增。把
$_SESSION['fail'] = $_SESSION['attempts']++;
$_SESSION['fail'] = ++$_SESSION['attempts'];
同时,在$_SESSION['attempts']
中存储失败的尝试,否则它不会超过2递增。
$_SESSION['attempts'] = $_SESSION['fail'];
您可以简单地使用:
$_SESSION['fail']= $_SESSION['attempts']+1;
卡住的问题是,它首先定义会话变量,然后执行程序。因此,请确保不要设置变量,除非它还没有设置。
即:
if(!isset($_SESSION['attempts'])) { $_SESSION['attempts']=1;}
<?php
if (blah)
{
$_SESSION['error'] = "<strong>Details not correct.</strong> Please try again.";
//$_SESSION['email'] = $email;
if(isset($_SESSION['attempts'])) // check session attempts exist
{
$_SESSION['fail'] = $_SESSION['attempts']++; //session exist so do increment
}
else
{
$_SESSION['attempts'] = 1; // session attempts not exist condition so make first attempt
}
//$_SESSION['fail'] is echo'd on my login form
}
像
一样使用代码//from check.php
if (blah)
{
$_SESSION['error'] = "<strong>Details not correct.</strong> Please try again.";
//$_SESSION['email'] = $email;
if(!isset($_SESSION['attempts']))
{
$_SESSION['attempts'] = 1; //first attempt
}
else
{
$_SESSION['attempts']++; //increment
}
$_SESSION['fail'] = $_SESSION['attempts'];
//$_SESSION['fail'] is echo'd on my login form
}
remove $_SESSION['attempts'] = 1;在你的if条件
应该是这样的…
if(isset($_SESSION['attempts'])) { $_SESSION['fail'] = ++$_SESSION['attempts']; } else { $_SESSION['attempts'] = 1; }