简体   繁体   English

限制在CodeIgniter中直接访问您的视图文件?

[英]Restricting direct access to your view-files in CodeIgniter?

I'm new to CodeIgniter, I was wondering if it's considered a good practice to restrict direct access to your view-files? 我是CodeIgniter的新手,我想知道限制直接访问您的视图文件是否被视为一种好习惯? Obviously they are going to contain a lot of php-code that relies on variables and what not passed to them from the controller, so the php-code could easily come up with an error if it's directly accessed couldn't it? 显然,它们将包含许多依赖变量的php代码,以及未从控制器传递给它们的内容,因此,如果直接访问php代码,则很容易出错,不是吗?

Bonus question: Why are the helpers, libraries, hooks etc. folders empty in the application folder? 额外的问题:为什么助手文件夹,库文件夹,钩子文件夹等在应用程序文件夹中为空?

Thanks for your time. 谢谢你的时间。

The main reason nobody bothers to restrict access to their view files is because they will either fatal error or show a useless page. 没有人愿意限制对其视图文件的访问的主要原因是,它们将导致致命错误或显示无用的页面。

If people want to go to the effort of trying to work out your folder structure and file names, they will be rewarded with... absolutely nothing. 如果人们想尝试找出您的文件夹结构和文件名,他们将得到……绝对没有的收获。 You would have to write some really crazy code to make a view insecure. 您将不得不编写一些非常疯狂的代码以使视图不安全。

If you REALLY want to secure them, go ahead. 如果您真的想保护它们,请继续。

At the top of your view, enter: 在视图顶部,输入:

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

I have never seen anyone restrict access to view files, but i dont see any reason why you wouldn't be able to do so. 我从未见过有人限制查看文件的访问权限,但我看不出您为什么不能这样做的任何原因。 For better security you should really put your system and application folder below the web root and point the index.php back one directory to those folders. 为了获得更好的安全性,您实际上应该将系统和应用程序文件夹放在Web根目录下,并将index.php指向一个目录指向那些文件夹。 That way the possibility of someone directly accessing your files is slim to none. 这样,有人直接访问您的文件的可能性微乎其微。

As for why the folders are empty. 至于为什么文件夹是空的。 The application folder is where you put YOUR code. 您可以在应用程序文件夹中放置您的代码。 The helper, libraries, and models folders in the system folder are filled with source code. 系统文件夹中的帮助程序,库和模型文件夹中充满了源代码。 The goal is to only put your code in application so when you upgrade to future versions of CodeIgniter your dont break any code or functionality you have implemented. 目标是仅将代码放入应用程序中,因此,当您升级到CodeIgniter的将来版本时,请勿破坏已实现的任何代码或功能。 You also can overload the codeigniter functions by doing what is says here... 您还可以通过执行此处所说的操作来重载codeigniter函数。

http://codeigniter.com/user_guide/general/creating_libraries.html http://codeigniter.com/user_guide/general/creating_libraries.html

and scroll down to "Extending Native Libraries" 并向下滚动到“扩展本机库”

user, if you are on a shared host and they don't allow access to anything else than wwwroot folder, you can create a subfolder (name it "private") and write its .htaccess file to deny all requests to this subfolder. 用户,如果您位于共享主机上,并且它们不允许访问wwwroot文件夹以外的其他任何文件,则可以创建一个子文件夹(将其命名为“ private”)并编写其.htaccess文件以拒绝对此子文件夹的所有请求。 Then you can place the system and application folders of codeigniter in this subfolder and place your index.php folder in the regular location (changing the "system" and "application" variables inside index.php to correctly reflect the new paths) and that way all code is secure from direct access. 然后,您可以将codeigniter的系统和应用程序文件夹放置在此子文件夹中,并将index.php文件夹放置在常规位置(更改index.php内的“ system”和“ application”变量以正确反映新路径),这样所有代码均不可直接访问。 :-) :-)

edited: About the folders question, its a scope thing. 编辑:关于文件夹的问题,它的作用范围。 The helpers, libraries, hooks application folders are for application specific items. 辅助程序,库,挂钩应用程序文件夹用于特定于应用程序的项目。 Maybe ones you custom create, or maybe ones downloaded from a third party. 也许是您自定义创建的,或者是从第三方下载的。 But the idea is that you have "system-wide" items and then you have "application-wide" items. 但想法是您拥有“系统范围的”项目,然后才拥有“应用程序范围的”项目。 Having application folders allows you to extend system-wide items to meet the specific application needs (see more @ http://codeigniter.com/user_guide/general/creating_libraries.html ). 拥有应用程序文件夹可让您扩展系统范围的项目,以满足特定的应用程序需求(请参阅更多@ http://codeigniter.com/user_guide/general/creating_libraries.html )。 This doesn't make too much sense with one application, but if your installation has multiple applications, thats really where this comes in handy. 对于一个应用程序来说,这并不太有意义,但是如果您的安装中包含多个应用程序,那真的很方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM