CodeIgniter Forums
Library creation problem! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Library creation problem! (/showthread.php?tid=11637)

Pages: 1 2


Library creation problem! - El Forum - 09-17-2008

[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');

/**
* Code Igniter AJAX Class
*
* This class enables you to check User Authentication with the help of Session.
*
* @package        CodeIgniter
* @subpackage    Libraries
* @category    Libraries
* @author        Aniesh
* @link        http://www.no-link-yet.com
*/


class Site_authentication
{

    function Site_authentication()
    {
        $this->obj =& get_instance();
    }

    function validate_user()
    {
        // Not logged in, then redirect to the Home Page.
        if(!$this->session->userdata('logged_in'))
            redirect(site_url(), 'refresh');        
    }


}

?>


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()
    {
    
        $this->Site_authentication->validate_user();

        $data['mysubscriptions']    =    $this->Mysubscription_model->mysubscriptions();

        $this->load->view('mysubscription_view',$data);
    }

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/controllers/mysubscription.php on line 28


-----------------------------------------------------------


Can you please help me to fix this ?

Thank You..


Library creation problem! - El Forum - 09-17-2008

[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();



Library creation problem! - El Forum - 09-17-2008

[eluser]Joseph1982[/eluser]
Thank you for your reply.. I checked by changing its name and its worked..


Library creation problem! - El Forum - 09-17-2008

[eluser]Joseph1982[/eluser]
How can I mark this problem is solved ?


Library creation problem! - El Forum - 09-17-2008

[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..


Library creation problem! - El Forum - 09-17-2008

[eluser]xwero[/eluser]
Code:
if(!$this->obj->session->userdata('logged_in'))



Library creation problem! - El Forum - 09-17-2008

[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


Library creation problem! - El Forum - 09-17-2008

[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
{

    function User_authentication()
    {
        $this->obj =& get_instance();
    }

    function display()
    {

        // Not logged in, then redirect to the Home Page.
        if(!$this->obj->session->userdata('logged_in'))
            redirect('', 'refresh');        
    }

}



Library creation problem! - El Forum - 09-17-2008

[eluser]Weblizard[/eluser]
That was suggested by xwero,
anyway my suggestion was
Code:
class User_authentication
{

    function User_authentication()
    {
        
    }

    function display()
    {

$CI =& get_instance();        
// Not logged in, then redirect to the Home Page.
        if(!$CI->session->userdata('logged_in'))
            redirect('', 'refresh');        
    }

}
which is work better with php4


Library creation problem! - El Forum - 09-17-2008

[eluser]Joseph1982[/eluser]
I tried your method also and it works. Thank you for all your support & suggestions.