【发布时间】:2016-04-05 22:05:42
【问题描述】:
我已经为密码重置功能编写了一些代码。目前我生成一个令牌并发送一个链接,其中包含一个包含令牌的 URL 供用户访问,并重置他们的密码。我遇到的问题是,当我尝试点击发送给用户以确认新密码部分的链接时出现错误;
Impossible to access an attribute ("email") on a string variable ("someemail@gmail.com") in "confirm-new-password.twig" at line 45
我的模型中有一个函数可以检查令牌的时间戳是否已过期,如下所示。如果已过期,则返回 null,否则应返回存储在数组中的电子邮件和令牌的值,以便可以在 twig 模板中使用它来处理表单;
public function get_token($email,$token){
$email = mysqli_real_escape_string($this->link, $email);
$token = mysqli_real_escape_string($this->link, $token);
$result = mysqli_query($this->link, "select email, token, expirytime from user where email = '{$email}'");
$row = mysqli_fetch_assoc($result);
$time = strtotime($row['expirytime']);
$curtime = time();
$userResetDets = array($row['email'],$row['token']);
if($token === $row['token'] && (($curtime-$time) < 60)){
return $userResetDets;
}else{
$res = mysqli_query($this->link,"update user set token='' and expirytime='' where email = '{$email}'");
return null;
}
}
在我的控制器中,我有以下代码;
$app->get('/confirm-new-password/{email}/{token}', function($email,$token) use($app) {
$test = $app['auth']->get_token($email,$token);
if (null !== $test){
return $app['twig']->render('confirm-new-password.twig', array('active_page' => 'confirm-new-password', 'is_user_logged_in' => $app['auth']->is_user_logged_in(), 'items' => $app['tutor']->get_user_id(), 'test' => $test));
}else{
return $app->redirect('/');
}
});
在我的树枝文件中,我有以下内容;
<form class="form-signin" action="/confirm-new-password" method="post">
<h2 class="form-heading">Confirm New Password</h2>
<label for="inputNewPass1" class="sr-only">New Password</label>
<input type="password" id="inputNewPass1" class="form-control" name="pass1" placeholder="New Password" required>
<label for="inputNewPass2" class="sr-only">Re-Type New Password</label>
<input type="password" id="inputNewPass2" class="form-control" name="pass2" placeholder="Re-type New Password" required>
{% for items in test %}
<input type="hidden" name="email" value="{{ items.email }}">
<input type="hidden" name="token" value="{{ items.token }}">
{% endfor %}
<div class="spamCheck">
<label for="inputPostcode" class=sr-only">Postcode</label>
<input type="text" id="inputPostcode" class="form-control" name="postcode" placeholder="Leave this field blank" />
</div>
<button class="btn btn-lg btn-default btn-block" type="submit">Reset Password</button>
在我的模型中,我还具有获取主键的功能,以便循环遍历 twig 文件中的数组变量;
public function get_user_id(){
$result = mysqli_query($this->link, 'select email from user');
while($row = mysqli_fetch_assoc($result)){
foreach($row as $item){
$items = $item;
}
}
}
但是,当我尝试运行此代码时出现上述错误。我查看了this 并尝试按照上述说明将树枝文件中的forloop 更改为此;
{% if test %}
<input type="hidden" name="email" value="{{ test.email }}">
<input type="hidden" name="token" value="{{ test.token }}">
{% endif %}
然后我得到错误;
Key "email" for array with keys "0, 1" does not exist in "confirm-new-password.twig" at line 45
我还查看了this 并将forloop 再次更改为;
{% if test is defined %}
<input type="hidden" name="email" value="{{ test.email }}">
<input type="hidden" name="token" value="{{ test.token }}">
{% endif %}
但得到同样的错误;
Key "email" for array with keys "0, 1" does not exist in "confirm-new-password.twig" at line 45
我还查看了this,但又遇到了错误。
【问题讨论】: