Announcement

Collapse
No announcement yet.

PHP Class Help

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP Class Help

    I've been working to put together a few standard library classes in PHP that I can use throughout my future sites without much tweaking, but I keep running into a certain error:

    Fatal error: Call to a member function on a non-object in /home/www/e-troop.awardspace.com/user.class.php on line 63

    I know that this means that the script is attempting to call a class function for a class object that has not been set yet, but here's where it gets interesting. I include a common.php file in every page, in this instance index.php. The index.php and common.php file look like so:

    index.php
    Code:
    <?php
    require_once('common.php');
    
    $user->session_resume();
    $auth->assign($user->sid);
    
    echo ('<html><head></head><body>');
    if ($user->level = 3) {echo 'Logged in as administrator.';}
    
    echo ('</body></html>');
    ?>
    common.php
    Code:
    <?php
    
    require_once ('config.php'); 
    require_once ('mysql.class.php');
    require_once ('user.class.php');
    require_once ('auth.class.php');
    
    $user = new User();
    $auth = new Auth();
    $db = new MySQL();
    
    $db->connect();
    ?>
    So common.php includes all of the class files required, and creates 3 new objects ($user, $auth and $db) and then connects to the database. Now that the included file (common.php) has successfully run, the script returns to the index.php file and tries to use the $user->session_resume(); function. Before I continue, here are the user.class.php, auth.class.php and mysql.class.php files:

    user.class.php
    Code:
    <?php
    class User {
    	var $sid;
    	var $old_sid;
    	var $uid = '0';
    	var $username;
    	var $password;
    	var $logged_in = false;
    	var $login_time;
    	var $last_resume;
    	var $level = '0';
    	var $hash;
    	var $error = array();
    
    	/* 
    	Checks to see if a session is already set or if a username/pw combo has been passed to this page for checking. 
    		If it finds a posted combo, it checks to see if it is valid
    			If valid, awesome! Regenerate Session Id, then add post variables and other info for hash to session superglobal and SQL database.
    			If not valid, create new session as guest.
    		If it finds a session combo, it checks to see if it is valid.
    			If valid, awesome! 
    			If not valid, unsets session variables and creates new session as guest.
    		If it finds nothing, create a new session as guest.
    	*/
    	function session_resume() {
    		$this->sid = isset($_COOKIE[$cfg_cookie . '_sid']) ? $_COOKIE[$cfg_cookie . '_sid'] : false;
    		$this->password = isset($_COOKIE[$cfg_cookie . '_pw']) ? $_COOKIE[$cfg_cookie . '_pw'] : false;
    		if (isset($_POST['username']) && isset($_POST['password'])) { // Login attempt
    			$data = $db->return_query('SELECT password, user_id FROM etroop_users WHERE user_name = ' . addslashes($this->username));
    			if ($data['password'] === $this->password) {
    				$this->old_sid = $this->fsid();
    				$this->sid = fsid(true); // regenerates session id
    				$this->logged_in = true;
    				$this->login_time = time();
    				$this->last_resume = time();
    				$this->uid = $data['user_id'];
    				$this->level = $data['user_level'];
    				// set a cookie with the secret session hash that protects session validity
    				setcookie($cfg_cookie . '_hash', md5($_SERVER['HTTP_USER_AGENT'] . '::' . $this->login_time . '::' . $this->last_resume . '::' . $this->sid . '::' . $this->password), $cfg_cookie_exp, '/', $cfg_cookie_domain);
    				$db->send_query('UPDATE etroop_sessions SET session_id = ' . $this->sid . ', session_start = ' . $this->login_time . ', session_update = ' . $this->last_resume . ', session_hash = ' . $_COOKIE[$cfg_cookie . '_hash'] . ', session_user_id = ' . $this->uid . ' WHERE session_id = ' . $this->old_sid);
    				unset($_POST['username']);
    				unset($_POST['password']);
    				unset($data);
    			} else {
    				$this->error[] = 'Wrong Username and Password Combination.';
    			}
    		} else if ($this->sid && $this->password) { // Session resume attempt
    			$data = $db->return_query('SELECT * FROM etroop_sessions WHERE session_id = ' . $this->fsid());
    			$check_hash = md5($_SERVER['HTTP_USER_AGENT'] . '::' . $data['session_start'] . '::' . $data['session_resume'] . '::' . $data['session_id'] . '::' . $_COOKIE[$cfg_cookie . '_pw']);
    			if ($data['hash'] === $check_hash) { // both hashes match then username/password match
    				$this->logged_in = true;
    				$this->sid = $this->fsid();
    				$this->login_time = $data['session_start'];
    				$this->last_resume = time();
    				$this->uid = $data['user_id'];
    				$this->level = $data['user_level'];
    				setcookie($cfg_cookie . '_hash', md5($_SERVER['HTTP_USER_AGENT'] . '::' . $this->login_time . '::' . $this->last_resume . '::' . $this->sid . '::' . $this->password), $cfg_cookie_exp, '/', $cfg_cookie_domain);
    				$db->send_query('UPDATE etroop_sessions SET session_hash = ' . $_COOKIE[$cfg_cookie . '_hash'] . ', session_resume = ' . $this->last_resume . ', session_user_id = ' . $this->uid . ' WHERE session_id = ' . $this->sid);
    				$this->logged_in = true;
    			} else {
    				$this->error[] = 'Session hacking attempt.';
    			}
    		} else if ($result = $db->return_query('SELECT session_id FROM etroop_sessions WHERE session_id = ' . $this->fsid())) { // session in progress as guest
    			$db->send_query('UPDATE etroop_sessions SET session_resume = ' . time() . ' WHERE session_id = ' . $this->fsid());
    		} else { // nothing found, create new session as guest
    			$this->fsid(true); // creates a new session id and sets it
    			$db->send_query('INSERT INTO etroop_sessions (session_id, session_uid, session_start, session_resume) VALUES (' . $this->fsid() . ', 0, ' . time() . ', ' . time() . ')');
    		}
    	}
    	
    	/*
    	By default, returns the current session id. If (true) is set, it creates a new session id and replaces the old one in the cookies.
    	*/
    	
    	function fsid($new = false) {
    		if ($new = true) {
    			setcookie($cfg_cookie . '_sid', md5(microtime * microtime . 'e-troop!random_value_cookie'), $cfg_cookie_exp, '/', $cfg_cookie_domain);
    		} else if (isset($_COOKIE[$cfg_cookie . '_sid'])) {
    			return $_COOKIE[$cfg_cookie . '_sid'];
    		} else {
    			return false;
    		}
    	}
    }
    ?>
    auth.class.php
    Code:
    <?php
    class Auth {
    	
    	function assign($f_sid) {
    		if ($user->logged_in) {
    			$data = $db->return_query('SELECT user_level FROM etroop_users WHERE user_id = ' . $user->uid);
    			$user->level = $data['user_level'];
    		} else {
    			$user->level = 0;
    		}
    	}
    }
    ?>
    mysql.class.php
    Code:
    <?php
    class MySQL {
    	var $name = 'server.name.com';
    	var $login = 'my_login_name';
    	var $password = 'my_password';
    	var $con;
    	var $error = array();
    
    	function connect() {
    		if (!$this->con = mysql_connect($this->name, $this->login, $this->password) && !mysql_select_db('main')) {$this->error[] = mysql_error();}
    	}
    	
    	function send_query($query) {
    		if (!mysql_query($query, $this->con)) {$this->error[] = mysql_error(); return false;} else {return true;}
    	}
    
    	function return_query($query) {
    		if (!mysql_query($query, $this->con)) {
    			$this->error[] = mysql_error();
    			return false;
    		} else {
    			$r = array();
    			$result = mysql_query($query);
    			while ($row = mysql_fetch_assoc($result)) {
    				$r[] = $row;
    			}
    			return $r;
    		}
    	}
    }
    ?>
    So the script goes to the user class and runs the session_resume() function. It goes down to line 63 and attempts to call the $db->return_query function, where I presume it calls an error because $db has not yet been set as a class.

    ...
    (line 63 of user.class.php) else if ($result = $db->return_query('SELECT session_id FROM etroop_sessions WHERE session_id = ' . $this->fsid())) { // session in progress as guest
    ...

    I do not see why I can't call functions of other classes from within a class if all the classes have been included, made into objects, and then called. Any help would be greatly appreciated, I have been struggling with this error for the past week with no avail. I modeled my class inclusion off of how phpBB does it through separate class files being included and made into objects through a common.php type file.

    Acid

  • #2
    Re: PHP Class Help

    It's telling you that $db isn't there. Either it's not connecting properly, or that line is getting called before the actual connection happens.
    [volun2]
    NS Game Officer. TF2 Admin. BF2 Admin / Scripter. PM with issues.
    Tempus: Pokerface is nailing it right on the head. Everyone who is arguing against him is simply arguing against reality.
    <anmuzi> it is not permitted to have privacy or anonymity
    <LazyEye> yeah when I play on TG the server digs though my trash

    Arm yourself with knowledge: TG NS TF2 BF2

    Comment

    Connect

    Collapse

    TeamSpeak 3 Server

    Collapse

    Advertisement

    Collapse

    Twitter Feed

    Collapse

    Working...
    X