chriscloyd Posted November 24, 2011 Share Posted November 24, 2011 i have two classes and I am trying to call one class in through another here is my code but my welcome is not showing at all ever when i change the var's to 1 class site { function DisplayContent ($page) { global $Users; echo '<div align="center"> <div id="Header"> <div id="NavBar">'; if ($Users->logged == 1) { echo 'Welcome '. $Users->user .' - <a href="?page=viewreminders">View Reminders</a> - <a href="?page=addreminder">Add Reminder</a> - <a href="logout.php" id="Logout">Logout</a>'; } else { echo '<a href="?page=login">Login</a> - <a href="?page=register">Register</a> - <a href="?page=forgotpassword">Forgot Password?</a>'; } echo '</div> </div> <div id="Content">'; $this->ShowPage($page); echo '</div> <div id="Footer">Copyright © '.date('Y').'<br /> Coded and Designed by Chris Cloyd</div> </div>'; } } class users { var $logged; var $user; var $email; var $level; function LogIn ($name,$email,$level) { $this->logged = 1; $this->user = $name; $this->email = $email; $this->level = $level; } function LogOut () { $this->logged = "0"; $this->user = ""; $this->email = ""; $this->level = ""; } } Quote Link to comment https://forums.phpfreaks.com/topic/251711-class-global/ Share on other sites More sharing options...
teynon Posted November 24, 2011 Share Posted November 24, 2011 Print out the objects: print_r($Users); Quote Link to comment https://forums.phpfreaks.com/topic/251711-class-global/#findComment-1290892 Share on other sites More sharing options...
chriscloyd Posted November 24, 2011 Author Share Posted November 24, 2011 users Object ( [logged] => [user] => => [level] => ) Quote Link to comment https://forums.phpfreaks.com/topic/251711-class-global/#findComment-1290893 Share on other sites More sharing options...
chriscloyd Posted November 24, 2011 Author Share Posted November 24, 2011 so i know the users is working, but its not working inside the site class here go to cloydprojects.com you can login and it wont change the links email - [email protected] password - test Quote Link to comment https://forums.phpfreaks.com/topic/251711-class-global/#findComment-1290895 Share on other sites More sharing options...
chriscloyd Posted November 24, 2011 Author Share Posted November 24, 2011 Could it be that when it goes to the login page its starting a new one here my index.php <?php /** * @author Chris Cloyd * @copyright 2011 */ /** Session Start For Users */ session_start(); /** Load Config */ include("load/config.php"); /** Load Class.Site and Class.Reminder */ include("load/class.site.php"); include("load/class.reminder.php"); include("load/class.users.php"); /** Start Classes */ $Site = new site(); $Reminder = new reminder(); $Users = new users(); /** Start Html by loading $Site->DisplayHeader(); */ $Site->DisplayHeader(); echo $Users->user; /** Load Middle Content by loading $Site->DisplayContent(); */ if (isset($_GET['page'])) { $Site->DisplayContent($_GET['page']); } else { $Site->DisplayContent('login'); } /** End Html by loading $Site->DisplayFooter(); */ $Site->DisplayFooter(); ?> login.php <?php /** * @author Chris Cloyd * @copyright 2011 */ /** Session Start For Users */ session_start(); /** Load Config */ include("../load/config.php"); /** Load Class.Site and Class.Reminder */ include("../load/class.site.php"); include("../load/class.reminder.php"); include("../load/class.users.php"); /** Start Classes */ $Site = new site(); $Reminder = new reminder(); $Users = new users(); $email = mysql_escape_string($_POST['emailLogin']); $password = mysql_escape_string($_POST['passwordLogin']); $password = md5($password); $Search = mysql_query("SELECT * FROM users WHERE email = '".$email."' AND password = '".$password."' "); $Found = mysql_num_rows($Search); if ($Found > 0) { $UserInfo = mysql_fetch_assoc($Search); if ($UserInfo['status'] != 1) { $_SESSION['umadBroEmail'] = $email; header("Location: http://cloydprojects.com/index.php?page=activate"); } $Users->LogIn($UserInfo['name'],$UserInfo['email'],$UserInfo['level']); echo $Users->user; $_SESSION['umadBro'] = $UserInfo['name']; //header("Location: http://cloydprojects.com/index.php?page=home"); } else { $_SESSION['umadBroError'] = 'Incorrect User Information, Try Again!'; //header("Location: http://cloydprojects.com/index.php?page=login"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/251711-class-global/#findComment-1290897 Share on other sites More sharing options...
teynon Posted November 24, 2011 Share Posted November 24, 2011 You're not passing the object in the session. You are making a new empty user object on the index page. Quote Link to comment https://forums.phpfreaks.com/topic/251711-class-global/#findComment-1290899 Share on other sites More sharing options...
trq Posted November 24, 2011 Share Posted November 24, 2011 Do not use globals with classes. It completely breaks the encapsulation that they provide, you may as well not use classes at all. Search for some tutorials on "PHP Dependency Injection" to get some ideas on how it should be done. A very simple form of dependency injection is to simply have one object accept the other object through an argument in its __construct. eg; class a {} class b { private $a; public function __construct(a $a) { $this->a = $a; } } $b = new b(new a); Quote Link to comment https://forums.phpfreaks.com/topic/251711-class-global/#findComment-1290911 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.