views:

171

answers:

2

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.

+1  A: 

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")
}
Rodrigo
That works if I have a function that returns the string "Inclue.asp", which I don't... Might end up having to do something similar, as I'm not sure such a thing is possible.
Tristan Havelick
One chance is to throw a exception and catch it, then call Server.GetLastError http://www.w3schools.com/asp/asp_ref_error.asp
Rodrigo
@DrFredEdison: I'm not sure I understand this statement "That works if I have a function that returns the string "Inclue.asp", which I don't"?? Why would need a "function" to return "Include.asp", the code proposed would exist in the Include.asp file, all that is needed then is for the include file to have a literal string that matches its own name.
AnthonyWJones
all I was saying is that I would prefer a function that works regardless of the name of the include. However, I really only need this in one place, so this solution is sufficient.
Tristan Havelick
A: 

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.

AnthonyWJones