<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Site_Controller extends CI_Controller {

    function __construct() 
	{
        parent::__construct();
		$this->user_online();
		date_default_timezone_set('Asia/Phnom_Penh');
    }
	
	private function user_online()
	{	
		$this->load->model("Mod_user");
		if($this->is_logged_in() == true)
		{
			$time = time();
			//$time_check = $time-600; //SET TIME 10 Minute
			$time_check = $time-300; 
			$date = date('Y-m-d');
			$session_id = $this->session->userdata('sessionId');
			$data = array(
				'sessionId' => $session_id,
				'ip_address' => $this->input->ip_address(),
				'active' => 1,
				'date' => $date,
				'time' => $time,
			);
				
			$query = $this->Mod_user->getuser($session_id);	
			if ($query->num_rows() == 0){
				$this->Mod_user->insertuser($data);	
			}else{
				$this->Mod_user->updateuser($data);	
			}
			$this->Mod_user->deleteuser($time_check);
		}		
		
	}
	
	
	private function is_logged_in()
	{
		if($this->session->userdata('sessionId'))
		{
			return true;
		}
		else
		{
			$this->load->helper(array('string'));
			$this->session->set_userdata('sessionId',random_string('nozero', 8));	
			return false;
		}
	}
	
	
}
