2013-03-01 79 views
1

我已经大量使用Codeigniter框架来依赖于我的php web开发。我喜欢这样的事实,即类别和型号有行防止直接访问php配置和类似codeigniter的类

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

防止直接访问该文件。

我想在我的非codeigniter网站(主要是类)中做同样的事情,并想知道我是否可以做同样的事情?是否有最佳做法来做到这一点?

谢谢!

回答

2

你可以与.htacces

我一把抓起codeingiter论坛这个代码,它将从URL中删除index.php文件,并防止您直接访问文件,除非它是一个图像或CSS,你可以看到对评论。

现金去ElliotHaughin

Options -Indexes 
Options +FollowSymLinks 
RewriteEngine On 
RewriteBase /(base domain goes here)/ 

#Removes access to the system folder by users. 
#Additionally this will allow you to create a System.php controller, 

RewriteCond %{REQUEST_URI} ^system.* 
RewriteRule ^(.*)$ index.php?/$1 [L] 

#When your application folder isn't in the system folder 

RewriteCond %{REQUEST_URI} ^application.* 
RewriteRule ^(.*)$ /index.php?/$1 [L] 

#Checks to see if the user is attempting to access a valid file, 
#such as an image or css document, if this isn't true it sends the 
#request to index.php 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [L] 

# If we don't have mod_rewrite installed, all 404's 
# can be sent to index.php, and everything works as normal. 
# Submitted by: ElliotHaughin 

ErrorDocument 404 /index.php 
1

笨设置在index.php文件的基本路径不变。所以你只需要在你的非Codeigniter项目的索引文件中做同样的事情,然后将下面的行添加到你不想直接访问脚本的任何文件中。

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
2

最佳做法是将文件移动到公用文件夹之外,因此您的文件根本无法访问。 只有在公用文件夹中应该公开的文件,如css,js文件,并将应用程序移动到一个文件夹。

所以,如果您的公用文件夹是: /home/pcken/pubic_html

与您的应用程序将您的文件夹 /home/pcken/

和使用的index.php作为一个路由器,包括从该文件夹中的文件。

否则!defined("BASEPATH")工作正常。