Welcome Guest, Not a member yet? Register   Sign In
Cookie Helper in Application Library
#1

[eluser]wbremen[/eluser]
Hey there,

I upgraded to 2.0 and a Library which used to work in CI 1.7 now crashes my whole application.

Following code from a library located in application/librariesm does create the problem:

Code:
class Someclass {

    private $CI = "";

    function __construct()
    {

        $this->CI =& get_instance();
        $this->CI->load->helper('url', 'cookie');

    }
    
    function some_function()
    {
        if(get_cookie('cookie_name')){
            delete_cookie('cookie_name');
        }

    }
}

The following error is displayed:
Quote:Fatal error: Call to undefined function get_cookie() in [...]application\libraries\Someclass.php [...]

It seems like the Instance did not load the cookie helper but I can not find any mistakes in loading it.
#2

[eluser]InsiteFX[/eluser]
Code:
class Someclass {

    private $CI;

    function __construct()
    {

        $this->CI =& get_instance();
        $this->CI->load->helper('url', 'cookie');

    }
    
    function some_function()
    {
        // your getting an error because cookie_name does not exisit!
        // Also you never created a cookie...
        if(get_cookie('cookie_name')){
            delete_cookie('cookie_name');
        }

    }
}

InsiteFX
#3

[eluser]wbremen[/eluser]
No, just because this Library (or even extract) doesn't set any cookies doesn't mean there are no cookies! Also I am convinced that in that case the error message would not say
Quote:undefined function set_cookie
#4

[eluser]InsiteFX[/eluser]
If you keep getting that error then it sounds like the helper is not being loaded!

Also you may need to use
Code:
$this->CI->get_cookie('cookie_name'); // etc.

InsiteFX
#5

[eluser]wbremen[/eluser]
[quote author="InsiteFX" date="1298772955"]If you keep getting that error then it sounds like the helper is not being loaded![/quote]
Oh - good guess! That was only what I said in my first post
[quote author="wbremen" date="1298749450"]It seems like the Instance did not load the cookie helper but I can not find any mistakes in loading it.[/quote]
If the loader did not load the cookie helper, than someone should have a look at the documentation wether its code is correct.

Code:
$this->CI->get_cookie('cookie_name'); // etc.
Why use a helper like that, normally the helpers are not used in classes but only as single functions?
#6

[eluser]InsiteFX[/eluser]
Try loading the cookie helper in the application/config/autoload.php file and see if it works!

InsiteFX
#7

[eluser]Gonzo2[/eluser]
Well I was also facing this problem of loader, but when I put them on seperate lines it worked like charm.

eg:
$this->load->helper('url', 'cookie');
Will not work at all I dont know the reason why? url helper will be loaded and rest truncated, vice-versa for cookie helper if you put that first.

$this->load->helper('url');
$this->load->helper('cookie');

Will work like charm or the other way is to Autoload.


Even I have notice a odd thing about cookie helper. The native PHP way of setting a cookie is different than CI way.

Native PHP: setcookie[name, value, expire, path, domain, secure, httponly]
CI: set_cookie($name, $value, $expire, $domain, $path, $prefix)

There is a swap between path and domain while setting the cookie environment, path comes first in native PHP way and domain comes first the CI way, so if you are habitual of using cookie native PHP way or mix of PHP and CI then its confusing to set the environment into CI


Gonzo




Theme © iAndrew 2016 - Forum software by © MyBB