blueman378 Posted November 26, 2008 Share Posted November 26, 2008 Hi guys, Well im rewriting the login script that most people would know (PHP Login System with Admin Features | evolt.org) because there are things i dont need/ do need that it doesnt have) anyway im getting this error: Fatal error: Call to a member function setError() on a non-object in C:\wamp\www\login\include\session.php on line 257 and yet heres the code so far form.php <?php //--------------------- //FORM.PHP //--------------------- //this file is designed to control error reporting on forms. //it is advised that you do not modify this file as you will stop the site working. //--------------------- //--------------------- //AUTHOR DETAILS //--------------------- //AUTHOR: NZ Evox //AUTHOR EMAIL: [email protected] //SUPPORT: http://www.baf.com //FORUM THEMES: http://www.baf.com/themes/ //FORUM MODS: http://www.baf.com/mods/ //--------------------- //--------------------- //SCRIPT DETAILS //--------------------- //MAJOR: 001 //MINOR: .0.0.0 //COMPLETE: 1.0.0.0 //LAST UPDATE: November 2008 //--------------------- class Form { var $values = array(); var $errors = array(); var $num_errors; /* Class constructor */ function Form(){ if(isset($_SESSION['value_array']) && isset($_SESSION['error_array'])){ $this->values = $_SESSION['value_array']; $this->errors = $_SESSION['error_array']; $this->num_errors = count($this->errors); unset($_SESSION['value_array']); unset($_SESSION['error_array']); } else{ $this->num_errors = 0; } } function setValue($field, $value){ $this->values[$field] = $value; } function setError($field, $errmsg){ $this->errors[$field] = $errmsg; $this->num_errors = count($this->errors); } function value($field){ if(array_key_exists($field,$this->values)){ return htmlspecialchars(stripslashes($this->values[$field])); }else{ return ""; } } function error($field){ if(array_key_exists($field,$this->errors)){ return "<font size=\"2\" color=\"#ff0000\">".$this->errors[$field]."</font>"; }else{ return ""; } } function getErrorArray(){ return $this->errors; } }; ?> [code] [b]session.php[/b] [code]<?php //--------------------- //SESSION.PHP //--------------------- //this file is designed to control user interaction with the site. //it is advised that you do not modify this file as you will stop the site working. //--------------------- //--------------------- //AUTHOR DETAILS //--------------------- //AUTHOR: NZ Evox //AUTHOR EMAIL: [email protected] //SUPPORT: http://www.baf.com //FORUM THEMES: http://www.baf.com/themes/ //FORUM MODS: http://www.baf.com/mods/ //--------------------- //--------------------- //SCRIPT DETAILS //--------------------- //MAJOR: 001 //MINOR: .0.0.0 //COMPLETE: 1.0.0.0 //LAST UPDATE: November 2008 //--------------------- //--------------------- //INCLUDES //--------------------- //these are the other scripts needed to run the site //--------------------- include("database.php"); include("form.php"); //--------------------- class session { var $username; //Username given on sign-up var $userid; //Userid stored in the database var $userlevel; //The level to which the user pertains var $time; //Time user was last active (page loaded) var $logged_in; //True if user is logged in, false otherwise var $userinfo = array(); //The array holding all user info var $url; //The page url current being viewed var $referrer; //Last recorded site page viewed function session() { $this->sessionstart(); } function sessionstart() { session_start(); global $database; $this->logged_in = $this->checkLogin(); if(!$this->loggedin) { $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time); } else { $database->addActiveUser($this->userid, $this->time); } } function checkLogin() { global $database; if(isset($_SESSION['username']) && $_SESSION['username'] != "Guest") { $this->userinfo = $database->getUserInfo($_SESSION['username']); $this->username = $this->userinfo['username']; $this->userid = $this->userinfo['userid']; $this->userlevel = $this->userinfo['userlevel']; return true; } else { return false; } } function register($subuser, $subpass, $subemail, $subbirthday){ global $database; //--------------------- //USERNAME ERROR CHECKING //--------------------- $field = "username"; //--------------------- //CHECCK IF USERNAME IS EMPTY //--------------------- if(!$subuser || strlen($subuser = trim($subuser)) == 0){ $form->setError($field, "* Username not entered"); } else{ //--------------------- //CHECK LENGTH //--------------------- $subuser = stripslashes($subuser); if(strlen($subuser) < 5){ $form->setError($field, "* Username below 5 characters"); } else if(strlen($subuser) > 30){ $form->setError($field, "* Username above 30 characters"); } //--------------------- //CHECK IF USERNAME IS ALPHANUMERIC //--------------------- else if(!eregi("^([0-9a-z])+$", $subuser)){ $form->setError($field, "* Username not alphanumeric"); } //--------------------- //CHECK IF USERNAME IS RESERVED //--------------------- else if(strcasecmp($subuser, GUEST_NAME) == 0){ $form->setError($field, "* Username reserved word"); } //--------------------- //CHECK IF USERNAME IS ALREADY IN USE //--------------------- else if($database->usernameTaken($subuser)){ $form->setError($field, "* Username already in use"); } //--------------------- //CHECK IF USERNAME IS BANNED else if($database->usernameBanned($subuser)){ $form->setError($field, "* Username banned"); } } //--------------------- //BIRTHDAY ERROR CHECKING //--------------------- $field = "birthday"; //--------------------- //CHECK IF BURTHDAY IS EMPTY //--------------------- if(!$subbirthday || strlen($subbirthday = trim($subbirthday)) == 0){ $form->setError($field, "* Birthday not entered"); } //--------------------- //CHECK BIRTHDAY FORMAT //--------------------- elseif (!preg_match('/^[0-9]{2}-[0-9]{2}-[0-9]{4}$/', $subbirthday)) { $field = "birthday"; $form->setError($field, "* Invalid Birthday format must be \"11-11-" .Date(Y). "\""); } //--------------------- //PASSWORD ERROR CHECKING //--------------------- $field = "pass"; //--------------------- //CHECK IF PASSWORD IS EMPTY //--------------------- if(!$subpass){ $form->setError($field, "* Password not entered"); } //--------------------- //CHECK PASSWORD LENGTH //--------------------- else{ $subpass = stripslashes($subpass); if(strlen($subpass) < 4){ $form->setError($field, "* Password too short"); } //--------------------- //CHECK IF PASSWORD IS ALPHANUMERIC //--------------------- else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){ $form->setError($field, "* Password not alphanumeric"); } } //--------------------- //EMAIL ERROR CHECKING //--------------------- $field = "email"; //--------------------- //CHECK IF EMAIL IS EMPTY //--------------------- if(!$subemail || strlen($subemail = trim($subemail)) == 0){ $form->setError($field, "* Email not entered"); } //--------------------- //CHECK EMAIL FORMAT //--------------------- else{ $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*" ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*" ."\.([a-z]{2,}){1}$"; if(!eregi($regex,$subemail)){ $form->setError($field, "* Email invalid"); } $subemail = stripslashes($subemail); } //--------------------- //ERRORS EXIST GET USER TO CORRECT THEM //--------------------- if($form->num_errors > 0){ return 1; } //--------------------- //NO ERRORS ADD NEW USER //--------------------- else{ if($database->addNewUser($subuser, md5($subpass), $subemail, $subbirthday)){ return 0; //New user added succesfully }else{ return 2; //Registration attempt failed } } } function login($subuser, $subpass, $subremember, $subhidden){ global $database; //--------------------- //USERNAME ERROR CHECKING //--------------------- $field = "username"; //--------------------- //CHECK IF USERNAME IS EMPTY //--------------------- if(!$subuser || strlen($subuser = trim($subuser)) == 0){ $form->setError($field, "* Username not entered"); } else{ //--------------------- //CHECK LENGTH //--------------------- $subuser = stripslashes($subuser); if(strlen($subuser) < 5){ $form->setError($field, "* Username below 5 characters"); } else if(strlen($subuser) > 30){ $form->setError($field, "* Username above 30 characters"); } //--------------------- //CHECK IF USERNAME IS ALPHANUMERIC +_ //--------------------- else if(!eregi("^([0-9a-z_])+$", $subuser)){ $form->setError($field, "* Username invalid"); } } //--------------------- //PASSWORD ERROR CHECKING //--------------------- $field = "password"; //--------------------- //CHECK IF USERNAME IS EMPTY //--------------------- if(!$subuser || strlen($subuser = trim($subuser)) == 0){ $form->setError($field, "* Username not entered"); } else{ //--------------------- //CHECK LENGTH //--------------------- $subuser = stripslashes($subuser); if(strlen($subuser) < 5){ $form->setError($field, "* Username below 5 characters"); } else if(strlen($subuser) > 30){ $form->setError($field, "* Username above 30 characters"); } //--------------------- //CHECK IF USERNAME IS ALPHANUMERIC +_ //--------------------- else if(!eregi("^([0-9a-z_])+$", $subuser)){ $form->setError($field, "* Username invalid"); } } //--------------------- //ERRORS EXIST GET USER TO CORRECT THEM //--------------------- if($form->num_errors > 0){ return 1; } //--------------------- //NO ERRORS ADD NEW USER //--------------------- else{ if($database->login($subuser, md5($subpass), $subremember, $subhidden)){ return 0; //user logged in }else{ return 2; //failed } } } } //--------------------- //INITIALIZE THE CLASSES //--------------------- $session = new session; $form = new Form; //--------------------- ?> so form.php is being included and it is being initialized yet i still get that error ps line:257 is $form->setError($field, "* Username invalid"); im getting that when im intentionally entering an invalid username. so any ideas? Link to comment https://forums.phpfreaks.com/topic/134419-solved-non-object-actually-exists/ Share on other sites More sharing options...
blueman378 Posted November 26, 2008 Author Share Posted November 26, 2008 never mind, got it, forgot to declare the form class as global. Link to comment https://forums.phpfreaks.com/topic/134419-solved-non-object-actually-exists/#findComment-699810 Share on other sites More sharing options...
trq Posted November 26, 2008 Share Posted November 26, 2008 Don't use globals. You may as well not be using objects at all. Link to comment https://forums.phpfreaks.com/topic/134419-solved-non-object-actually-exists/#findComment-699811 Share on other sites More sharing options...
blueman378 Posted November 27, 2008 Author Share Posted November 27, 2008 if im not using globals how would i access the other classes from within say the session class? Link to comment https://forums.phpfreaks.com/topic/134419-solved-non-object-actually-exists/#findComment-700258 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 You would instantiate them within the class you need them. Link to comment https://forums.phpfreaks.com/topic/134419-solved-non-object-actually-exists/#findComment-700311 Share on other sites More sharing options...
blueman378 Posted November 27, 2008 Author Share Posted November 27, 2008 hmm i must be mistaken, i thought that if you re-instantiate it starts a new instance rather than continuing the old one? Link to comment https://forums.phpfreaks.com/topic/134419-solved-non-object-actually-exists/#findComment-700329 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 hmm i must be mistaken, i thought that if you re-instantiate it starts a new instance rather than continuing the old one? That is correct. If you want only one instance of an object to exist however you should make it a singleton. Link to comment https://forums.phpfreaks.com/topic/134419-solved-non-object-actually-exists/#findComment-700398 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.