2017-08-28 66 views
-1

我正在嘗試將用戶重定向到他們的配置文件,其用戶名在標題中。但是,重定向不顯示用戶名。登錄頁面要求輸入電子郵件和密碼(用戶名省略)。有人能告訴我我做錯了什麼嗎?登錄後重定向到配置文件(PDO)

在class.php:

public function register($uname,$email,$upass,$code) 
{ 
try 
{  
$password = md5($upass); 
$stmt = $this->conn->prepare("INSERT INTO tbl_users(userName,userEmail,userPass,tokenCode) 
              VALUES(:user_name, :user_mail, 
:user_pass, :active_code)"); 
$stmt->bindparam(":user_name",$uname); 
$stmt->bindparam(":user_mail",$email); 
$stmt->bindparam(":user_pass",$password); 
$stmt->bindparam(":active_code",$code); 
$stmt->execute(); 
return $stmt; 
} 
catch(PDOException $ex) 
{ 
echo $ex->getMessage(); 
} 
} 

public function login($email,$upass,$uname) 
{ 
try 
{ 
$stmt = $this->conn->prepare("SELECT * FROM tbl_users WHERE 
userEmail=:email_id AND userName=:user_name"); 
$stmt->execute(array(":email_id"=>$email,":user_name"=>$uname)); 
$userRow=$stmt->fetch(PDO::FETCH_ASSOC); 


if($stmt->rowCount() == 1) 
{ 
if($userRow['userStatus']=="Y") 
{ 
if($userRow['userPass']==md5($upass)) 
{ 
    $_SESSION['userSession'] = $userRow['userID']; 
    $_SESSION['userName'] = $userRow['userName']; 
    return true; 

重定向代​​碼:

public function redirectchannel() 
{ 
header("Location: index.php?id=$userName"); 
} 

登錄頁面:

if(isset($_POST['btn-login'])) 
{ 
$email = trim($_POST['txtemail']); 
$upass = trim($_POST['txtupass']); 

if($user_login->login($email,$upass)) 
{ 
$user_login->redirectchannel(); 
} 
} 

<input type="email" class="reginput" placeholder="Email address" name="txtemail" required /> 
<input type="password" class="reginput" placeholder="Password" name="txtupass" required /> 
<button class="formbutton" type="submit" name="btn-login">Login</button> 

和着陸頁:

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid"); 
$stmt->execute(array(":uid"=>$_SESSION['userSession'])); 
$row = $stmt->fetch(PDO::FETCH_ASSOC); 
+0

你在哪裏打電話redirectchannel? – Chris

+0

哪裏的代碼實際顯示任何東西?這裏有很多缺失。 –

+0

在登錄頁面上調用redirectchannel。更新了問題。 – Cordell

回答

0

雖然我不知道爲什麼把查詢字符串的用戶名,因爲你已經擁有它的會話,但仍:

public function redirectchannel() 
{ 
    // what is `$userName` here? It is NULL  
    header("Location: index.php?id=$userName"); 
} 

所以,如果你仍然想通過用戶名作爲查詢參數,我想你可以這樣做:

public function redirectchannel() 
{ 
    header("Location: index.php?id=" . $_SESSION['userName']); 
    exit; //always exit immediately after location headers. 
} 
+0

謝謝,這工作。 – Cordell

0

如果我正確理解這個,你不需要通過URL傳遞用戶名值。您已經使用用戶名創建會話變量:

$_SESSION['userName'] 

您可以在使用同一會話的任何頁面中使用此相同變量。只需創建像變量:

$username = $_SESSION['userName']; 

現在,您可以隨時在頁面上調用這個變量,像這樣:

User Name: <?php echo $username; ?>