eli312 Posted February 22, 2010 Share Posted February 22, 2010 Hi, i am getting an error that seems to be happening when i use the include function. so my index file at the top includes a file that links to all the classes. require_once("includes/start.php"); now the rest of the page is mainly html/css and it all works fine. if i then put in the code for the login system which calls upon the session to check if logged in. (at the top just bellow the require_once start.php) if(!$session->is_logged_in()) { /* do somthing */ ;} else { /* do somthing */ } if (isset($_POST['login'])) { // Form has been submitted. $email = trim($_POST['email']); $password = trim($_POST['password']); // Check database to see if username/password exist. $found_user = Member::authenticate($email, $password); if ($found_user) { $session->login($found_user); $_SESSION['message'] = "<div class=\"GreenMessage\"> You are now Logged in. </div>"; } else { // username/password combo was not found in the database $_SESSION['message'] = "<div class=\"RedMessage\"> Error. Email/password could not be found. </div>"; } } else { // Form has not been submitted. $username = ""; $password = ""; } then the next line of code is my big problem. <?php if($session->is_logged_in()) { echo "<a href=\"#\">Logout</a></div>" ;} else { echo "<a href=\"javascript:showhide('login')\">Login</a> | <a href=\"#\">SignUp</a></div>"; }?> So basically if logged in it changes to log out. And again this all works fine until i put this code about into a file and include it onto the index. When i then get this error: Notice: Undefined variable: session in /Users/Eli/Sites/MusicSite/layout/header2.php on line 60 Fatal error: Call to a member function is_logged_in() on a non-object in /Users/Eli/Sites/MusicSite/layout/header2.php on line 60 And that is my problem once in the include it can not find the variable. 1. i have tried putting the require_once("includes/start.php"); at the top but that does not do anything. 2. i am thinking it might be an ordering problem as in the order all the code is run but i can seem to work out how it done. Also just for your information inside the start.php is this: defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR); defined('SITE_ROOT') ? null : define('SITE_ROOT', DS. 'Users' .DS. 'Eli' .DS. 'Sites' .DS. 'MusicSite'); defined('SITE') ? null : define('SITE', DS. '~eli' .DS. 'MusicSite'); defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes'); defined('SITE_TITLE') ? null : define('SITE_TITLE', 'MusicSite.com'); require_once(LIB_PATH.DS."config.php"); require_once(LIB_PATH.DS."functions.php"); require_once(LIB_PATH.DS."database.php"); require_once(LIB_PATH.DS."member.php"); require_once(LIB_PATH.DS."session.php"); Thank you for your time and help. Eli Quote Link to comment https://forums.phpfreaks.com/topic/192991-variable-error/ Share on other sites More sharing options...
sader Posted February 22, 2010 Share Posted February 22, 2010 at the top of your page add session_start(); Quote Link to comment https://forums.phpfreaks.com/topic/192991-variable-error/#findComment-1016372 Share on other sites More sharing options...
eli312 Posted February 22, 2010 Author Share Posted February 22, 2010 at the top of your page add session_start(); Thank you for your reply but the session_start(); is in the class and it all works fine if as long as i put the code all in index. but if i include that code from another file into the index it seems to forget where the session class is... Quote Link to comment https://forums.phpfreaks.com/topic/192991-variable-error/#findComment-1016377 Share on other sites More sharing options...
eli312 Posted February 23, 2010 Author Share Posted February 23, 2010 :BUMP Ok, So i thought i would make it easier to understand. I have 5 files: -initalize.php -functions.php -test.php -session.php -index.php Inside initalize.php defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR); defined('SITE_ROOT') ? null : define('SITE_ROOT', DS. 'Users' .DS. 'Eli' .DS. 'Sites' .DS. 'MusicSite'); defined('SITE') ? null : define('SITE', DS. '~eli' .DS. 'MusicSite'); defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes'); defined('SITE_TITLE') ? null : define('SITE_TITLE', 'MySite.com'); require_once(LIB_PATH.DS."config.php"); require_once(LIB_PATH.DS."functions.php"); require_once(LIB_PATH.DS."database.php"); require_once(LIB_PATH.DS."member.php"); require_once(LIB_PATH.DS."session.php"); Now all this does is defines the site directory and requires all the classes. Inside functions.php // Includes the layout pages function site_layout($template="") { include(SITE_ROOT.DS.'layout'.DS.$template.".php"); } There is much more in there but that's the only one important to my example Inside test.php if($session->is_logged_in()) {echo "You are logged in<br/>";}else{echo "You are not logged in<br/>";} This is the code that is the problem it is meant to look in the session class and check if the user is logged in or not. Inside session.php class Session { private $logged_in=false; public $user_id; function __construct() { session_start(); $this->check_login(); if($this->logged_in) { } else { } } public function is_logged_in() { return $this->logged_in; } public function login($user) { // database should find user based on username/password if($user){ $this->user_id = $_SESSION['user_id'] = $user->id; $this->logged_in = true; } } public function logout() { unset($_SESSION['user_id']); unset($this->user_id); $this->logged_in = false; } private function check_login() { if(isset($_SESSION['user_id'])) { $this->user_id = $_SESSION['user_id']; $this->logged_in = true; } else { unset($this->user_id); $this->logged_in = false; } } } $session = new Session(); $s =& $session; This is all the code to check if logged in, to login, and to logout. Inside index.php require_once("includes/initalize.php"); include("layout/test.php"); site_layout("test"); So it requires initalize.php and includes test.php and then calls function "test". So this is my errors when i go on to index.php index.php You are not logged in Notice: Undefined variable: session in /Users/Eli/Sites/MusicSite/layout/test.php on line 3 Fatal error: Call to a member function is_logged_in() on a non-object in /Users/Eli/Sites/MusicSite/layout/test.php on line 3 So as you can see when just including the file it works fine. but if i use the function to call it and run the code i get an error. So what is the problem? Sorry for long message have a lot to say Thanks to anyone that can help. Eli Quote Link to comment https://forums.phpfreaks.com/topic/192991-variable-error/#findComment-1016918 Share on other sites More sharing options...
mikesta707 Posted February 23, 2010 Share Posted February 23, 2010 read the error message. the variable $session isn't an object (and you seem to be using it as if it were) can you post the rest of the code on text.php Quote Link to comment https://forums.phpfreaks.com/topic/192991-variable-error/#findComment-1016922 Share on other sites More sharing options...
eli312 Posted February 23, 2010 Author Share Posted February 23, 2010 That's the thing it is. if i post the raw code: <?php require_once("includes/initalize.php"); ?> <?php if($session->is_logged_in()) {echo "You are logged in<br/>";}else{echo "You are not logged in<br/>";}?> <?php include("layout/test.php");?> <?php site_layout("test");?> Into index it works fine and i now get: You are not logged in You are not logged in Notice: Undefined variable: session in /Users/Eli/Sites/MusicSite/layout/test.php on line 2 Fatal error: Call to a member function is_logged_in() on a non-object in /Users/Eli/Sites/MusicSite/layout/test.php on line 2 Meaning to me it seems to be an error with the function but the function it self works as in if i put. echo "hello world"; inside test.php and use the function site_layout("test"); to call it. It will echo hello world. So the problem is that is loses the variable and i dont understand why Quote Link to comment https://forums.phpfreaks.com/topic/192991-variable-error/#findComment-1016930 Share on other sites More sharing options...
eli312 Posted February 23, 2010 Author Share Posted February 23, 2010 Problem has been solved in another topic. The problem was Variable scope and all it need was the function to call global. // Includes the layout pages function site_layout($template="") { global $session; include(SITE_ROOT.DS.'layout'.DS.$template.".php"); } Quote Link to comment https://forums.phpfreaks.com/topic/192991-variable-error/#findComment-1017047 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.