Welcome Guest, Not a member yet? Register   Sign In
prblem in reading Cookie value
#1

[eluser]ahmadalyan[/eluser]
i was trying to write my own library and i want to use cookie in it so i wrote
Code:
$CI =& get_instance();
$CI->input->set_cookie($this->auth_config['user_id'],$user_id );
$CI->input->set_cookie($this->auth_config['user_guid'],$user_guid );
$CI->input->set_cookie($this->auth_config['ip_address'],$ip_address );
$CI->input->set_cookie($this->auth_config['user_agent'],$user_agent );
$CI->input->set_cookie($this->auth_config['last_login'],$last_login );

and when try to used the cookie i wrote

Code:
$CI =& get_instance();
$user_id = $CI->input->cookie("user_id");
$user_guid = $CI->input->cookie("user_guid");
$user_agent = $CI->input->cookie("user_agent");
$last_login = $CI->input->cookie("last_login");

but the value of the variable return false

and when i try to write var_dump ($_COOKIE) i got this

Code:
array (size=2)
  'PHPSESSID' => string '7j0a2cc34rv5ro4b83v7rtb2m2' (length=26)
  'ahmadalyan_blog' => string 'a:6:{s:10:"session_id";s:32:"d8fe12fe8c4d99ac89d75df26610fc3a";s:10:"ip_address";s:9:"127.0.0.1";s:10:"user_agent";s:65:"Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0";s:13:"last_activity";i:1357325670;s:9:"user_data";s:0:"";s:8:"language";s:6:"arabic";}b4b2fa7c5db11174a16a2331523b81cb' (length=306)
#2

[eluser]InsiteFX[/eluser]
Did you look at the cookie_helper ?
#3

[eluser]ahmadalyan[/eluser]
no i didn't try to use this helper
#4

[eluser]ahmadalyan[/eluser]
i try the helper and i have the same result

is there any one can help me
#5

[eluser]CroNiX[/eluser]
You didn't post enough code to be able to tell what's going on for sure.

You can't read a cookie that was set until the NEXT page request. So if you are setting it and immediately trying to read it, it won't work. Cookies, CI or not, don't get sent until the final output is sent to the browser because it's sent in the headers.
#6

[eluser]ahmadalyan[/eluser]
ok i 'm trying to write my authentication library so i wrote this code

Code:
public function __construct()
{
  //session_start();
  // get codeigniter object to work with
  $CI =& get_instance();
  
  // load config file of the library
  $this->auth_config = $CI->load->config('auth', TRUE);
}


function is_logged_in()
{
  $CI =& get_instance();
  if(isset($_SESSION["user_id"]) && isset($_SESSION["user_guid"]) && isset($_SESSION["user_email"]) && isset($_SESSION["ip_address"]) & isset($_SESSION["user_agent"]))
  {
   $user_id = $_SESSION["user_id"];
   $user_guid = $_SESSION["user_guid"];
   $ip_address = $_SESSION["ip_address"];
   $user_agent = $_SESSION["user_agent"];
  
   $CI->db->where($this->auth_config['user_id'],$user_id);
   $CI->db->where($this->auth_config['user_guid'],$user_guid);
   $CI->db->where($this->auth_config['user_agent'],$user_agent);
   $CI->db->where($this->auth_config['ip_address'],$ip_address);
  
   $query = $CI->db->get($this->auth_config['login_table'],1);
   if($query->num_rows() !== 1)
   {
    /* there is a problem in the login information */
    return false;
   }
  
   return true;
  }
  else
  {
   return $this->loggin_from_cookie();
  }

}

function loggin_from_cookie()
{
  // get Code igniter instance
  $CI =& get_instance();
  
  // read information from cookie
  $user_id = $CI->input->cookie($this->auth_config['user_id']);
  $user_guid = $CI->input->cookie($this->auth_config['user_guid']);
  $user_agent = $CI->input->cookie($this->auth_config['user_agent']);
  $last_login = $CI->input->cookie($this->auth_config['last_login']);
/*  var_dump($_COOKIE);  
  echo $user_agent;
  echo $user_guid;
  exit()*/;
  // build where segment of the SQL statment to check if login activity is exists or not
  $CI->db->where($this->auth_config['user_id'],$user_id);
  $CI->db->where($this->auth_config['user_guid'],$user_guid);
  $CI->db->where($this->auth_config['user_agent'],$user_agent);
  $CI->db->where($this->auth_config['last_login'],$last_login);
  
  // get data
  $query = $CI->db->get($this->auth_config['login_table'],1);
  
  
  // check if there is any return ed data
  if($query->num_rows() !== 1)
  {
   /* their username and password combination
   * were not found in the databse */
   return false;
  }
  else
  {
   // read variable from database
   $row = $query->row_array();
   $login_id = $row[$this->auth_config['login_id']];
   $user_agent = $row[$this->auth_config['user_agent']];
   $user_guid = $row[$this->auth_config['user_guid']];
   $user_id = $row[$this->auth_config['user_id']];
  
   // update database recored last login and ip address
   $last_login = date("Y-m-d H-i-s");
   $ip_address = $CI->input->ip_address();
   // check if ip address is vaild
   if(!$CI->input->valid_ip($ip_address))
   {
    return false;
   }
  
   // build array to update data
   $data = array(
   $this->auth_config['ip_address'] => $ip_address ,
   $this->auth_config['last_login'] => $last_login  
   );
  
   // where segment
   $CI->db->where($this->auth_config['login_id'],$login_id);
  
   // do update
   $CI->db->update($this->auth_config['login_table'],$data);
  
   // select segment
   $CI->db->select($this->auth_config['user_name'],$auth_config['user_email']);
  
   // where segment
   $CI->db->where($this->auth_config['user_id'],$user_id);
  
   //get data
   $query = $CI->db->get($this->auth_config['user_table'],1);
  
   if($query->num_rows() !== 1)
   {
    /* their username and password combination
    * were not found in the databse */
    return false;
   }
   // read variable
   $row = $query->row_array();
   $user_name = $row[$this->auth_config['user_name']];
   $user_email = $row[$this->auth_config['user_email']];
  
   //store user id in the session
   $_SESSION["user_id"] = $user_id;
   $_SESSION["user_name"] = $user_name;
   $_SESSION["user_email"] = $user_email;
   $_SESSION["ip_address"] = $ip_address;
   $_SESSION["user_agent"] = $user_agent;
   $_SESSION["last_login"] = $last_login;
   $_SESSION["user_guid"] = $user_guid;
  
   return true;
  
  }

}

