Twister1004 Posted January 7, 2012 Share Posted January 7, 2012 Hey everyone! I'm starting to rewrite my code for my website and I'm understanding OOP more and more (since I had a class on it), however, I just can't seem to figure out what is going wrong with the code and my login script as to why it is not running my object, when its been declaired. Here are the required parts of the files that is required for this OOP code im working on. Login Function (Not writen in OOP yet) File: functions.php function userLogin($username, $pass){ $checkUser = mysql_query("SELECT * FROM `accounts` WHERE `username` = '".$username."' LIMIT 1"); if(mysql_num_rows($checkUser) == 1){ while($User = mysql_fetch_array($checkUser)){ if(hash('sha512', $pass) == $User['password']){ if(!@$_SESSION['id']){ //More Dynamic way other than SESSIONS are needed for updated accounts //Add security to banned accounts $_SESSION['id'] = $User['user_id']; $_SESSION['user'] = $User['username']; $_SESSION['permission'] = $User['clearence']; $_SESSION['isArtist'] = $User['isArtist']; $_SESSION['artistType'] = $User['artistType']; $_SESSION['lastlogin'] = $User['lastlogin']; $_SESSION['loginTime'] = time(); $_SESSION['artistTypeName'] = artistType($_SESSION['artistType']); $updateIP = mysql_query("UPDATE `accouns` SET `lastip` = '".$_SERVER['REMOTE_ADDR']."' WHERE `user_id` = '".$_SESSION['id']."'"); //This is a SQL error by the wrong table and it is purposefully done for testing my OOP Code. $updateLogin = mysql_query("UPDATE `accounts` SET `lastlogin` = '".time()."' WHERE `user_id` = '".$_SESSION['id']."' LIMIT 1"); if(!$updateIP){ $reports->Error("Failure to update IP in login: ".mysql_error(), $_SERVER['REQUEST_URI']); // ~~~~~~~~~~~~~~~~~ THIS IS LINE 24 ~~~~~~~~~~~~~~~~~ logout(0); die(); } if(!$updateLogin){ sendError(mysql_error(), $_SERVER['REQUEST_URI']); logout(0); } echo "<meta http-equiv=\"refresh\" content=0; url=\"\"/>"; } else break; } else{ echo "<font color='red'>Username or Password is incorrect.</font>"; } } } else{ echo "<font color='red'>Username or Password is incorrect.</font>"; } } File: includes.php include("./inc/classes/reports.php"); include("./inc/database.php"); include("./inc/functions.php"); include("./inc/artclass.php"); //include("./inc/classes/loginsystem.php"); //$login = new Login(time()); $reports = new Reports(); File:Reports.php <?php class Reports{ public function Error($error, $page){ $error = mysql_real_escape_string($error); $page = mysql_real_escape_string($page); $sendError = mysql_query("INSERT INTO `errors` (errorCode, page, date) VALUES ('".$error."', '".$page."', '".time()."')"); //date("F m, Y -- h:i:s") if(!$sendError){ $this->Message(0); } else{ $this->Message(2); } } public function Report(){ } public function Query($Table, $Comment, $Page, $Date){ // NOT inplemented } private function Message($number){ switch($number){ case 0: echo "There was an error reporting an issue. Please alert us by <a href='contact.php'>contacting us</a>."; break; case 1: echo "There was an error sending a report. Please use the <a href='contact.php'>contact us</a> and let us know!"; break; case 2: echo "There was an error. Please wait about 30 minutes to retry your request.<br/><br/> We apologize for the inconvience."; break; } } } ?> The Error: Notice: Undefined variable: reports in functions.php on line 23 Fatal error: Call to a member function Error() on a non-object in functions.php on line 23 Any help would be very much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/254561-oop-error/ Share on other sites More sharing options...
trq Posted January 7, 2012 Share Posted January 7, 2012 Your error has nothing to do with the fact that your code might be OOP. By the way, just because your using classes does not make your code OOP either. Anyway, $reports isn't defined within your userLogin() function. Quote Link to comment https://forums.phpfreaks.com/topic/254561-oop-error/#findComment-1305356 Share on other sites More sharing options...
Twister1004 Posted January 7, 2012 Author Share Posted January 7, 2012 I understand that OOP isnt the problem, I'm still learning how to use it correctly. However, it is still declaired inside the main file that is included. So isn't still declaired? Quote Link to comment https://forums.phpfreaks.com/topic/254561-oop-error/#findComment-1305361 Share on other sites More sharing options...
trq Posted January 7, 2012 Share Posted January 7, 2012 Functions have there own scope. Variables declared outside of them do not exist within them and vice versa. You will need to pass the variable in as an argument. Quote Link to comment https://forums.phpfreaks.com/topic/254561-oop-error/#findComment-1305362 Share on other sites More sharing options...
Twister1004 Posted January 7, 2012 Author Share Posted January 7, 2012 Ahh ok! That makes sense! Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/254561-oop-error/#findComment-1305381 Share on other sites More sharing options...
Andy-H Posted January 8, 2012 Share Posted January 8, 2012 Unrelated, but I think you'll find autoloading useful Quote Link to comment https://forums.phpfreaks.com/topic/254561-oop-error/#findComment-1305427 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.