OO:Call to a member function query_first() on a non-object in

SuReBuRn

Mitglied
Versuche grade ein LoginScript via OOP (halbswegs) zu realisieren. Dazu sei erwähnt, dass das ziemlich neu für mich ist, also bitte nicht schlagen :D

bekkome diesen Fehler:
Fatal error: Call to a member function query_first() on a non-object in /www/htdocs/w0070625/_acp/lib/modules/login.class.php on line 41
und weiss einfach nicht wieso... Hier mein Script:

login.php:
PHP:
$area = "login";
$fileName = "login.class.php";

// initiate acp
require_once(LIB_DIR."ACP.class.php");

// create classes
$db = &new db($sqlhost, $sqluser, $sqlpassword, $sqldb, $phpversion);
$login = new login();

login.class.php:
PHP:
/**
 * this class handles the login
 */
class login {
	protected $redirectionTime 		= 3;
	protected $redirectionLocation 	= "shoutbox.php";
	
	/**
	* constrcutor: if $username or $password exists:
	* redirect to shoutbox
	*/
	public function __construct() {
	
	
		$db = &new db($sqlhost, $sqluser, $sqlpassword, $sqldb, $phpversion);

		$POST_username 			= strip_tags(trim($_POST['username']));
		$POST_password 			= md5(strip_tags(trim($_POST['password'])));
		$COOKIE_username 		= strip_tags(trim($_COOKIE['fckr_password']));
		$COOKIE_password 		= md5(strip_tags(trim($_COOKIE['fckr_password'])));
		
		$this->loadTemplates("login.tpl");
		
		if ((isset($COOKIE_username) && $COOKIE_username != "") && (isset($COOKIE_password) && $COOKIE_password != "")) {
			echo "COOKIE JA";
			if ($this->checkUsernameAndPassword($this->getUserIdFromUsername($COOKIE_username), $COOKIE_password) == 1) {
				msgRedirect("Deine Anmeldung war erfolgreich.",$redirectionTime,$redirectionLocation);
				echo "COOKIE OK";
			} else {
				echo "COOKIE NICHT OK"."<br />";
			}
		} else {
			echo $POST_username."<br />";
			echo $POST_password."<br />";
			if ($this->checkUsernameAndPassword($this->getUserIdFromUsername($POST_username), $POST_password) == 1) {
				echo "POST OK";
			} else {
				echo "POST NICHT OK"."<br />";
			}
		}
	}
	
	public function getUserIdFromUsername($username) {
		$userData = $db->query_first("SELECT `userid` FROM `bb".N."_users` WHERE `username` LIKE ".$username."");
		$this->userid = $userData['userid'];
		echo "userid: ".$_POST['username'];
	}
	
	public function checkUsernameAndPassword($userid, $password) {
		$userData = $db->query_first("SELECT `username`, `password` FROM `bb".N."_users` WHERE `userid` = ".$userid."");
		if ($this->username == $userData['username'] && $this->password == $userData['password']) {
			echo "ERFOLGREICH";
			return 1;
		}
		else echo "NICHT ERFOLGREICH";
	}
	
	public function loadTemplates($tpl1, $tpl2 = "", $tpl3 = "", $tpl4 = "", $tpl5 = "", $tpl6 = "") {
		$tpl = &new tpl(TPL_DIR);
		$tpl->newFile($tpl1);
		$tpl->addPlHo("PHP_SELF",$_SERVER['PHP_SELF']);
		$tpl->addPlHo("POST_username",$_POST['username']);
		$tpl->addPlHo("POST_password",$_POST['password']);
		$tpl->parseTemplate();
	}
}
 
Versuch mal das:
PHP:
public function getUserIdFromUsername($username) {
        global $db;
        $userData = $db->query_first("SELECT `userid` FROM `bb".N."_users` WHERE `username` LIKE ".$username."");
        $this->userid = $userData['userid'];
        echo "userid: ".$_POST['username'];
    }
 
EDIT:

Wo kommt query_first() überhaupt her ?
 
Zuletzt bearbeitet von einem Moderator:
Versuch mal das:
PHP:
public function getUserIdFromUsername($username) {
        global $db;
        $userData = $db->query_first("SELECT `userid` FROM `bb".N."_users` WHERE `username` LIKE ".$username."");
        $this->userid = $userData['userid'];
        echo "userid: ".$_POST['username'];
    }

global sollte tunlichst vermieden werden, ist einer der unschönsten Programmierarten die es nur gibt...

@SuReBuRn

Ich würde dir empfehlen eine private Variable (= Eigenschaft) für deine Klasse zu erstellen, so musst du nur im Konstrukt die DB-Verbindung aufbaun, und kannst diese dann in der ganzen Klasse verwenden.

