Welcome Guest, Not a member yet? Register   Sign In
How on earth do you use a custom function in this???!!!! [SOLVED]
#1

[eluser]Emkay[/eluser]
All I want to do is store a function in a library and then call it whenever I want. Seems simple enough, but CI is making it a nightmare trying to get it to work. This is what I have done.

- Created a new file called "Standard.php" in the libraries folder
- Created a class opener inside this file, also called "Standard"
- Added my custom function, called "authorize"
- Configured the autoload.php file in the config folder to auto load this library
- Tried to call the function from one of my controllers using:

Code:
$this->standard->authorize(2);

Yet I get this error:

Code:
Fatal error: Call to a member function userdata() on a non-object in C:\wamp\www\site\libraries\Standard.php on line 15

What on earth am I doing wrong? How can such a simple procedure not work?! I'm tearing my hair out over this! Help me please!

This is my Standard.php library file.

Code:
<?php

class Standard {

    function authorize($types = NULL)    {
        
        if($this->session->userdata('active') != 1) {
            $message = "You need to login to access this page.";
            $this->session->set_flashdata('message',$message);
            $this->session->set_flashdata('message-type','error');
            redirect('login');
        }
        
        if($types != NULL)  {
            $user_type = $this->session->userdata('user_type');
            if(!in_array($user_type, $types))  {
                $message = "You are not authorized to access this page.";
                $this->session->set_flashdata('message',$message);
                $this->session->set_flashdata('message-type','error');
                redirect('home');
            }
        }
    }

}

?>
#2

[eluser]Andreas Karlsson[/eluser]
You need to load the session library.

$this->session is not defined so you need to load it before you do $this->session->userdata();

http://ellislab.com/codeigniter/user-gui...oader.html
#3

[eluser]Aken[/eluser]
You also need to create a variable to use the CodeIgniter super object in your class. When using Libraries, $this will always refer to the library class, NOT the CI super object (in your case, $this will be the Standard class).

This is what I would recommend doing:

Step 1) Add the Session library to your autoload (you want sessions on every page, so might as well load it by default).

Step 2) Change your library code to the following:
Code:
<?php

class Standard {

    private $CI;

    function __construct()
    {
        // $this->CI becomes the CodeIgniter super object reference.
        $this->CI =& get_instance();
    }

    function authorize($types = NULL)
    {
        if ($this->CI->session->userdata('active') != 1) {
            $message = "You need to login to access this page.";
            $this->CI->session->set_flashdata('message',$message);
            $this->CI->session->set_flashdata('message-type','error');
            redirect('login');
        }
        
        if ($types != NULL)  {
            $user_type = $this->CI->session->userdata('user_type');
            if(!in_array($user_type, $types))  {
                $message = "You are not authorized to access this page.";
                $this->CI->session->set_flashdata('message',$message);
                $this->CI->session->set_flashdata('message-type','error');
                redirect('home');
            }
        }
    }

}

?>
#4

[eluser]Emkay[/eluser]
Thank you both a lot!!! Appreciate it Smile




Theme © iAndrew 2016 - Forum software by © MyBB