Welcome Guest, Not a member yet? Register   Sign In
Library creation problem!
#1

[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/c...iption.php on line 28


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


Can you please help me to fix this ?

Thank You..
#2

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

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

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

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

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

[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
#8

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

}
#9

[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
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB