codeigniter function run without being called, weird - codeigniter

I have a library called Traffic, I use it when I need to record user visit. I autoload it in the autoload config file, but only call the method function when needed.
class Traffic {
function monitor()
{
$CI=& get_instance();
$ip = $CI->input->ip_address();
$input = array( 'ip' => $ip);
$CI->db->insert('traffic', $input);
}
}
I call it like this
class Post extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->traffic->monitor();
$this->load->view('post_view');
}
}
This is post_view.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<img src="http://sample.com/photo/img_2133.jpg" />
</body>
</html>
The problem is, if the image in the page can not be found (it was deleted), the insertion will have happen twice. Look like the 404 error method monitor() of the traffic class. Even though I did not ask it to do so.
So if the post view page has 10 images, and all of them was deleted or not exist, my traffic table will have 11 new records. 1 for what I called in the controller, and 10 for 404 error.
How can I stop 404 error automatically call the monitor method. And how the hell did the 404 errors have access to my library?
UPDATE:
my 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]
My 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";

The problem is within the routing. Let's see to the example of image URL photos/image.jpg.
First, according to the rules in .htaccess the URL (it doesn't match an existing file) is changed to index.php/photos/image.jpg.
Second, according to the rule (:any) the URL is changed to post/index/photos/image.jpg, which refers to the index method of Post controller. So every deleted image causes insertion to the traffic table.
The solution is to filter requests within photos directory and make server to throw genuine 404 HTTP-errors for discontinued images. For example, you can do that by adding photos directory to the .htaccess rule:
RewriteCond $1 !^(index\.php|resources|photos|robots\.txt)

Related

URL Segment in Codeigniter

In my codeigniter project, i am working around the url parameter. Its almost working but there is some problem in the view page which is loading.
I have set all the configuration as per my knowledge. But I doubt there must be some issue in the .htaccess or routes.php file.
.htaccess file --
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
routes.php --
$route['default_controller'] = 'home';
$route['home'] = 'home/index';
$route['faq'] = 'home/faq';
$route['privacy-policy'] = 'home/policy';
$route['doupnow-videos'] = 'home/videos';
$route['doupnow-audios'] = 'home/audios';
$route['morevideo/(:any)'] = 'home/morevideo/$1';
$route['moreaudio'] = 'home/moreaudio';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Controller Page --
public function morevideo($cat)
{
$result['list']=$this->Home_Model->displayVideosAll($cat);
$this->load->view('header');
$this->load->view('doupnow-video-more', $result);
$this->load->view('footer');
}
Now when i navigate to "http://doupnow.com/morevideo/latest", this page getting the parameter correctly and also displaying the data based on the parameter only but the view is not getting the html and css elements like the other pages.
Kindly give me the proper direction. Thank You.
On your site, when you're linking to a script or stylesheet, you link it by doing:
/js/script.js
But you need to give the full url like this:
http://doupnow.com/js/script.js

Laravel 4 500 Internal Server

first I want to say I have read and tried all the other questions here with the same title but I am still getting the same error 500.
I have XAMPP local and everything works ok, the problem is when I upload the site to my webhosting.
index is in public folder and works fine, but when I tried to go to /finder I get the 500 page
this is the structure of my files:
public/index.php its ok
app/views/finder.php 500 error
this is my route file:
Route::get('/', function()
{
return View::make('index');
});
Route::get('finder', 'ApartmentsController#finder');
and this is my htaccess
Options -MultiViews
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
any advice?
thanks in advance.
UPDATE:
the problem is not in htaccess, the problem is in the method finder(), exactly here in the query builder, but why it works local???:
$searchResult=DB::table('books')->join('apartments', 'apartment_id', '=', 'apartments.id');
if(((count($tipo)==NULL)||(count($tipo)==1)) && $tipo[0]==''){
}else if (count($tipo)==1){
$searchResult->where('apt_type','=',$tipo[0]);
}else{
$searchResult->where(function($query){
//Here is the problem, if I delete this piece of code everything works $query->where('apt_type','=',Input::get( 'tipo' )[0]);
for ($i=1;$i<count(Input::get( 'tipo' ));$i++){
$query->orwhere('apt_type','=',Input::get( 'tipo' )[$i]);
}
});
}
The syntax Input::get( 'tipo' )[$i] is only possible in PHP 5.4+. If your host is PHP 5.3, it won't work.
http://php.net/manual/en/migration54.new-features.php
Function array dereferencing has been added, e.g. foo()[0].

codeigniter 404 error at root web site

I'm trying to setup codeigniter for our website.
if I want the main page to be http://example.com
and my folder structure looks like this:
public_html\
application\
common\
system\
how do I setup codeigniter to show the page when going to http://example.com? I'm getting a 404 error when viewing it.
I edited config.php, but im not sure what to put for my base url.
my htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
my routes.php
$route['default_controller'] = "HomeController";
$route['404_override'] = '';
my HomeController.php
public function index()
{
$this->load->model('HomeModel');
$this->load->view('templates/header');
$data['result'] = $this->HomeModel->function1();
$this->load->view('index', $data);
$this->load->view('templates/footer');
}
my HomeModel.php
<?php
class HomeModel extends CI_Model{
public function index()
{
}
public function function1()
{
$query = $this->db->get('work_orders');
//return $query->result();
return $query->result_array();
}
}
EDIT2:
Edit your default_controller from HomeController to homecontroller Like this:
$route['default_controller'] = "homecontroller";
and rename (if needed) your controller from HomeController.php to homecontroller.php and in your Controller itself call it Homecontroller.
Do the same for the model. call it homemodel.php (or home_model.php) in your directory and Call it Homemodel in your model itself.
Also clear your browser cache.
EDIT1:
Rename your model. I guess you called your model HomeModel.php. just call it home_model.php. this should fix your problem.
Your .htaccess file should look like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^LoginTut.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond $1 !^(index\.php|images|table-images|js|robots\.txt|css|captcha)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
This is just copied from my server and it works great.
After then, change your default_controller to:
$route['default_controller'] = "the default controller";
and if public_html is a folder you created your index page should be:
$config['index_page'] = "public_html/";
otherwise leave it blank. (if it's your server's httpdocs)
should work :)
At your router.php in config folder there is an line
$route['default_controller'] = "your controller name";
add your controller that you want to display defaultly
You can make base_url empty:
$config['base_url'] = '';
Try this working .htaccess conf:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
IF nothing helps then hosting doesn't support mod_rewrite apache module

