简体   繁体   English

中心index.php页面

[英]Centre index.php page

When coding a web application, should all files be linked through a single index.php file. 编写Web应用程序时,是否应通过单个index.php文件链接所有文件。 Would doing such a thing help security, or would it make more complications later on. 做这样的事情有助于安全,或者它会在以后产生更多的复杂性。

How would you guys achieve this? 你们将如何实现这一目标? Most (if no all) the site I have seen use this approach. 我见过的大多数(如果不是全部)网站都使用这种方法。 Is there a reason for this? 是否有一个原因?

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Doing so doesn't such much about security. 这样做对安全性的影响不大。

The main advantage is that having every request go through index.php makes sure that you have a single entry point into your applications's PHP code -- which means you can put your initialization / configuration code there (or call it from there) and it'll always get executed. 主要的优点是让每个请求都通过index.php确保你的应用程序的PHP代码有一个入口点 - 这意味着你可以将初始化/配置代码放在那里(或从那里调用它)和它'永远都会被执行。


On the how to achieve this, you'll need : 关于如何实现这一点,你需要:

  • A RewriteRule so all requests to non-existant files are sent to index.php RewriteRule所有对不存在的文件的请求都发送到index.php
  • In your index.php script (or somewhere called by index.php , like a router) , put some code to call the right controller/action. index.php脚本中(或者由index.php调用的某个地方,比如路由器) ,放一些代码来调用正确的控制器/动作。


For the RewriteRule , here's for example what is often done with Zend Framework : 对于RewriteRule ,这里是Zend Framework经常做的事情:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

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

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