rofl90 Posted March 29, 2008 Share Posted March 29, 2008 This is a lot of code, but most of it might appear to be junky, I really do appreciate any help, been stuck on this for hours here it is: errors: Notice: Undefined variable: db in /home/fhlinux160/p/x/user/htdocs/backend/classes/user.php on line 38 Fatal error: Call to a member function query() on a non-object in /home/fhlinux160/p/x/user/htdocs/backend/classes/user.php on line 38 Code: config.php: <?php /* * Config */ $root = "/home/fhlinux160/p/x/user/htdocs/backend/"; $url = "http://x.com"; $classes = $root . "classes/"; /* Include Database.php */ require_once $classes . "database.php"; /* Define Database Settings */ $database ["server"] = "x"; $database ["username"] = "x"; $database ["password"] = "x"; $database ["database"] = "x"; /* Connect */ $db->connect ( $database ["server"], $database ["username"], $database ["password"], $database ["database"] ); /* More includes */ require_once $classes . "user.php"; require_once $classes . "settings.php"; /* Define UserID */ $myuid = session_id(); ?> User.php: <?php class user { function __construct() { } public function user_type($page) { global $logged_user; $user = $logged_user; $level = $db->query("SELECT * FROM users WHERE user='$user'"); $level_a = $db->fetch_array($level); $final_level = $level_a['usergroup']; $check_level = $db->query("SELECT * FROM userstructure WHERE name='$final_level'"); $check_level_a = $db->fetch_array($check_level); if ( $check_level_a[$page] == '1' ) { return "1"; } else { return "0"; } } public function getuid() { $session = session_id(); $get_user_id = $db->query("SELECT * FROM session WHERE session_id='$session'"); $fetch_user_id = $db->fetch_array($get_user_id); $user_id_tag = $fetch_user_id['userid']; $get_user_name = $db->query("SELECT * FROM users WHERE id='$user_id_tag'"); $fetch_user_name = $db->fetch_array($get_user_name); $logged_user = $fetch_user_name["user"]; return $logged_user; } public function check_login() { $id = session_id (); $start = $db->query("SELECT * FROM session WHERE session_id='$id'" ); $start_n = $db->num_rows( $start ); if ($start_n == "0") { return "0"; } else { $get_session_a = $db->fetch_array( $start ); $user_id = $get_session_a ["userid"]; $check_user = $db->query("SELECT * FROM users WHERE id='$user_id'" ); $c_u_n = $db->num_rows( $check_user ); if ($c_u_n == "0") { return "0"; } else { $timeout_query = $db->query("SELECT * FROM settings"); $timeout_array = $db->fetch_array($timeout_query); $timeout = $timeout_array['timeout']; $now = time (); $last = $get_session_a ["date"]; $check = $now - $last; if ($check > $timeout) { return "0"; } else { $db->query("UPDATE session SET date='$now' WHERE session_id='$id'" ); return "1"; } } } } public function __destruct() { } } if (! isset ( $user )) { $user = new user ( ); } ?> database.php: <?php /** * Database core. */ class database { private $server; private $username; private $password; private $database; /** * Construct */ function __construct() { } /** * Check, and begin connection. */ public function connect($server, $username, $password, $database) { if ($server == NULL || $username == NULL || $database == NULL) { //TODO: error. } else { $this->server = $server; $this->username = $username; $this->password = $password; $this->database = $database; $this->database_connect (); } } /** * Database error. */ public function error($text) { echo "There has been an error. Please try again later. <br /><br /> " . $text; exit (); } /** * Begin connection. */ private function database_connect() { $conn = @mysql_connect ( $this->server, $this->username, $this->password ); if ($conn == false) { $this->error ( mysql_error () ); } else { $this->select_db (); } } /** * Select database. */ private function select_db() { $select_db = @mysql_select_db ( $this->database ); if ($select_db == false) { $this->error ( mysql_error () ); } } /** * Database query */ public function query($query_text) { $query = mysql_query ( $query_text ) or die ( mysql_error () ); return $query; } /** * Database fetch array */ public function fetch_array($query_object) { $fetch = mysql_fetch_array ( $query_object ); return $fetch; } /** * Database number rows */ public function num_rows($query_object) { $num = mysql_num_rows ( $query_object ); return $num; } /** * Destruct */ function __destruct() { unset ( $this->server ); unset ( $this->username ); unset ( $this->password ); unset ( $this->database ); mysql_close (); } } if (! isset ( $db )) { $db = new database ( ); } ?> index.php: (errors will be at top, as its erroring the config part. toponly: <?php session_start (); ini_set('display_errors', 1); error_reporting(E_ALL); include "config.php"; if($user->check_login() == "1") { header("Location: index2.php"); exit(); } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted March 29, 2008 Share Posted March 29, 2008 you have a function using $db, but $db has not been defined inside that function. <?php public function check_login() { $id = session_id (); $start = $db->query("SELECT * FROM session WHERE session_id='$id'" ); ... } I suggest you read up on "variable scope" in the PHP manual Quote Link to comment Share on other sites More sharing options...
rofl90 Posted March 29, 2008 Author Share Posted March 29, 2008 but the function is being called after db is defined from config.php Quote Link to comment Share on other sites More sharing options...
Barand Posted March 29, 2008 Share Posted March 29, 2008 I suggest you read up on "variable scope" in the PHP manual Quote Link to comment Share on other sites More sharing options...
rofl90 Posted March 29, 2008 Author Share Posted March 29, 2008 I scanned through it, should I have to in user.php global $db; will global retain it's value from database.php Quote Link to comment Share on other sites More sharing options...
rofl90 Posted March 29, 2008 Author Share Posted March 29, 2008 Would I put the global in config.php or the global in user.php or database.php? Quote Link to comment Share on other sites More sharing options...
Barand Posted March 29, 2008 Share Posted March 29, 2008 try <?php session_start (); ini_set('display_errors', 1); error_reporting(E_ALL); include "config.php"; if($user->check_login($db) == "1") { // pass db to function header("Location: index2.php"); exit(); } ?> and <?php public function check_login($db) { $id = session_id (); $start = $db->query("SELECT * FROM session WHERE session_id='$id'" ); ... } Quote Link to comment Share on other sites More sharing options...
rofl90 Posted March 29, 2008 Author Share Posted March 29, 2008 same errors, if i just use global $db; in user will it work etc etc Quote Link to comment Share on other sites More sharing options...
rofl90 Posted March 29, 2008 Author Share Posted March 29, 2008 I tried the global it worked to an extent it's now bombarded me with errors: Warning: Missing argument 1 for user::check_login(), called in /home/fhlinux160/p/prowebdesigns.co.uk/user/htdocs/backend/index.php on line 64 and defined in /home/fhlinux160/p/prowebdesigns.co.uk/user/htdocs/backend/classes/user.php on line 40 Notice: Undefined variable: db in /home/fhlinux160/p/prowebdesigns.co.uk/user/htdocs/backend/classes/user.php on line 42 Fatal error: Call to a member function query() on a non-object in /home/fhlinux160/p/prowebdesigns.co.uk/user/htdocs/backend/classes/user.php on line 42 Quote Link to comment Share on other sites More sharing options...
Barand Posted March 29, 2008 Share Posted March 29, 2008 As you are using OOP, I'd forget that the global keyword exists. Quote Link to comment Share on other sites More sharing options...
rofl90 Posted March 29, 2008 Author Share Posted March 29, 2008 Lol, fair enough, I'll try some stuff. Quote Link to comment Share on other sites More sharing options...
rofl90 Posted March 29, 2008 Author Share Posted March 29, 2008 Instead of using $db-> in the user.php I extended it to database.php and used $this-> YAY! =] thanks Baarand Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.