Welcome Guest, Not a member yet? Register   Sign In
set_cookie not working
#1

[eluser]ibnclaudius[/eluser]
The cookies are not being set. Is there something wrong?

Code:
public function set_cookie($email)
{
  $length = '20';
  $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  $rand_string = '';
  
  for ($i = 0; $i < $length; $i++)
  {
   $rand_string .= $characters[mt_rand(0, strlen($characters)-1)];
  }
  
  $value = sha1($this->session->userdata('email') . $rand_string);
  
  $cookie = array(
    'name'   => 'remember',
    'email'  => $email,
    'value'  => $value,
    'expire' => '86500',
    'domain' => '.localhost',
    'path'   => '/',
    'secure' => TRUE);
  
  $this->input->set_cookie($cookie);
}
#2

[eluser]Bhashkar Yadav[/eluser]
are you loading cookie helper
Code:
$this->load->helper('cookie');
#3

[eluser]ibnclaudius[/eluser]
Yes.

I am trying to fetch the cookie like this:
Code:
if($this->input->cookie('name') === 'remember')
  {
   $this->form->auto_login();
  }
#4

[eluser]Bhashkar Yadav[/eluser]
please take a reference from here http://ellislab.com/codeigniter/user-gui...elper.html
#5

[eluser]ibnclaudius[/eluser]
That was the first thing I did Confused
#6

[eluser]CroNiX[/eluser]
Code:
if($this->input->cookie('name') === 'remember')
makes no sense. You named the cookie "remember", not "name", so retrieve the "remember" cookie.

#7

[eluser]Aken[/eluser]
The domain of the cookie should be left empty on localhost environments.
#8

[eluser]ibnclaudius[/eluser]
I made the changes, but for some reason the "remember me" is not working. Look:

Code:
public function __construct()
{
  parent::__construct();

  $this->load->helper(array('url'));
  $this->load->library(array('session'));
  
  if(is_array($this->input->cookie('remember')) && $this->session->userdata('logged_in') !== TRUE)
  {
   if($this->form->auto_login())
   {
    redirect('user', 'location');
   }
  }
}

Code:
private function _set_cookie($email)
{
  $length = '32';
  $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  $rand_string = '';
  
  for ($i = 0; $i < $length; $i++)
  {
   $rand_string .= $characters[mt_rand(0, strlen($characters)-1)];
  }
  
  $value = sha1($this->session->userdata('email') . $rand_string . time());
  
  $cookie = array(
    'name'   => 'remember',
    'email'  => $email,
    'value'  => $value,
    'expire' => '86500',
    'domain' => '',
    'path'   => '/',
    'secure' => TRUE);
  
  $create_session = $this->mb_user_mod->create_session($cookie['email'], $cookie['value']);
  
  if($create_session)
  {
   $this->input->set_cookie($cookie);
  }
}
#9

[eluser]CroNiX[/eluser]
cookie isn't an array, so your check fails.
#10

[eluser]ibnclaudius[/eluser]
I tried this for test:

Code:
&lt;?php
class Site extends CI_Controller
{
public function __construct()
{
  parent::__construct();

  $this->load->helper(array('url'));
  $this->load->library(array('session'));
  
  if(is_array($this->input->cookie('remember')) && $this->session->userdata('logged_in') !== TRUE)
  {
   if($this->form->auto_login())
   {
    redirect('user', 'location');
   }
  }
  
  if($this->session->userdata('logged_in') === TRUE)
  {
   redirect('user', 'location');
  }
}

public function index()
{
  $this->load->helper('cookie');
  
  $cookie = array(
    'name'   => 'remember',
    'email'  => 'teste',
    'value'  => 'teste',
    'expire' => '86500',
    'domain' => '',
    'path'   => '/',
    'secure' => TRUE);
  
  $this->input->set_cookie($cookie);
  
  print_r($this->input->set_cookie);
}
}

But give this error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Input::$set_cookie

Filename: controllers/site.php

Line Number: 40




Theme © iAndrew 2016 - Forum software by © MyBB