chaky
chaky

Reputation: 137

Codeigniter 3 cookies not working on the server

my code is working perfectly fine on the localhost but it's not working on the server.

Below is on the home function

$this->input->set_cookie('ci_sm', 'test', 86400);

And I'm try to read this from another page/function

echo get_cookie('ci_sm');

Of cause the library is loaded via autoload.php

$autoload['helper'] = array('cookie');

Testing this on a Ubuntu server but it doesn't seem to work. Any help if you have come across this issue.

Upvotes: 1

Views: 1947

Answers (2)

Oluwaseye
Oluwaseye

Reputation: 695

Load the helper either in your config file by default or in your controller. You can follow my code sample by creating an array and setting the value with whatever data.

class Cookie_controller extends CI_Controller { 

      function __construct() { 
         parent::__construct(); 
         $this->load->helper(array('cookie', 'url')); 
      } 

      public function index() { 
      } 

      public function display_cookie() { 
            $cookie = array(
                'name'   => 'data',
                'value'  => '21',
                'expire' =>  92000,
                'secure' => false
            );
            $this->input->set_cookie($cookie); 
            var_dump($this->input->cookie('data', false)); 
      } 


   } 

Hope this helps

Upvotes: 1

Mahbubul Islam
Mahbubul Islam

Reputation: 1018

Here is my suggestion that may help to overcome this issue:

For site-wide cookies regardless of how your site is requested, add your URL to the domain starting with a period, like this: .your-domain.com

The structure of CI's set_cookie function is:

$this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);

So your final set_cookie function will be like this:

$this->input->set_cookie('ci_sm', 'test', 86400, '.some-domain.com');

Upvotes: 3

Related Questions