dongpai2468 2016-04-05 22:05
浏览 27
已采纳

在树枝中访问字符串变量错误的属性

I have written some code for a password reset function. At the moment I generate a token and send a link with a url containing the token for the user to visit, and reset their password. The issue I am having is that I get the error when trying to follow the link that is sent to the user for the confirm new password part;

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

I have a function in my model that checks whether the timestamp for the token has expired or not as show below. If has expired return null otherwise it should return the values for the email and token stored in an array so that it can be used in the twig template in order to process the form;

    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;
      }

  }

in my controller I have the following code;

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

});

and in my twig file I have the following;

<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>

also in my model I have the function to get the primary key in order to loop through the array variables in twig file;

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;
        }
    }
}

However I get the error above when I try to run this code. I had a look at this and tried to follow the instructions as mentioned by changing my forloop within the twig file to this;

  {% if test %}

        <input type="hidden" name="email" value="{{ test.email }}">
        <input type="hidden" name="token" value="{{ test.token }}">

 {% endif %}

then I get the error;

Key "email" for array with keys "0, 1" does not exist in "confirm-new-password.twig" at line 45

I also had a look at this and changed the forloop again to;

  {% if test is defined %}

        <input type="hidden" name="email" value="{{ test.email }}">
        <input type="hidden" name="token" value="{{ test.token }}">

 {% endif %}

but get the same error;

Key "email" for array with keys "0, 1" does not exist in "confirm-new-password.twig" at line 45

I also had a look at this but got an error as well again.

  • 写回答

1条回答 默认 最新

  • dsnm64969 2016-04-05 22:28
    关注

    This line:

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

    Creates an array with 2 elements, at indices 0 and 1, but you are trying to use it like an associative array later. Change it to this:

    $userResetDets = array(
      'email' => $row['email'],
      'token' => $row['token']
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题