我正在尝试通过我的登录页面登录。每次我在字段中输入一个值或只是按登录时,我都会注意到在我的浏览器上它在运行方法处停止并且我的页面留空。
我的数据库有两列,实际标题为“用户名”和“密码”。 我似乎无法弄清楚是什么原因造成的。
这是我的 login_model.php 文件:
<?php
class Login_Model extends BaseModel {
public function __contruct(){
parent:: __construct();
}
public function run() {
$sth = $this->database->prepare("SELECT id FROM users WHERE
Username = :username AND Password = :password");
$sth->execute(array(
':username' => $_POST['username'],
':password' => $_POST['password']
));
//$data = $sth->fetchAll();
$count = $sth->rowCount();
if ($count > 0){
//login
Session::init();
Session::set('loggedIn', true);
header('location: ../controllers/dashboard');
} else {
//show error
header('location: ../login/loginIndex');
}
}
}
?>
这是我在 Controller 的 login.php 文件中的内容:
<?php
class Login extends BaseController {
function __construct(){
parent::__construct();
}
//this is to avoid interference with the page that I want to call
function index() {
$this->view->render('../views/login/loginIndex');
}
function run(){
$this->model->run();
}
// public function other() {
// require '../models/loginModel.php';
// $model = new loginModel();
// }
}
这是我想要结束的地方:
<?php
class Dashboard extends BaseController {
function __construct(){
parent::__construct();
Session::init();
// You set the variable session: LoggedIn if they pass the condition
$logged = Session::get('loggedIn');
if ($logged == false){
Session::destroy();
header('location: ../login/loginIndex');
exit;
}
}
//this is to avoid interference with the page that I want to call
function index() {
$this->view->render('../views/dashboard/adminPage');
}
}
更新:
所以在添加了您提到的以下代码之后,这就是我得到的:
Notice: Undefined property: Login::$model in /apps/help-i-need-a-tutor/controllers/login.php on line 19
Fatal error: Call to a member function run() on a non-object in /apps/help-i-need-a-tutor/controllers/login.php on line 19
我该如何找出我的问题的原因?我查看了 login_model.php 类中的 run 方法,但似乎无法找到此错误的来源。
最佳答案
错误消息是说您在 Login
中没有 Login_Model
的实例。
尝试在 Login
中的构造函数上方插入 private $model
,然后在构造函数中使用 $model = new Login_Model()
分配给它>
关于php - 每次尝试登录或按登录时,我都卡在运行方法中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33683204/