Welcome Guest, Not a member yet? Register   Sign In
Extending Input library get_instance() problem
#1

[eluser]LoRdCoStE[/eluser]
Hi, I want to extend the input library, so I make MY_Input.php under application/library

In this class I need to use session, and I want my library to be compatible with both standard session and database session.

So I need to use $ci->session but I can't get it work because I think that the function get_instance is not already there when my class is initialized so:

Code:
var ci;

...

$ci =& get_instance()

$ci->session

is not working.

How can I do that?

I need access to session (both normal and db) so no $_SESSION.

If this is absolutely impossible there are problem if I use just one session var (directly) $_SESSION['my_var'] also if database session are enable?
I mean does ci unset that var or just don't call session_start()?

Thanks for your help.
#2

[eluser]LoRdCoStE[/eluser]
up
#3

[eluser]Phil Sturgeon[/eluser]
Depends where you are putting that code within the Input library. The Input library is called very early on in the instance (before CI_Base is called and get_instance() is defined) so it will fatal error if you call in in the constructor, or any method called by the instrustor (or early CI).

Pastie up some more code.
#4

[eluser]LoRdCoStE[/eluser]
Thanks for you answer.

What I'm trying to do is an automatic CSRF library.

Here some example code (that don't work):

Code:
<?php

class MY_Input extends CI_Input
{
    function __construct()
    {                
        $this->_verify_token();
        
        parent::CI_Input();
    }
    
    private function _verify_token()
    {
        if(!empty($_POST))
        {
            $ci =& get_instance();
            
            if(!isset($_POST['csrf_token']) || empty($_POST['csrf_token']) || $this->ci->session->userdata('csrf_token') != $_POST['csrf_token'])
            {
                show_error('Il token CSRF non può essere verificato.');
            }
        }
    }
}

Right now I have solved using $_SESSION, but I would like to find a difference soulutions, maybe extending one of the input library method wher get_instance() is available.
#5

[eluser]n0xie[/eluser]
It might be easier to just extend the Form_Validation library then the Input library if you want 'standard' csrf protection. Take a look here




Theme © iAndrew 2016 - Forum software by © MyBB