bem Posted May 14, 2014 Share Posted May 14, 2014 Hi , Followed the chapter fully to test it all out and works well, but when no one is logged in and goes to the add_page.php file a fatal error occurs. Is there a way to correct this when someone isn't logged in? Fatal error: Call to a member function canCreatePage() on a non-object in directory\c09\add_page.php on line 7 Link to comment Share on other sites More sharing options...
HartleySan Posted May 14, 2014 Share Posted May 14, 2014 Hello and welcome to the forums, bem. Could you please provide us with the relevant code so that we can better assist you? Thanks. Link to comment Share on other sites More sharing options...
bem Posted May 15, 2014 Author Share Posted May 15, 2014 Same as the book for this part to be honest. Been testing it out before I go adding in my own stuff. Works fine when logged in as the Public account and I go to add_page.php I'm redirected to the index page. But if no one is logged in at all and I go to the page I get the error I mentioned above. So my add_page.php has: // Redirect if the user doesn't have permission: if (!$user->canCreatePage()) { header("Location:index.php"); exit; } classes/User.php: class User { // All attributes correspond to database columns. // All attributes are protected. protected $id = null; protected $userType = null; protected $username = null; protected $email = null; protected $pass = null; protected $dateAdded = null; // Method returns the user ID: function getId() { return $this->id; } // Method returns a Boolean if the user is an administrator: function isAdmin() { return ($this->userType == 'admin'); } // Method returns a Boolean indicating if the user is an administrator // or if the user is the original author of the provided page: function canEditPage(Page $page) { return ($this->isAdmin() || ($this->id == $page->getCreatorId())); } // Method returns a Boolean indicating if the user is an administrator or an author: function canCreatePage() { return ($this->isAdmin() || ($this->userType == 'author')); } } Link to comment Share on other sites More sharing options...
Larry Posted May 16, 2014 Share Posted May 16, 2014 The error would suggest that $user is not a User object, which means it's not being created properly before that line. Link to comment Share on other sites More sharing options...
Recommended Posts