我应该包括本地还是远程?
只是我在包含文件时想知道的事情:
假设我想包含一个文件,或链接到它。我应该举个例子:
include("../localfile.php");
还是应该使用
include("http://sameserver.com/but/adirect/linkto/localfile.php");
Is one better than the other?还是更安全?或者这只是个人喜好?
显然,如果您有一个文件要包含到多个目录中的文件中,并且该文件包含不同的文件,那么这是必要的,或者是否有其他方法可以做到这一点?
Just something I wonder about when including files:
Say I want to include a file, or link to it. Should I just for example:
include("../localfile.php");
or should I instead use
include("http://sameserver.com/but/adirect/linkto/localfile.php");
Is one better than the other? Or more secure? Or is it just personal preference?
Clearly it would be a necessity if you had a file that you would include into files in multiple directories, and THAT file includes a different file, or is there some other way of doing that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
读取文件比发出 HTTP 请求和获取响应要快得多。如果可以的话,切勿
include(a_uri)
。如果您想计算包含的完整文件路径,请使用
$_SERVER['DOCUMENT_ROOT']
。Reading a file is much faster than making an HTTP request and getting the response. Never
include(a_uri)
if you can help it.Use
$_SERVER['DOCUMENT_ROOT']
if you want to calculate a complete file path for your include.如前所述,绝对包含本地文件而不是执行 HTTP 请求(这需要更多时间,不会被缓存,并且从技术上讲,如果他知道在哪里查找内容,全世界都可以查看内容)。
还有一个小细节,如果您使用包含文件的完整路径,它甚至会比相对路径更快,特别是如果您使用某种字节代码缓存。
As said before, definitely include a local file and not do an HTTP request (which takes more time, is not cached and the contents are technically viewable to all the world, if he knows where to look for it).
One more small detail, if you use full paths to your included files, it will even be faster then relative paths, especially if you use some kind Byte Code Cache.
绝对包含本地文件,因为 php 脚本并不真正知道或关心您在本地服务器上包含脚本,因此 url 路径会导致 http 请求,而来自 http 请求的网络延迟几乎是一般来说,渲染任何 html 页面时,拥有的页面越少,效果就越好。
就我个人而言,我一般会尽量避免使用
include
和require
,而倾向于使用require_once
,因为使用require_once
意味着您正在编写可重用的代码,而不是编写在包含它时立即执行的代码。引入类定义,引入函数库,但尽量避免在包含它时立即执行的代码,因为这将使其更难以重用。Definitely include the local file, because the php script doesn't really know or care that you're including a script on your local server, so the url path causes an http request, and network latency from http requests is pretty much the bottleneck for rendering any html page in general, the fewer of them you have, the better off you're going to be.
Personally, I try to avoid using
include
andrequire
in general, in favor ofrequire_once
, because usingrequire_once
means that you are writing your code reusably instead of writing code that executes immediately when you include it. Pull in class definitions, pull in function libraries, but try to avoid code that executes immediately when you include it, because that will make it harder to reuse.如果您的问题是保留它,这样当您从登台转移到生产时就不必更改十亿个路径,请按照我学到的这个小技巧:
然后在所有路径引用中使用
BASE_DIR
。当需要移动站点时,只需将该定义更改为新路径(此时应该只是/
)。If your question is about keeping it so you don't have to change a billion paths when you move from staging to production, go with this little tidbit I learned:
Then use
BASE_DIR
in all of your path references. When it's time to move your site, just change that definition to the new path (which should just be/
at that point).除了其他人所说的之外,这些调用还会产生不同的结果,因为删除调用将执行 php output,而不是文件内容。除非你阻止 php 处理该文件,在这种情况下,你会将你的代码暴露给世界,这也不一定是你真正想要的。
In addition to what other people say, these invocations will have different results, since the remove invocation will execute php output, not the file contents. Unless you stop php from processing the file, in which case you're exposing your code to the world which is also not necessarily what you actually want to.
始终包含在本地,因为如果包含远程,某人可以创建不同的文件并做一些令人讨厌的事情。另一个问题是你无法通过远程包含来真正测试它。据我所知你应该使用 require_once 代替......
Always include locally, cause if you include remote someone can create a different file and do nasty things. And the other problem you can't really test this with remote includes. As far as i know you should use require_once instead...