我想寫一個登錄/會話頁面的第一次在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');
}
}
}
什麼是$ USER_DATA,在聲明? –
將'session_start()'放在腳本的頂部,*放在類的外面。我沒有理由不總是有第一行調用這個函數。 –
@MADTerry查看更新。 – hello