Library creation problem! |
[eluser]Joseph1982[/eluser]
Hi, I created a new library and added to the path: system/application/libraries/Site_authentication.php The Library code is: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Then in the system/application/config/autoload.php, I have loaded this library by: Code: $autoload['libraries'] = array('Site_authentication', 'session'); Then I call the function form the Controller: Code: function index() But it returns the following error: ----------------------------------------------------------- A PHP Error was encountered Severity: Notice Message: Undefined property: Mysubscription::$Site_authentication Filename: controllers/mysubscription.php Line Number: 28 ----------------------------------------------------------- Fatal error: Call to a member function validate_user() on a non-object in http://sitename.com/system/application/c...iption.php on line 28 ----------------------------------------------------------- Can you please help me to fix this ? Thank You..
[eluser]Weblizard[/eluser]
You must use lower case when accessing your class So you should change controller code to this Code: $this->site_authentication->validate_user();
[eluser]Joseph1982[/eluser]
Thank you for your reply.. I checked by changing its name and its worked..
[eluser]Joseph1982[/eluser]
Now it returns another error: ****************************************** A PHP Error was encountered Severity: Notice Message: Undefined property: User_authentication::$session Filename: libraries/User_authentication.php Line Number: 32 Fatal error: Call to a member function userdata() on a non-object in /system/application/libraries/User_authentication.php on line 32 On Line 32, I added a Session checking and is below: Code: if(!$this->session->userdata('logged_in')) Actually I want to redirect to the home page, if the user doesn't logged in.. For that I checked inside the Session..
[eluser]Weblizard[/eluser]
You can't use $this (CodeIgniter's native resources) under your library instead you need to first assign the CodeIgniter super object to a variable and then use that variable Please add this to your method Code: $CI =& get_instance(); now you have access to $CI so you can call userdate() method this way Code: if(!$CI->session->userdata('logged_in')) You may read User guide for better understanding about coding your library
[eluser]Joseph1982[/eluser]
Thank you again. I tried that way but it doesn't work. But I tried another method as you mentioned and that works fine. Now the library is like this: Code: class User_authentication
[eluser]Weblizard[/eluser]
That was suggested by xwero, anyway my suggestion was Code: class User_authentication
[eluser]Joseph1982[/eluser]
I tried your method also and it works. Thank you for all your support & suggestions. |
Welcome Guest, Not a member yet? Register Sign In |