Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在类内拆分函数_Php - Fatal编程技术网

Php 在类内拆分函数

Php 在类内拆分函数,php,Php,这是类中的一个函数,它可以工作 public function login($username,$password){ $hashed = $this->get_user_hash($username); try { $stmt = $this->_db->prepare('SELECT username FROM members WHERE password = :password AND active="Yes"

这是类中的一个函数,它可以工作

public function login($username,$password){

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

        try {
            $stmt = $this->_db->prepare('SELECT username FROM members WHERE password = :password AND active="Yes" ');
            $stmt->execute(array('password' => $hashed));
            $row = $stmt->fetch();
            $_SESSION["uname"] = $row['username'];
        }
        catch(PDOException $e) {
            echo '<p class="bg-danger">'.$e->getMessage().'</p>';
        }

        if($this->password_verify($password,$hashed) == 1){
            $_SESSION['loggedin'] = true;
            return true;
        }   
    }
上面的代码也可以工作,但在下面的部分中,我无法获得会话[“uname”]

公共函数get_uname(){
$hashed=$this->get\u user\u hash($username);
试一试{
$stmt=$this->_db->prepare('从成员中选择用户名,其中password=:password和active=“Yes”);
$stmt->execute(数组('password'=>$hashed));
$row=$stmt->fetch();
$\u SESSION[“uname”]=$row['username'];//这不起作用
}
捕获(PDO$e){
echo'

。$e->getMessage()。

; } }
当你退一步看它时,它其实很简单

您的第一个函数如下所示:

public function login($username,$password){
注意,您传递的是
$username
,由以下人员获取:

 $hashed = $this->get_user_hash($username);
在新的拆分函数中,您不会传递
$username

public function get_uname(){

    $hashed = $this->get_user_hash($username); //where is username coming from?

因此,当输入为null(可能为false)时,
$this->get\u user\u hash()
可能会返回您编程的任何内容,因此您的查询不起作用,因为
$hash
没有任何意义。有意义吗?

这些函数在哪里调用?您好,您的意思是您无法获取$row['username']的值。session_start();开始?@vaibhavmande,是的,就是这样。顺便说一句,用户密码是用唯一的盐散列的吗?如果两个用户的密码相同,会发生什么情况?
 $hashed = $this->get_user_hash($username);
public function get_uname(){

    $hashed = $this->get_user_hash($username); //where is username coming from?