[英]current_url() returns false URLs like the favicon address in the page header in codeigniter
我試圖記錄用戶正在訪問的頁面詳細信息並將其存儲在數據庫中。 因此,我在名為hits_helper.php
的幫助器中編寫了一個函數,該函數使用codeigniter的current_url()
來執行此操作。
function count_hits($options = array())
{
$CI =& get_instance();
$CI->load->library('user_agent');
$date = date('Y-m-j H:i:s', strtotime(date('Y-m-j H:i:s')) + 1214);
$data = array (
'page_Address' => current_url(),
'hit_Date' => $date
);
$CI->db->insert('counter', $data);
}
網址幫助程序已自動加載。
它可以工作並將頁面URL插入數據庫中 ,但也可以在頁面的頂部分別插入一些URL,例如favicon.ico和css url。 我究竟做錯了什么?!
創建新的助手名稱My_url_helper
function current_url()
{
$CI =& get_instance();
$url = $CI->config->site_url($CI->uri->uri_string());
return $_SERVER['QUERY_STRING'] ? $url.'?'.$_SERVER['QUERY_STRING'] : $url;
}
可能對您有幫助
您沒有做錯任何事情,並且您的模型函數可以執行您應該做的事情。 使用這種方法,您可以創建一個過濾器函數以排除異常,如下所示:
function filter_hits(){
$exeptions = array('css', 'js', 'images');
foreach($exceptions as $exception){
if(!strpos(current_url(), 'http://'. base_url() . $exception))
$this->count_hits(current_url());
}
}
,其中count_hits()
將采用current_url()
字符串作為參數。
function count_hits($current_url, $options = array())
{
$CI =& get_instance();
$CI->load->library('user_agent');
$date = date('Y-m-j H:i:s', strtotime(date('Y-m-j H:i:s')) + 1214);
$data = array (
'page_Address' => $current_url,
'hit_Date' => $date
);
$CI->db->insert('counter', $data);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.