调用成员函数prepare()on null我的代码出错

时间:2017-05-18 16:59:39

标签: php

这是我的代码我正在调用成员函数prepare()on null我的代码错误可以任何人请帮我解决这个问题。这个问题在它正常工作前几天突然发生,但现在它在我尝试将用户名和密码添加到文本字段时显示

class LoginSystem {

	/* Start Config */

	private $dbhost 			= "";	
	private $dbport 			= "";	
	private $dbuser 			= "";	
	private $dbpass 			= "";	
	private $dbname 			= "";	
	private $dbtable 			= "";	
 	private $pages 				= "";	
    private $news				= "";
	private $doct				= "";
	private $health				= "";
	private $dept				= "";
	private $features			= "";
	private $epages 			= "";
	private $tips 				= "";	
	private $core 				= "";	
	private $banner				= "";	
 
	private $secureKey		= "";	
	private $passwordSalt 	= "";	
	private $company		= "xyz";	
	var $phpsessionstart	= true;	
	var $emailLogin			= false;	
	var $rememberMe			= false;	
	
		
		
		public $staticPages		= array(
			"/register.php"
 		);	
 	
		private $loginPage		= "/index.php"; 
		private $homePage			= "/home.php";	
 
	public $loggedIn 		= false;
	public $db				= true;
	public $user			= false;
	private $cookie;
	private $session;
	private $remCook;
	private $dbh;
	private $initCalled	= false;
 
	public function __construct(){
		if($this->phpsessionstart == true){
			session_start();
		}
		
		try{
			
			array_push($this->staticPages, $this->loginPage);
			
			$this->dbh		 = new PDO("mysql:dbname={$this->dbname};host={$this->dbhost};port={$this->dbport}", $this->dbuser, $this->dbpass,array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
  ));
			$this->db 		 = true;
			$this->cookie	 = isset($_COOKIE['logSyslogin']) ? $_COOKIE['logSyslogin'] : false;
			$this->session  = isset($_SESSION['logSyscuruser']) ? $_SESSION['logSyscuruser'] : false;
			$this->remCook  = isset($_COOKIE['logSysrememberMe']) ? $_COOKIE['logSysrememberMe'] : false;
			
			$encUserID 		 = hash("", "{$this->secureKey}{$this->session}{$this->secureKey}");
			$this->loggedIn = $this->cookie == $encUserID ? true : false;
			
			
			if($this->rememberMe === true && isset($this->remCook) && $this->loggedIn === false){
				
				$encUserID		 = hash("", "{$this->secureKey}{$this->remCook}{$this->secureKey}");
				$this->loggedIn = $this->cookie == $encUserID ? true : false;
				
				if($this->loggedIn === true){
					$_SESSION['logSyscuruser'] = $this->remCook;
				}
			}
			
			$this->user = $this->session;
			return true;
			
		}catch( PDOException $e ) {
			return false;
		}
	}
	
	
	public function init() {
		if( $this->loggedIn && array_search($this->curPage(), $this->staticPages) !== false ){
			$this->redirect($this->homePage);
		}elseif( !$this->loggedIn && array_search($this->curPage(), $this->staticPages) === false ){
			$this->redirect($this->loginPage);
		}
		$this->initCalled = true;
	}
	
	
	public function login($username, $password, $cookies = true){
		if($this->db === true){
			
			/* We Add LIMIT to 1 in SQL query because we need to just get an array of data with key as the column name. Nothing else. */
			if($this->emailLogin === true){
				$query = "SELECT `id`, `password`, `password_salt` FROM `{$this->dbtable}` WHERE `username`=:login OR `email`=:login ORDER BY `id` LIMIT 1";
			}else{
				$query = "SELECT `id`, `password`, `password_salt` FROM `{$this->dbtable}` WHERE `username`=:login ORDER BY `id` LIMIT 1";
			}
			
			$sql = $this->dbh->prepare($query);
			$sql->bindValue(":login", $username);
			$sql->execute();
			
			if($sql->rowCount()==0){
				return false;
			}else{
				/* Get the user details */
				$rows			= $sql->fetch(PDO::FETCH_ASSOC);
				$us_id		= $rows['id'];
				$us_pass 	= $rows['password'];
				$us_salt 	= $rows['password_salt'];
				$saltedPass = hash('', "{$password}{$this->passwordSalt}{$us_salt}");
				
				if($saltedPass == $us_pass){
					if($cookies === true){
						
						$_SESSION['logSyscuruser'] = $us_id;
						setcookie("logSyslogin", hash("", $this->secureKey.$us_id.$this->secureKey), time()+3600*99*500, "/");
						
						if( isset($_POST['remember_me']) && $this->rememberMe === true ){
							setcookie("logSysrememberMe", $us_id, time()+3600*99*500, "/");
						}
						$this->loggedIn = true;
						if( $this->initCalled ){
							$this->redirect($this->homePage);
						}
					}
					return true;
				}else{
					return false;
				}
			}
		}
	}

1 个答案:

答案 0 :(得分:0)

对不起,这不是一个答案,但需要空间来输入所有

删除一些代码并添加debug echos:

<?php
class LoginSystem {

    /* Start Config */

    private $dbhost             = "";
    private $dbport             = "";
    private $dbuser             = "";
    private $dbpass             = "";
    private $dbname             = "";
    private $dbtable            = "";
    private $dbh;
    public  $db                 = true;

    public function __construct(){

        try{
            echo '<p>trying to create connection</p>'; // Debug echo
            $this->dbh       = new PDO("mysql:dbname={$this->dbname};host={$this->dbhost};port={$this->dbport}", $this->dbuser, $this->dbpass,array(
               PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
               PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
            ));
            $this->db        = true;
            echo '<p>made it</p>'; // Debug echo

            return true;

        }catch( PDOException $e ) {
            echo '<p>'.$e->getMessage() . ' - Failing</p>';  // Debug echo
            return false;
        }
    }

    public function login($username, $password, $cookies = true){
        if($this->db === true){

            /* We Add LIMIT to 1 in SQL query because we need to just get an array of data with key as the column name. Nothing else. */

            if($this->emailLogin === true){
                $query = "SELECT `id`, `password`, `password_salt` FROM `{$this->dbtable}` WHERE `username`=:login OR `email`=:login ORDER BY `id` LIMIT 1";
            }else{
                $query = "SELECT `id`, `password`, `password_salt` FROM `{$this->dbtable}` WHERE `username`=:login ORDER BY `id` LIMIT 1";
            }
            echo "<pre>"; // Debug string
            var_dump($this->dbh); // Debug string
            echo "</pre>"; // Debug string

            $sql = $this->dbh->prepare($query);
            $sql->bindValue(":login", $username);
            $sql->execute();

            if($sql->rowCount()==0){
                return false;
            }else{
                return true;
            }
        }
    }
}

$a = new LoginSystem();
$a->login('a','b');

我发现你的代码在给出正确的凭据和表名(我假设你硬编码为私有变量)的情况下有效。此外,你得到的准备警告通常意味着$this->dbh不是正确的对象或未定义,请尝试在我拥有的位置添加var_dump(),并告诉我们$this->dbh的内容。

对于某些事情你可以尝试:

请检查数据库主机上的配置,名称,通行证,端口等

同时检查您的PDO是否已启用,并检查您自己的错误消息以获取更多信息

最后将您的password_salt存储在您的用户数据库中通常不是一个好主意,它与锁定您的房子并将您的钥匙放在门垫下相同。