PHP无法检查登录详细信息

时间:2016-09-09 09:08:27

标签: php class session cookies login

我正在学习PHP并开始使用课程。我正在尝试创建登录系统但无法从数据库验证用户名和密码。当我点击登录时,它不会让我进去。我试图通过echo和die()检查我在一个函数内的距离,我被_ login_action()中的_check_db check_db()所困,<?php class adminLogin { private $_host="localhost"; private $_username="root"; private $_password=""; private $_database="sms"; private $_conn; private $_loginusername; private $_loginpassword; public function __construct() { session_start(); if(isset($_POST['loginusername'])) { $this->_loginusername = $_POST['loginusername']; } if(isset($_POST['loginpassword'])) { $this->_loginpassword = $_POST['loginpassword']; //get the username and password } } protected function _dbConnect() { try { $this->_conn = new PDO("mysql:host=$this->_host;dbname=$this->_database", $this->_username, $this->_password); return $this->_conn; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); } } public function authenticate() { //first check whether session is set or not if(!isset($_SESSION['admin_login'])) { //check the cookie if(isset($_COOKIE['loginusername']) && isset($_COOKIE['loginpassword'])) { //cookie found, is it really someone from the if($this->_check_db($_COOKIE['loginusername'], $_COOKIE['loginpassword'])) { $_SESSION['admin_login'] = $_COOKIE['loginusername']; header("location: /views/adminView.php"); die(); } else { header("location: /index.php"); die(); } } else { header("location: /index.php"); die(); } } } /** * Check for login in the action file */ public function login_action() { //insufficient data provided if ($this->_loginusername == '' || $this->_loginpassword == '') { header("location: /index.php"); } //check the database for username if ($this->_check_db($this->_loginusername,$this->_loginpassword)) { $_SESSION['admin_login'] = $this->_loginusername; //ready to login //check to see if remember, ie if cookie if (isset($this->$_POST['rememberlogin'])) { //set the cookies for 1 day, ie, 1*24*60*60 secs //change it to something like 30*24*60*60 to remember user for 30 days setcookie('loginusername', $this->_loginusername, time() + 30 * 24 * 60 * 60); setcookie('loginpassword', $this->_loginpassword, time() + 30 * 24 * 60 * 60); } else { //destroy any previously set cookie setcookie('loginusername', '', time() - 1 * 24 * 60 * 60); setcookie('loginpassword', '', time() - 1 * 24 * 60 * 60); } header("location: /views/adminView.php"); } else { header("location: /index.php"); } die(); } private function _check_db($passedusername,$passedpassword) { $this->_dbConnect(); $checkAdminQuery = $this->_conn->prepare("SELECT * FROM `admins` WHERE `username` = ?"); $checkAdminQuery->execute(array($passedusername)); $row = $checkAdminQuery->fetch(); if ($row['username'] == $passedusername && $row['password'] == $passedpassword) { $this->_conn = null; //close the connection return true; } else { return false; } }}?> 没有返回某些内容,或者我的陈述中出现了问题。

我的班级adminLoginModel.php:

<div class="login-panel panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Please Sign In</h3>
        </div>
        <div class="panel-body">
            <form role="form" id="loginForm" method="post" action="/controllers/mainController.php">
                <fieldset>
                    <div class="form-group">
                        <input id="loginusername" class="form-control" placeholder="Username" name="loginusername" type="text" autofocus>
                    </div>
                    <div class="form-group">
                        <input id="loginpassword" class="form-control" placeholder="Password" name="loginpassword" type="password" value="">
                    </div>
                    <div class="checkbox">
                        <label for "rememberlogin">
                            <input name="rememberlogin" id="rememberlogin" type="checkbox" value="Remember Me">Remember Me
                        </label>
                    </div>
                    <!-- Change this to a button or input when using this as a form -->
                    <button class="btn btn-lg btn-success btn-block" value="submit" name="login" onclick="return loginCheck()" >Login</button>
                </fieldset>
            </form>
        </div>
    </div>

登录表单:

if(isset($_POST['login']))
{
    include_once(ROOT.'/models/adminLoginModel.php');
    $admin = new adminLogin();
    $admin->login_action();

}

在控制器中有一点代码:

"<li class='Item show'><a class='Link' onclick='console.log(\"hello\")'>click</a></li>"

0 个答案:

没有答案