url_suffix in codeigniter not working properly

I am new to Codeigniter and using Codeigniter 1.7, I created a .htaccess file in root directory with code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]
to remove index.php from urls, and added
$config['url_suffix'] = ".html";
to config.php file to provide default suffix for all pages. After that I created a controller test.php with this code:
class Test extends Controller{
function test(){
parent::Controller();
}
function index(){
$this->load->view('test');
}
}
and a view also:
<?php
echo anchor(current_url(), 'This Page');
?>
When I navigate to http://localhost/ci/test.html it works fine, but when I click the link auto generated by anchor() function in my view it goes to http://localhost/ci/index.php/test.html
How can I remove /index.php/ from urls generated by anchor() function?
Also when i point to home page
localhost/ci/index.html
it shows me a 404 Page not found error but when I point to
localhost/ci/index.php
it works fine. Why home page is not being converted to index.html instead of index.php?
You have to create a rewrite condition with htaccess.
To remove index.php, add this to the root folder .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*) /index.php/$1 [L]
If your loading Test as your default controller then this will be your base url
localhost/ci/
or
localhost/ci/test/
or
localhost/ci/test/index.html
Make sure to set your config.php to this
$config['index_page'] = ''
You should also be using Codeigniter 2.1 for proper php 5 support.
Also your constructor funciton should be like so if using php 5. Else stick with what you have for php 4.
public function __construct()
{
parent::__construct();
}

CodeIgniter redirect loop in subfolder on server

I'm stuck with redirect loop when I deploy the files from localhost to my server.
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
The site is multilingual and I'm using .htaccess file to remove the index.php from the uri. I'm trying to deploy the site to a subfolder in my domain (i.e http://www.mydomain.com/subfolder) and ideally I want the urls like this (http://www.mydomain.com/subfolder/en/welcome)
My .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
I have also tried:
RewriteEngine on
RewriteBase /subfolder/
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
My config.php:
$config['base_url'] = 'http://www.mydomain.com/subfolder/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
My routes.php:
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['^../products/(:num)'] = "products/products_list/index/$1";
$route['^../products'] = "products/products_list/index";
$route['^../products/detail/(:num)'] = "products/product/index/$1";
$route['^../(.+)$'] = "$2";
And finally MY_Config class for i18n:
class MY_Config extends CI_Config {
function site_url($uri = '')
{
if (is_array($uri))
{
$uri = implode('/', $uri);
}
if (function_exists('get_instance'))
{
$CI =& get_instance();
$uri = $CI->lang->localized($uri);
}
return parent::site_url($uri);
}
}
This setup works fine in my local environment, I access via http://local.subfolder.com, I set a virtual host to do this, but this setup doesn't work when I deploy to the server in a subfolder.
I would be really glad if someone can point where the problem is. Sorry for the long post, but wanted to give as much as information.
Thanks in advance
I'd try removing the trailing slash from RewriteBase
RewriteBase /subfolder
And also modifying your RewriteCond
RewriteEngine On
RewriteBase /subfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I've noticed that using the ? before /$1 seems to work sometimes in CI too.
Very useful doc:
http://codeigniter.com/wiki/mod_rewrite/
if u want to use the htaccess for a particular folder., u just put your htaccess with in that folder. for ex.,
if ur using five virtual hosting like v1,v2,v3,v4,v5. And if u want to use your htaccess in v3, just put that file within that folder.

Resources