简体   繁体   中英

current_url() returns false URLs like the favicon address in the page header in codeigniter

I am trying to record the page details which the user is visiting and store them in the database. So, I wrote a function in a helper named hits_helper.php which uses the current_url() of codeigniter to do it.

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 helper is autoloaded.

It works and inserts the page url in the database , but it also inserts some urls like the favicon.ico and some css urls in the head section of the page respectively. What am I doing wrong?!

Create new helper name 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;
}

May be it help you

You're not doing anything wrong, and your model function does what it is suppose to do. Using this approach, you could create a filter function, to exclude exceptions, like this:

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());
    }
} 

, where count_hits() would take current_url() string as parameter.

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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM