2014-05-23 33 views
0

我想寫一個登錄/會話頁面的第一次在OOP風格。當我運行下面的腳本時,頁面重定向,但是當我嘗試回顯會話ID時,它不做任何顯示。 我是否正確地做這件事?會話ID沒有設置,但頁面登錄

<?php 
     session_start(); 
require_once('classes/function.php'); 
require_once('classes/user.php'); 

class Session{ 

    private $is_logged_in = FALSE; 
    private $id ; 
Public $ user_data; 
    private $email ; 

public function logout(){ 
    session_start(); 
    unset($_SESSION['id']); 
    unset($this->id); 
    session_destroy(); 
    $is_logged_in = FALSE; 
    Misc::redirect('index.php'); 
} 

public function is_logged_in(){ 
    return $this->is_logged_in ; 
} 

public function login($email, $password){ 
    global $user; 
    global $misc; 

    if($misc->check_form()){ 
     echo $this->error; 
    } 
    if (!MIsc::check_email_format($email)) { 
      echo "Email format is invalid"; 
      } 
      $login_user = $user->authenticate($email, $password); 
      if($login_user){ 
      $_SESSION['id'] = $this->id = $this->user_data['id']; 
      $_SESSION['email'] = $this->email = $this->user_data['email']; 
      $is_logged_in = TRUE; 
      Misc::redirect('127.0.0.1/users/index.php'); 
      } 
} 
} 

$session = new Session; 
?> 

我的登錄頁看起來像這樣

<?php 
require_once('classes/user.php'); 
require_once('classes/session.php'); 
require_once('classes/function.php'); 

if(isset($_POST['submit'])){ 
    try{ 
    $email =$_POST['email']; 
    $password = $_POST['password']; 
    $user = new Users; 
    $user->authenticate($email , $password); 
    $login = new session; 
    $login->login($email , $password); 
} 
catch (Exception $e){ 
    $error = new Errors(); 
    echo "<b>".$error->displayError($e)."</b>"; 
} 
} 

?> 

$ USER_DATA從這些方法

public function user_exist($email){ 
    global $db; 
    $stmt = $db->dbh->prepare("SELECT id, password, email FROM $this->table WHERE `email`= :email LIMIT 1"); 
    $stmt->bindValue(':email', $email); 
    $stmt->execute(); 
    if($stmt->rowCount() == 1) { 
    $this->user_data = $stmt->fetch(); 
    return TRUE; 
    } else { 
    throw new Exception('User do not exist'); 
    } 
} 

    /* 
    what does this method do 
*/ 
public function authenticate($email, $password){ 
    global $db; 
    $auth = $this->user_exist($email); 
    if($auth) { 
    if(password_verify($password, $this->user_data['password'])) { 
     return $this->user_data; 
    } else { 
     throw new Exception('Email/Password combination incorrect'); 
    } 
    } 
} 
+3

什麼是$ USER_DATA,在聲明? –

+4

將'session_start()'放在腳本的頂部,*放在類的外面。我沒有理由不總是有第一行調用這個函數。 –

+0

@MADTerry查看更新。 – hello

回答

1

也許你錯了在本節得到了..

 $login_user = $user->authenticate($email, $password); 
     if($login_user){ 
     $_SESSION['id'] = $this->id = $this->user_data['id']; 
     $_SESSION['email'] = $this->email = $this->user_data['email']; 
     $is_logged_in = TRUE; 
     Misc::redirect('127.0.0.1/users/index.php'); 
     } 

我可以看到$ user_data沒有在課堂上聲明,你應該在使用它之前聲明它。

另一個問題,我在你的OOP風格看

public function authenticate($email, $password){ 
    global $db;//you should have $db declared in class, SInce it is not used here you should remove 
    $auth = $this->user_exist($email); //Why you are assigning it to $auth you should directly place in condition if you need it once 
    if($auth) { 
    if(password_verify($password, $this->user_data['password'])) { //You already have $password variable 
     return $this->user_data; //you can return true instead of returning object which you are not reusing. 
    } else { 
     throw new Exception('Email/Password combination incorrect'); //i like it :) 
    } 
    } 

你也應該啓用錯誤報告,檢查問題

ini_set('display_startup_errors',1); 
ini_set('display_errors',1); 
error_reporting(-1); 
+0

我聲明$ user_data,但它沒有解決問題 – hello

+0

查看更新的答案,打開錯誤報告。發佈錯誤。 –

+0

沒有錯誤顯示在屏幕上 – hello