我有一个名为“流量”的图书馆,当我需要记录用户访问时,我就会使用它。我将其自动加载到autoload配置文件中,但只在需要时调用方法函数。
class Traffic {
function monitor()
{
$CI=& get_instance();
$ip = $CI->input->ip_address();
$input = array( 'ip' => $ip);
$CI->db->insert('traffic', $input);
}
}我这样叫它
class Post extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->traffic->monitor();
$this->load->view('post_view');
}
}我是post_view.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<img src="http://sample.com/photo/img_2133.jpg" />
</body>
</html>问题是,如果找不到页面中的图像(已删除),则插入将发生两次。类似于流量类的404错误方法监视器()。即使我没有要求它这么做。
因此,如果post视图页面有10个图像,并且所有图像都被删除或不存在,我的流量表将有11条新记录。1表示我在控制器中调用的内容,10表示调用404错误。
如何停止404错误自动调用监视器方法。而404错误是如何访问我的库的呢?
更新:
我的htaccess
#this code redirect site to non www address then remove index.php from url
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]我的routes.php
$route['default_controller'] = "home";
$route['404_override'] = 'my404';
$route['(login|post)'] = "$1";
$route['(login|post)/(:any)'] = "$1/$2";
$route['(:any)'] = "post/index/$1";发布于 2016-01-08 10:02:04
问题在路由范围内。让我们看看图像URL photos/image.jpg的例子。
首先,根据.htaccess中的规则,URL (它与现有文件不匹配)被更改为index.php/photos/image.jpg。
第二,根据(:any)规则,将URL改为post/index/photos/image.jpg,这是Post控制器的索引方法。因此,每个被删除的图像都会导致插入到traffic表。
解决方案是过滤照片目录中的请求,并使服务器对中断的图像抛出真正的404 HTTP错误。例如,可以通过将照片目录添加到.htaccess规则中来做到这一点:
RewriteCond $1 !^(index\.php|resources|photos|robots\.txt)https://stackoverflow.com/questions/34618385
复制相似问题