douzhi2012 2016-04-04 01:58
浏览 57

尝试回显值时未定义的索引错误,我正在使用类外的函数

Hey guys I have a question and I still consider myself pretty new at coding, so forgive me if I come off foolish.

I am studying in school as of now and we have a project to build a full stack recreation of craigslist. Any who the problem I am having deals with PHP. I have created an account page with text areas. I would like to echo out the user's information on their so the user can see what he put on and update as he likes. Since my navbar is included on every page, I added the code:

if(isset($_SESSION['logged_in_user'])){
      var_dump($_SESSION['logged_in_user']);
      $user = $_SESSION['logged_in_user'];
      var_dump($user);
 }

on my account page I figured I can echo it out as <?= $attributes['first_name']?> within the placeholders. But I keep getting:

Undefined index: first_name

Also when I var_dump($user) I get an protected $attributes array.

In My Auth class is where I first defined $user as such:

public static function attempt($attemptedUsername, $attemptedPassword) {

$user = User::findByUserName($attemptedUsername);
    if ($user == null) {
        return false;
    }

    $validPassword = password_verify($attemptedPassword,$user->password);
    if ($validPassword == true) {
        $_SESSION['logged_in_user'] = $user;            
    }    
    return false;       
}   

and my findByUserName function is in the user class. the code is:

public static function findByUserName($user_name){

        // Get connection to the database
        self::dbConnect();

        $stmt = self::$dbc->prepare('SELECT * FROM users WHERE user_name = :user_name');

        $stmt->bindValue(':user_name', $user_name , PDO::PARAM_STR);

        //execute gets its own line, t or false
        $stmt->execute();

        $result=$stmt->fetch(PDO::FETCH_ASSOC);

        // @TODO: Create select statement using prepared statements

        // @TODO: Store the result in a variable named $result

        // The following code will set the attributes on the calling object based on the result variable's contents
        $instance = null;
        if ($result) {
            $instance = new static($result);
        }
        return $instance;
    }
  • 写回答

1条回答 默认 最新

  • duanpacan2583 2016-04-04 02:27
    关注

    Your problem seems to be with not being able to access the variable $user outside of the static method attempt() this can be fixed by declaring the variable globally at the beginning of the method attempt() like this:

    public static function attempt($attemptedUsername, $attemptedPassword) {
    
    global $user;
    $user = User::findByUserName($attemptedUsername);
        if ($user == null) {
            return false;
        }
    
        $validPassword = password_verify($attemptedPassword,$user->password);
        if ($validPassword == true) {
            $_SESSION['logged_in_user'] = $user;
    
        }
    
        return false;
    
    
    }
    

    More information can be found on this in the PHP documentation here.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题