如何使member_id可用于显示?

时间:2014-08-03 14:55:54

标签: php

我已经钻研了这个问题好几个小时但是因为我还是PHP的初学者,所以我无法弄清楚应该如何编辑这个文件。 我可以提供一些提示吗?

user.php的

<?php
include('password.php');

class User extends Password{

    private $_db;

    function __construct($db){
        parent::__construct();

        $this->_db = $db;
    }

    private function get_user_hash($username){  

        try {
            $stmt = $this->_db->prepare('SELECT password FROM members WHERE username = :username AND active="Yes" ');
            $stmt->execute(array('username' => $username));

            $row = $stmt->fetch();
            return $row['password'];

        } catch(PDOException $e) {
            echo '<p class="bg-danger">'.$e->getMessage().'</p>';
        }
    }

    public function login($username,$password,$member_id){

        $hashed = $this->get_user_hash($username);

        if($this->password_verify($password,$hashed) == 1){
            $_SESSION['loggedin'] = true;                    
            $_SESSION['username'] = $username; //I added this line and it works well. 
            $_SESSION['member_id'] = $member_id; //I added this line but the member_id just won't display.
            return true;
        }   
    }

    public function logout(){
        session_destroy();
    }

    public function is_logged_in(){
        if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
            return true;
        }       
    }



}

?>

我用这种方式显示我的用户名,它很完美。

$username = $_SESSION['username'];

但是,当我在member_id上应用相同的东西时,它无法显示任何内容。

$member_id = $_SESSION['member_id'];

提前致谢,请耐心等待我的愚蠢。 :(

1 个答案:

答案 0 :(得分:0)

查看您显示的代码,我怀疑您已经在某个地方拥有$member_id的值。我建议将它传递给你的login()方法。类似的东西: -

public function login($username, $password, $member_id)
{
    //The rest of your function
}

您的代码中的其他地方您需要具有以下内容: -

$user = new User();
$username = whereverYouGetUsernamesFrom();
$password = whereverYouGetPasswordsFrom();
$member_id = whereverYouGetMemberIdFrom();
$user->login($username, $password, $member_id);