[eluser]cheshirecat[/eluser]
Hey! This is my first post on this forum so I say Hello!
I've got a problem with setting cookies is CI.
I wrote a visit counter. It has a cookie lock. First, I placed it in a controller, and then I made a library of it. And this is an obscure thing that when it is a controller it works, and when a library it does work too (but not always) Example: this morning I tried if it works and it does but now it's not working. No changes in code between tests. Does anyone know why?? In a controller it works always, but I need it to be an library.
The problem is the library not always sets cookie. Sometimes it does, and othertime it doesn't.
This is the code.
Controller:
Code:
class Counter extends Controller
{
function Counter()
{
parent::Controller();
}
function index()
{
$this->load->helper('file');
$this->load->helper('cookie');
if ( !get_cookie('counter'))
{
$cookie = array(
'name' => 'counter',
'value' => 'counter runned',
'expire' => '3000'
);
set_cookie($cookie);
$dane = read_file('./docs/misc/licznik.txt');
$dane++;
write_file('./docs/misc/licznik.txt', $dane);
echo $dane;
}
else
{
$dane = read_file('./docs/misc/licznik.txt');
echo $dane;
}
}
}
And this is the library:
Code:
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Counter
{
function Counter()
{
$CI =& get_instance();
log_message('debug', 'Counter class initialized.');
$CI->load->helper('file');
$CI->load->helper('cookie');
}
function run()
{
if ( !get_cookie('counter'))
{
$cookie = array(
'name' => 'counter',
'value' => 'counter runned',
'expire' => '500'
);
set_cookie($cookie);
$count = read_file('./docs/misc/licznik.txt');
$count++;
write_file('./docs/misc/licznik.txt', $count);
return $count;
}
else
{
$count = read_file('./docs/misc/licznik.txt');
return $count;
}
}
}
The library is called by another controller in a constructor:
Code:
function Name() {
$this->load->library('counter');
$this->counter->run();
}
And then sent to a view.
Someone knows the solution?