Dies sieht dann ungefähr so aus:
PHP:
class login {
    protected $redirectionTime         = 3;
    protected $redirectionLocation     = "shoutbox.php";
    protected $db;
    
    /**
    * constrcutor: if $username or $password exists:
    * redirect to shoutbox
    */
    public function __construct() 
    {
    
    
        $this->db = new db($sqlhost, $sqluser, $sqlpassword, $sqldb, $phpversion);
        /*
        ....
        */
    }

    public function getUserIdFromUsername($username) {
        $userData = $this->db->query_first("SELECT `userid` FROM `bb".N."_users` WHERE `username` LIKE ".$username."");
        $this->userid = $userData['userid'];
        echo "userid: ".$_POST['username'];
    }

Achja, das &-Zeichen vor Klasseninstanzierungen, also z.b.
PHP:
$db = &new db(...);
ist seit php5 deprecated (= veraltet) und bringt eher einen Nachteil als einen Vorteil.

Gruß

Devil
 
Mal eine Frage die mir jetzt offen bleibt.

Wie bekommst du eigentlich die DB-Klasse dort rein.
Weil ich meine du verwendest keine registry Klasse soweit ich sehe und auch übergibst du das DB-Objekt nicht im constructor....

MFG Niels
 
Die benötigten Klassen gibt es von hier:
PHP:
// initiate acp
require_once(LIB_DIR."ACP.class.php");

Mittlerweile sieht der Code so aus, falls es dich interessiert:
PHP:
class login {
	public $redirectionTime 		= 0;
	public $redirectionLocation 	= "shoutbox.php";
	
	public $postUsername;
	public $postPassword;
	public $cookieUsername;
	public $cookiePassword;
	

	public function __construct() {
		$this->initDB();
		$this->initMsg();
		$this->initFunc();
		$this->initSessions();
		$this->initCookies();
//		$this->initTpl();
		
		$this->loadTemplates("login.tpl");
		
		
		$this->checkPosts($_POST['username'], $_POST['password']);
	}

	

	public function __call($name, $args) {
		echo "You called for ".$name.", which does not exist. \n";
		exit;
	}
	

	public function initDB() {
		require_once(LIB_DIR."db.inc.php");
		$this->db = new db($sqlhost, $sqluser, $sqlpassword, $sqldb, $phpversion);
	}
	

	public function initMsg() {
		$this->msg = new msg();
	}
	

	public function initFunc() {
		$this->func = new func();
	}

	public function initSessions() {
		$this->session = new session();
	}
	

	public function initCookies() {
		$this->cookie = new cookie();
	}

	public function loadTemplates($tpl1, $tpl2 = "", $tpl3 = "", $tpl4 = "", $tpl5 = "", $tpl6 = "") {
		$tpl = new tpl(TPL_DIR);
		$tpl->newFile($tpl1);
		$tpl->addPlHo("PHP_SELF",$_SERVER['PHP_SELF']);
		$tpl->addPlHo("POST_username",$_POST['username']);
		$tpl->addPlHo("POST_password",$_POST['password']);
		$tpl->parseTemplate();
	}

	public function checkPosts($postUsername, $postPassword) {
		$this->username = strip_tags(trim($postUsername));
		$this->password = md5(strip_tags(trim($postPassword)));
		
		/*
		if ($this->username && $this->username != "") echo $this->username;
		if ($this->password && $this->password != "") echo $this->password;
		*/
		
		if ($this->username && $this->username != "" && $this->password && $this->username != "") $this->checkUsernameAndPassword($this->username, $this->password);
		
	}
	
	

	public function getUserIdFromUsername($username) {
		$userData = $this->db->query_first("SELECT `userid` FROM `bb".N."_users` WHERE `username` = '".$username."'");
		$this->userid = $userData['userid'];
		return $userData['userid'];
	}
	

	 public function checkPermissions($userid) {
		 $userPermissions = $this->db->query_first("SELECT `groupid` FROM `bb".N."_user2groups` WHERE `userid` = '".$userid."'");
		 return $userPermissions['groupid'];
	 }
	
	

	public function checkUsernameAndPassword($username, $password) {
		$userData = $this->db->query_first("SELECT `userid`, `username`, `password` FROM `bb".N."_users` WHERE `username` = '".$username."'");
		if ($this->username == $userData['username'] && $this->password == $userData['password']) {
			// get userid
			$userid = $this->getUserIdFromUsername($this->username);
			// check permissions
			if ($this->checkPermissions($userid) == 1) {
				$this->cookie->setCookie($this->userid, $this->password);
				
				// $this->func->metaRedirect($this->redirectionTime, $this->redirectionLocation);
				
			}
			else $this->msg->printError("Du hast hier keinen Zugriff.");
		}
		else $this->msg->printError("Deine Anmeldung war nicht erfolgreich.");
	}
}
 
Zurück