Welcome Guest, Not a member yet? Register   Sign In
Newbie: Cookie-Helper is not working inside a model... why?

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:

class Visitor extends Model {

    function Visitor() {

    function count() {

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

        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?



Try loading the session library and see if it works.


Theme © iAndrew 2016 - Forum software by © MyBB