【发布时间】:2019-02-27 16:17:29
【问题描述】:
似乎在类方法中启动会话似乎不起作用。
最简单的方法是在脚本的开头启动会话,但如果在不使用的情况下运行,它是否不需要额外的服务器资源来维护?
仅在用户登录时启动和运行它似乎更直观。如何在类方法中执行它?
这是一长串代码,所以我没有先包含它,以免打扰您。您应该只考虑使用第 3 个 if 语句和 userSession() 方法。
class Users extends Controller
{
public function login(){
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//Sanitize input data
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$data = [
'email' => trim($_POST['email']),
'password' => trim($_POST['password']),
'email_err' => '',
'password_err' => ''
];
if ($this->userModel->findUserByEmail($data['email'])) {
//user found, Login
if ($userRow = $this->userModel->login($data['email'], $data['password'])){
$this->userSession($userRow);
$this->view("pages/index");
} elseif (!$this->userModel->login($data['email'], $data['password'])){
$data['password_err'] = 'hujovas passwordas';
$this->view("users/login", $data);
}
} else {
//mail not found
$data['email_err'] = 'email not found';
$this->view('users/login', $data);
}
} else {
//Sitas default
$data = ['email' => '', 'password' => '', 'email_err' => '', 'password_err' => ''];
$this->view("users/login", $data);
}
}
public function userSession($userRow){
session_start();
$_SESSION['id'] = $userRow->ID;
$_SESSION['email'] = $userRow->email;
$_SESSION['firstname'] = $userRow->firstname;
$_SESSION['lastname'] = $userRow->lastname;
redirect('pages/index');
}
}//class
【问题讨论】:
-
你这么有名的这门课在哪里?
-
doesn't seem to work是什么意思 -
我调用 session_start();在登录类的方法中,该方法似乎没有以任何方式注册。公共函数 userSession($userRow()){ session_start(); } 不起作用。
-
不幸的是,这还不足以提供帮助。据我所知,你把它放在了错误的行上——我猜不出你的代码来帮助找到问题
-
是什么让您认为某事不起作用?
session_start在哪里被调用应该无关紧要如果它被调用