• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why aren't my cookies writting?

#11
[eluser]CroNiX[/eluser]
How are you checking it? You won't be able to read a cookie until the next page reload. If you set it and immediately try to read it it won't work.

#12
[eluser]dkindler[/eluser]
Im not having trouble reading, I'm having trouble writting the cookie.

#13
[eluser]Learn CodeIgniter[/eluser]
You cannot pass TRUE in the second parameter it is expecting a string value and you need to use the time function to get the current time and then add the amount of time to it. With the die your cookie will always fail because it has not been written yet until the next page refresh.
Code:
function login()
{
  $this->input->set_cookie("loggedin", '1', time()+7200);
}

If you want to store TRUE in the cookie then you need to use a serialized array.
Code:
$value = array('logged_in' => TRUE);
$value = serialize($value);

$this->input->set_cookie("loggedin", $value, time()+7200);

$value = $this->input->cookie('loggedin', TRUE);
$value = unserialize($value);

#14
[eluser]dkindler[/eluser]
I am still having issues... Ill post ALL of my code:


Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

function __construct() {
        parent::__construct();
        $this->load->database();
  $this->load->library('encrypt');
  $this->load->helper('url');
  $this->load->library('session');
  $this->load->model('post_model');
  $this->load->model('session_model');
  $this->load->helper('cookie');
  
}

public function index()
{
  if ($this->input->cookie('login', TRUE) == "1") { //if you're logged in
   echo "you are logged in";
  } else { //if you're not logged in
   $global_password = $this->encrypt->encode($this->input->post('global_password')); //encrypt password
   $this->check_global_password($global_password);
  }
}

  function check_global_password($global_password) {
  $pw_in_question = $this->encrypt->decode($global_password); // decrypting encoded pw
  $query = $this->db->get('confidentials');
  foreach ($query->result() as $pw) { // find better way to do this
   $pw = $pw->password;
  }

  if ($pw == $pw_in_question) {
   $this->session_model->login();
   $this->index();
  } else {
   redirect($_SERVER['HTTP_REFERER']);
  }
}


}

and

Code:
<?php
class Session_model extends CI_Model {

function __construct() {
        parent::__construct();
        $this->load->database();
  $this->load->library('session');
  $this->load->helper('cookie');
}

function login() {
  $cookie = array(
      'name'   => 'login',
      'value'  => '1',
      'expire' => '7200',
      'domain' => '',
      'path'   => '',
      'prefix' => '',
      'secure' => TRUE
  );
  
  $this->input->set_cookie($cookie);
}

function logout() {
}
}
?>

#15
[eluser]Learn CodeIgniter[/eluser]
secure needs to be set to FALSE.
Code:
function login() {
  $cookie = array(
      'name'   => 'login',
      'value'  => '1',
      'expire' => time()+7200,
      'domain' => '',
      'path'   => '/',
      'prefix' => '',
      'secure' => FALSE
  );
  
  $this->input->set_cookie($cookie);
}

#16
[eluser]dkindler[/eluser]
Ok, that's a little better haha. I accidently left it in as TRUE because I used a backup file I previously had. So now it still doesn't log in, but then when i refresh the page it claims im logged in. How would I refresh the data before the page loads so it would say that im logged in the first time?

Thank you so much

#17
[eluser]Learn CodeIgniter[/eluser]
Code:
redirect('controller/function', 'refresh');


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.