2014-07-11 74 views
0

這是一個類中的功能和它的作品分裂一個類中的功能

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

現在,我想上面的代碼分離成兩個功能

public function login($username,$password){ 

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

    if($this->password_verify($password,$hashed) == 1){ 
     $_SESSION['loggedin'] = true; // this works 
     return true; 
    } 
} 

上面的代碼也適用,但在波紋管部分我無法得到的值$_SESSION["uname"]

public function get_uname(){ 

     $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']; // this doesn't work 
     } 
     catch(PDOException $e) { 
      echo '<p class="bg-danger">'.$e->getMessage().'</p>'; 
     } 

    } 
+0

這些函數在哪裏調用? –

+0

嗨,你的意思是你無法獲得$ row ['username']的價值。 – vbrmnd

+1

session_start();開始了嗎? –

回答

2

它實際上很簡單wh你退後一步,看看它。

你的第一個功能是這樣的:

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? 

所以,想必$this->get_user_hash()會返回任何當輸入爲空(也許爲false)時,你已經編程它,所以你的查詢不起作用,因爲$hashed沒有任何意義。合理?

+0

是的,當然,非常感謝。 – bonaca