Welcome Guest, Not a member yet? Register   Sign In
prblem in reading Cookie value
#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




Messages In This Thread
prblem in reading Cookie value - by El Forum - 01-04-2013, 12:02 PM
prblem in reading Cookie value - by El Forum - 01-05-2013, 10:01 AM
prblem in reading Cookie value - by El Forum - 01-05-2013, 10:04 AM
prblem in reading Cookie value - by El Forum - 01-07-2013, 10:49 AM
prblem in reading Cookie value - by El Forum - 01-07-2013, 12:14 PM
prblem in reading Cookie value - by El Forum - 01-08-2013, 12:35 PM
prblem in reading Cookie value - by El Forum - 01-08-2013, 12:39 PM
prblem in reading Cookie value - by El Forum - 01-08-2013, 03:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB