Welcome Guest, Not a member yet? Register   Sign In
Problems with cookies
#1

[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?
#2

[eluser]Chris Newton[/eluser]
Are you using PHP 4? PHP 4 sometimes has intermittent problems with calling $CI =& get_instance(); in the constructor function.

http://ellislab.com/codeigniter/user-gui...aries.html

Midway towards the bottom this is noted.
#3

[eluser]cheshirecat[/eluser]
Thank you for the reply!
My server serves PHP4 and PHP5 scripts. The library is now working (I have done no corrections...) so it's not bad. But no one knows when it stops working ;-)
#4

[eluser]BizComputing[/eluser]
If you have access to both PHP4 and PHP5, I would recommend you write your code against PHP5. I also have access to both via my shared hosting but .php extension is defaulted to PHP4 and I need to use the .php5 extension to trigger PHP5 processing. Thanks to an associate, I was able to add to my .htaccess which allows PHP5 from the .php extension.

Here's what I added:
AddType x-mapp-php5 .php

From what I understand, this may not work for all configurations.




Theme © iAndrew 2016 - Forum software by © MyBB