• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prevent direct access to views

I am having some issues securing my first codeigniter site. Even though I am using rewriting in a .htaccess file to get rid of the index.php segment in URL's, I can still access my php files in the views folder.

Here is my setup:

<VirtualHost *:80>
        ServerName www.domain.de

        DocumentRoot /var/www/igniter
        <Directory /var/www/igniter>
                Options Indexes FollowSymLinks
                AllowOverride all
                Order allow,deny
                Allow from all
        ErrorLog /var/log/apache2/error.log
        LogLevel warn
        CustomLog /var/log/apache2/access.log combined

.htaccess file in /var/www/igniter
<IfModule mod_rewrite.c>
        #AddType x-mapp-php5 .php
        Options -Indexes

        RewriteEngine on

        RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|sitemap\.xml|favicon\.ico)
        RewriteRule ^(.*)$ /index.php/$1 [L]

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php

$route['default_controller'] = "page";
$route['impressum'] = "impressum";
$route['ratgeber/(:any)'] = "ratgeber";
$route['scaffolding_trigger'] = "";

part of my config.php
$config['base_url'] = "/";
$config['index_page'] = "";
$config['uri_protocol']    = "REQUEST_URI";
$config['url_suffix'] = "";

Why can I still call directly to http://www.domain.de/system/application/...e_view.php ?

I use nGinx so I'm not familiar with Apache, but if you want users to not be able to access views, controllers, models, or whatever there is in your public folder, just move it out of the public folder except the index.php

So the server path to your system folder will be something like this:
System: /var/www/site.com/system/
Public: /var/www/site.com/public/

Then in your index.php you have to change the path to your system and application folder like this:

$system_folder = "../system";
$application_folder = "../application";

My application folder is not in my system folder, otherwise it will be something like this:

$system_folder = "../system";
$application_folder = "../system/application";


[eluser]Bart Mebane[/eluser]
If your host allows it, the simplest and most secure approach is to move your system folder above the document root (public html) folder. This Wiki article gives step-by-step instructions.

So if I understand you correctly, this is the only way to prevent direct access?

I don't know actually, I only used the method described above and would strongly recommend it.

[eluser]Bart Mebane[/eluser]
Another option is the approach used in the CI system files. Include this line at the top of every source file:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
The BASEPATH constant is defined in index.php. This statement thus allows pages to be accessed only through CI.

Significantly easier to just move system and application above webroot

Put this .htaccess file in system » application » views:

<Files ~ "\.(htaccess|php)$">
order allow,deny
deny from all

it's recursive, this way nobody can access directly to the views Wink

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.