在树枝

时间:2016-04-05 22:05:43

标签: php mysql symfony twig

我已经为密码重置功能编写了一些代码。目前,我生成一个令牌,并发送一个包含令牌的URL的链接,供用户访问,并重置他们的密码。我遇到的问题是,当我尝试按照发送给用户的链接确认新密码部分时出现错误;

Impossible to access an attribute ("email") on a string variable ("someemail@gmail.com") in "confirm-new-password.twig" at line 45

我的模型中有一个函数,用于检查令牌的时间戳是否已过期,如下所示。如果已过期则返回null,否则它应返回存储在数组中的电子邮件和令牌的值,以便可以在树枝模板中使用它来处理表单;

    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('/');
}

});

在我的twig文件中,我有以下内容;

<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并尝试按照提到的说明进行操作,方法是将twig文件中的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,但又遇到了错误。

1 个答案:

答案 0 :(得分:0)

这一行:

$userResetDets = array($row['email'],$row['token']);

创建一个包含2个元素的数组,索引为01,但您之后尝试将其用作关联数组。将其更改为:

$userResetDets = array(
  'email' => $row['email'],
  'token' => $row['token']
);