CodeIgniter Forums
Newbie: Cookie-Helper is not working inside a model... why? - 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: Newbie: Cookie-Helper is not working inside a model... why? (/showthread.php?tid=22394)



Newbie: Cookie-Helper is not working inside a model... why? - El Forum - 09-08-2009

[eluser]Unknown[/eluser]
Hello! I am new to CI, being my first framework, just started getting into it a week ago. I really like the concept and it seems to fit exactly what I need.

For practice reasons I converted a smaller and rather simple website to a CI-driven site. It's working quite well - mostly. But I came to one issue I can't solve.

There is a visitor counter on tha page. It's very simple, loading the current number from a textfile, checking a cookie if the visitor has been here before, otherwise adding 1 to the number, writing it back into the file and writing a cookie.

The code for this used to be inside the footer-view, because it is executed on every page. But to learn the concept of CI, I created a model for this visitor counter, which at the moment is called by the footer-view. The model looks like this:

Code:
class Visitor extends Model {

    function Visitor() {
        parent::Model();
    }


    function count() {

        $file_name = "res/countfile.txt";
        $dz = fopen($file_name, "r");
        $counter = fread($dz, 10);
        fclose($dz);
        $this->load->helper('cookie');
        $visitor=get_cookie('mysite');
        if ($visitor != 'yes') {
            $cookie = array('name'=>'mysite', 'value'=>'yes', 'expire'=>'0', 'domain'=>'www.mysite.de', 'path'=>'/', 'prefix'=>'');
            set_cookie($cookie);
            $counter++;
            $dz = fopen($file_name, "w");
            fwrite($dz, $counter);
            fclose($dz);
        }

        return $counter;

    }
}

The reading and writing functions are no problem, but the cookie helper functions just don't work. There is no cookie being written or read. The same code is working when I use it directly in the view file.

Is there anybody out there, seeing at one glance where I am thinking wrong?

Thanks!

Kroka


Newbie: Cookie-Helper is not working inside a model... why? - El Forum - 09-08-2009

[eluser]InsiteFX[/eluser]
Try loading the session library and see if it works.

Enjoy
InsiteFX