so this code test if the user is logged in or not using tow steps
1- test session variables if it has the session variable i will check if this variables are correct or not
2- if he hasn't session variables i will test if there is any cookie he is using to remember him in my website and do log in


#7

[eluser]ahmadalyan[/eluser]
sorry the function that i use to log user in and out are

Code:
function login($email, $password,$is_remember_me_checked)
{
  // get codeigniter instance
  $CI =& get_instance();
  
  // where variable
  $where = "((".$this->auth_config['user_name']."='".$email."' AND ".$this->auth_config['user_pwd']."='".$password."') OR (".$this->auth_config['user_email']."='".$email."' AND ".$this->auth_config['user_pwd']."='".$password."')) ";
  
  // select segment
  $CI->db->select($this->auth_config['user_id']);
  $CI->db->select($this->auth_config['user_name']);
  $CI->db->select($this->auth_config['user_pwd']);
  $CI->db->select($this->auth_config['user_email']);
  
  // where segment
  $CI->db->where($where);
  
  // get data
  $query = $CI->db->get($this->auth_config['user_table'],1);
  //echo $query->num_rows();

  if($query->num_rows() !== 1)
  {
   /* their username and password combination
   * were not found in the databse */
   return false;
  }
  else
  {
   // reading variable
   $row = $query->row_array();
  
   $user_id = $row[$this->auth_config['user_id']];
   $user_name = $row[$this->auth_config['user_name']];
   $user_email = $row[$this->auth_config['user_email']];
  
   // build variable for session
   $user_agent = $CI->input->user_agent();
   $user_guid = crypt(($this->guid()),'AHMADALYAN!@#$1234');
  
   //update the last login time
   $last_login = date("Y-m-d H-i-s");
   $ip_address = $CI->input->ip_address();
  
   // check if ip address valid
   if(!$CI->input->valid_ip($ip_address))
   {
    return false;
   }
  
   $data = array(
    $this->auth_config['user_id'] => $user_id ,
    $this->auth_config['user_guid'] => $user_guid ,
    $this->auth_config['ip_address'] => $ip_address ,
    $this->auth_config['user_agent'] => $user_agent ,
    $this->auth_config['last_login'] => $last_login
   );
  
   // do insert to login activity
   $CI->db->insert($this->auth_config['login_table'], $data);
  
   // set cookie if it check
   if($is_remember_me_checked === true)
   {
    $CI->input->set_cookie($this->auth_config['user_id'],$user_id );
    $CI->input->set_cookie($this->auth_config['user_guid'],$user_guid );
    $CI->input->set_cookie($this->auth_config['ip_address'],$ip_address );
    $CI->input->set_cookie($this->auth_config['user_agent'],$user_agent );
    $CI->input->set_cookie($this->auth_config['last_login'],$last_login );
   }
  
   //store user id in the session
   $_SESSION["user_id"] = $user_id;
   $_SESSION["user_name"] = $user_name;
   $_SESSION["user_email"] = $user_email;
   $_SESSION["ip_address"] = $ip_address;
   $_SESSION["user_agent"] = $user_agent;
   $_SESSION["last_login"] = $last_login;
   $_SESSION["user_guid"] = $user_guid;
   echo 'hi';
   return true;
  }
}

function logout()
{
  session_destroy();
  unset($_SESSION);
  
}


public function guid()
{
  return sprintf('xx-x-x-x-xxx',

    // 32 bits for "time_low"
    mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  
    // 16 bits for "time_mid"
    mt_rand(0, 0xffff),
  
    // 16 bits for "time_hi_and_version",
    // four most significant bits holds version number 4
    mt_rand(0, 0x0fff) | 0x4000,
  
    // 16 bits, 8 bits for "clk_seq_hi_res",
    // 8 bits for "clk_seq_low",
    // two most significant bits holds zero and one for variant DCE1.1
    mt_rand(0, 0x3fff) | 0x8000,
  
    // 48 bits for "node"
    mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
   );
}

and when i want to check if the user is log in i used

Code:
if(!$this->auth->is_logged_in())
{
  $data["language"] = $this->language_dic;
  $data['title']='Admin Login Page';
    
  echo Modules::run('admin_login/index',$data);
  return;
}
#8

[eluser]InsiteFX[/eluser]
You can view cookies if you first set them then do a webpage refresh or refresh your browser.




Theme © iAndrew 2016 - Forum software by © MyBB