我为访问命中创建了一个帮助器,它包含一个将一些数据插入数据库的函数:
hits_counter_helper.php :
function count_hits($options = array())
{
//Determine whether the user agent browsing your site is a web browser, a mobile device, or a robot.
if ($this->agent->is_browser())
{
$agent = $this->agent->browser() . ' ' . $this->agent->version() . ' - ' . $this->agent->platform();
}
elseif ($this->agent->is_robot())
{
$agent = $this->agent->robot();
}
elseif ($this->agent->is_mobile())
{
$agent = $this->agent->mobile();
}
else
{
$agent = 'Unidentified User Agent';
}
//Detect if the user is referred from another page
if ($this->agent->is_referral())
{
$referrer = $this->agent->referrer();
}
// correcting date time difference by adding 563 to it.
$date = date('Y-m-j H:i:s', strtotime(date('Y-m-j H:i:s')) + 563);
$data = array (
'page_Address' => current_url(),
'user_IP' => $this->input->ip_address(),
'user_Agent' => $agent,
'user_Referrer' => $referrer,
'hit_Date' => $date
);
$this->db->insert('counter', $data);
}
一旦我自动加载帮助程序并在我的控制器中调用此函数:
My_controller.php :
public function index() {
count_hits();
//index code here
}
问题是我得到一个空白页面而其他代码没有运行我认为。我做错了什么?!
答案 0 :(得分:4)
将以下代码添加到辅助函数的开头:
//get main CodeIgniter object
$CI =& get_instance();
将所有$this
替换为您的函数中的$CI
。
然后在控制器中的任何位置加载辅助函数,如下所示:
count_hits();