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

[eluser]vecima[/eluser]
First of all, hello. I've been messing around with CI for a few weeks, checking out the user's guide, and lurking at these forums, and I've been able to get a good amount accomplished. I'm doing a full rewrite of my site on CI and so far am amazed by how everything works.

I have hit a stumbling block though with regards to the cookie helper... I did search these forums but I couldn't find what I was looking for. My problem seems to be that cookies aren't being sent. I'm writing my own style switcher (I know it's been done, but I'm doing it for practice, and to avoid using javascript). So, in the footer that is always shown on my site, i have the following:

Code:
<a href="&lt;?=base_url()?&gt;style/change/standard/&lt;?=$uri_1?&gt;/&lt;?=$uri_2?&gt;/&lt;?=$uri_3?&gt;/&lt;?=$uri_4?&gt;">standard</a> |
<a href="&lt;?=base_url()?&gt;style/change/greyscale/&lt;?=$uri_1?&gt;/&lt;?=$uri_2?&gt;/&lt;?=$uri_3?&gt;/&lt;?=$uri_4?&gt;">greyscale</a> |
<a href="&lt;?=base_url()?&gt;style/change/inverted/&lt;?=$uri_1?&gt;/&lt;?=$uri_2?&gt;/&lt;?=$uri_3?&gt;/&lt;?=$uri_4?&gt;">inverted</a> |
<a href="&lt;?=base_url()?&gt;style/change/sobel/&lt;?=$uri_1?&gt;/&lt;?=$uri_2?&gt;/&lt;?=$uri_3?&gt;/&lt;?=$uri_4?&gt;">sobel</a> |
<a href="&lt;?=base_url()?&gt;style/change/irradiated/&lt;?=$uri_1?&gt;/&lt;?=$uri_2?&gt;/&lt;?=$uri_3?&gt;/&lt;?=$uri_4?&gt;">irradiated</a>

In my style controller, there is only the function "change" in which I set a cookie to determine the style, then redirect back to where the user came from.

my "Style" controller:
Code:
function change($newStyle='standard', $controller=NULL, $function=NULL, $arg1=NULL, $arg2=NULL)
    {
        $back_to = array();
        
        if ($controller != NULL) {
            $back_to[0] = $controller;
        }
        if ($function != NULL) {
            $back_to[1] = $function;
        }
        if ($arg1 != NULL) {
            $back_to[2] = $arg1;
        }
        if ($arg2 != NULL) {
            $back_to[3] = $arg2;
        }
        
        $style_cookie = array(
                   'name'   => 'MyPreferredStyle',
                   'value'  => $newStyle,
                   'expire' => '31556926',
                   'domain' => '.mydomain.com',
                   'path'   => '/',
                   'prefix' => '',
               );

        set_cookie($style_cookie);
    
        $this->data['newStyle'] = $newStyle;
                
         redirect("/$back_to[0]/$back_to[1]/$back_to[2]/$back_to[3]", 'location');
    }

and this is one of my controller's render function. I'm trying to pick the cookie back up and take the value out of it to set the style.

Code:
function render($view='welcome')
    {
        $this->data['uri_1'] = $this->uri->segment(1);
        $this->data['uri_2'] = $this->uri->segment(2);
        $this->data['uri_3'] = $this->uri->segment(3);    
        $this->data['uri_4'] = $this->uri->segment(4);
    
        $this->load->helper('cookie');
    
        if($styled = get_cookie('MyPreferredStyle')) {
            $this->data['newStyle'] = $styled['value'];
        } else {
            $this->data['newStyle'] = 'standard';
        }
        
        $content = $this->load->view($view, $this->data, true);

        $output = $this->load->view('header', $this->data, true);
        $output .= $this->load->view('nav', $this->data, true);
        $output .= $content;
        $output .= $this->load->view('footer', $this->data, true);
        
        $this->output->set_output($output);
    }

one thought I had was: could the cookie not get set due to this running on "localhost" rather than "mydomain.com"... that is, could the browser not accept cookies with differing domains?

a preemptive thanks for the help!
#2

[eluser]schnoodles[/eluser]
Not all of the cookies params are required, so if you dont set the actual domain in there it will just work on the domain it was set on so you wont have to change it when you update.

Only the actual name and value are required, the rest is all optional.
#3

[eluser]vecima[/eluser]
thanks for the reply.

since posting i had changed cookie code in the "change" method in the "Style" controller to this:
Code:
$style_cookie = array(
                   'name'   => 'MyPreferredStyle',
                   'value'  => $newStyle,
               );

        set_cookie($style_cookie);
but the cookie still isn't being set. does it matter where i load the cookie helper?
currently I'm loading the cookie helper in the Style controller's constructor like this:
Code:
function Style()
    {
        parent::Controller();        
        
        $this->load->helper('cookie');
    }
and the same way in the other controllers' constructors (since they need to get_cookie, if i can get it to set one.
#4

[eluser]Michael Wales[/eluser]
In config.php:

Code:
/*
|--------------------------------------------------------------------------
| Cookie Related Variables
|--------------------------------------------------------------------------
|
| 'cookie_prefix' = Set a prefix if you need to avoid collisions
| 'cookie_domain' = Set to .your-domain.com for site-wide cookies
| 'cookie_path'   =  Typically will be a forward slash
|
*/
$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']        = "/";
#5

[eluser]vecima[/eluser]
Thanks for the reply, walesmd, but I'm not sure what you're saying... in those config entries i've tried entering a domain but no cookie is being set in the browser at all... also, i thought that if the domain was left blank, CI filled in the sites current domain... am i wrong?
#6

[eluser]vecima[/eluser]
Just as an update and i guess a bump...

I've gotten this working with the regular PHP setcookie method and $_COOKIE variable. The whole thing works except for my code using the CI cookie helper. To be honest it's about the same amount of code to do it either way, so now I'm wondering if there is some benefit of using the helper? Obviously the helper makes the code a little bit cleaner, but I have yet to get it to work properly, and I really can't see the reason why. I'm not new to PHP or OOP, but I must be missing something here (I've checked it over countless times, though).

I'm hoping someone here can help me do it the CI way, otherwise, since it works, I'll just go on using the regular PHP cookie code :-S
#7

[eluser]juworld[/eluser]
I'm having the same problem. I also got it working with $_COOKIE and not with CI cookie helper.
#8

[eluser]vecima[/eluser]
yeah, sadly i've never been able to make it work with CI cookie code and am still using the regular php method...

the CI way doesn't really save you much code, but it gives it that nice CI look. :-)

I HEREBY CHALLENGE ALL CI PROS HERE TO COME UP WITH A WORKING COOKIE EXAMPLE!!!!

j/k, but if you wanted to you could ;-) (maybe just one quick controller and view).
#9

[eluser]Jim Higgins[/eluser]
I realize this is an old post, but after searching and searching, I have yet to find a solution. I am also experiencing the same problem and can not set a simple test cookie. Anyone have a solution to the above?




Theme © iAndrew 2016 - Forum software by © MyBB