ASP - 确定当前脚本是否作为包含运行
假设我有以下页面:
# Include.asp
<%
Response.Write IsIncluded() & "<br>"
%>
# Outside.asp
<!--#include file="Include.asp" -->
我需要它才能工作,这样如果我访问 http://Example.com/直接 Include.asp ,我看到“True”,但如果我访问 http://Example.com /Outside.asp 我看到错误。我宁愿不必向 Outside.asp 添加任何内容。谁能想出一种方法在 ASP 中创建这样的 IsInincluded 函数吗?我能够通过比较 __FILE__
和 $_SERVER['PHP_SELF'] 在 PHP 中创建这样的函数,但这在这里不起作用,因为 ASP 没有像 __FILE__
这样的东西我知道的代码>。
Lets say I have the following pages:
# Include.asp
<%
Response.Write IsIncluded() & "<br>"
%>
# Outside.asp
<!--#include file="Include.asp" -->
I need this to work such that if I access http://Example.com/Include.asp directly, I see "True", yet if I access http://Example.com/Outside.asp I see False. I'd perfer not to have to add anything to Outside.asp. Can anyone think of a way to create such an IsIncluded function in ASP? I was able to create such a function in PHP by comparing __FILE__
to $_SERVER['PHP_SELF'], but that won't work here becasue ASP doesn't have anything like __FILE__
that I am aware of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试检查请求的 URL 并将其与包含内容进行匹配。 JavaScript 中提供的示例
Try checking the URL requested and match it against the include. Example provided in JavaScript
一般来说,在 ASP 中,让包含文件也可作为可由客户端获取的内容并不是一个好习惯。如果您特别想阻止客户端获取包含文件,请将包含文件放入一个文件夹(称为“Includes”)中,然后阻止在 IIS 中访问该文件夹。
另外,如果您确实希望用户能够按原样访问包含文件,并且还允许其他页面包含它,则为包含文件创建一个“主机”页面。例如:-
您现在可以将直接访问“include.asp”时特有的代码和内容移动到 IncludeHost.asp 文件。
Generally in ASP its not good practice to have an include file also available as something that can be fetched by the client. If you specifically want to prevent the client fetching an include file then place your includes in a folder (called say "Includes") then block access to that folder in IIS.
OTH if you do want the user to be able to access the include file pretty much as it is and also allow other pages to include it then create a "host" page for the include. E.g.:-
You could now move code and content that were unique to "include.asp" when it was being accessed directly to the IncludeHost.asp file.