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>';
}
}
這些函數在哪裏調用? –
嗨,你的意思是你無法獲得$ row ['username']的價值。 – vbrmnd
session_start();開始了嗎? –