【问题标题】:current_url() returns false URLs like the favicon address in the page header in codeignitercurrent_url() 返回错误的 URL,例如 codeigniter 中页面标题中的 favicon 地址
【发布时间】:2023-03-04 13:59:01
【问题描述】:

我正在尝试记录用户正在访问的页面详细信息并将其存储在数据库中。所以,我在一个名为 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,但它也会在页面的 head 部分分别插入一些 url,如 favicon.ico 和一些 css url。我做错了什么?!

【问题讨论】:

  • 尝试加载url库$CI->load->helper('url');
  • 加载url helper,因为你正在调用current_url,它在url helper中
  • 正如我所说,它插入页面 url 并且可以工作。这意味着 url 助手是自动加载的。但是,它也会插入一些虚假的网址。

标签: php codeigniter


【解决方案1】:

创建新的助手名称 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;
}

也许对你有帮助

【讨论】:

  • 我先测试两天再通知大家
【解决方案2】:

你没有做错任何事,你的模型函数做了它应该做的事情。使用这种方法,您可以创建一个过滤函数来排除异常,如下所示:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多