ASP - 确定当前脚本是否作为包含运行

发布于 2024-08-02 13:19:12 字数 623 浏览 8 评论 0原文

假设我有以下页面:

# 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-08-09 13:19:12

尝试检查请求的 URL 并将其与包含内容进行匹配。 JavaScript 中提供的示例

function IsIncluded() {
  var url = String(Request.ServerVariables("URL"));
  url = url.substring(0, url.indexOf("?")).substring(0, url.indexOf("#")).substr(url.lastIndexOf("/"));
  return (url == "Include.asp")
}

Try checking the URL requested and match it against the include. Example provided in JavaScript

function IsIncluded() {
  var url = String(Request.ServerVariables("URL"));
  url = url.substring(0, url.indexOf("?")).substring(0, url.indexOf("#")).substr(url.lastIndexOf("/"));
  return (url == "Include.asp")
}
终止放荡 2024-08-09 13:19:12

一般来说,在 ASP 中,让包含文件也可作为可由客户端获取的内容并不是一个好习惯。如果您特别想阻止客户端获取包含文件,请将包含文件放入一个文件夹(称为“Includes”)中,然后阻止在 IIS 中访问该文件夹。

另外,如果您确实希望用户能够按原样访问包含文件,并且还允许其他页面包含它,则为包含文件创建一个“主机”页面。例如:-

# /Includes/Include.asp
<%
%>

# IncludeHost.asp
<!-- #include virtual="/Includes/Include.asp" -->

# Outside.asp
<!-- #include virtual="/Includes/Include.asp" -->
<%
   '' #Other content/code here
%>

您现在可以将直接访问“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.:-

# /Includes/Include.asp
<%
%>

# IncludeHost.asp
<!-- #include virtual="/Includes/Include.asp" -->

# Outside.asp
<!-- #include virtual="/Includes/Include.asp" -->
<%
   '' #Other content/code here
%>

You could now move code and content that were unique to "include.asp" when it was being accessed directly to the IncludeHost.asp file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文