Adding include files to an ASP script

golden-net

Killer Whale
Hi,

I am running a bulletin board asp script on my windows server using MS SQL - which is fine & fast.

I now want to ad a new advertising script, which works fine on its own in a seperate page - however when I put it in to the bulletin board script it breaks, i.e. throws out various errors. It works if I include it using an iframe, but thats not much good as of course all the links from the ads open in the iFrame NOT the main page. I know I could change all the links in the script to 'blank' but I would prefer to get the thing running right in the first place.

The annoying part is that my google ads script cut 'n' pasted straight in and runs no problem grrrrr.
So any good ideas how to run an 'include file' which is asp in asp???

Thanks for any help.

Chris ?(
 
<!---#include file="../ads/adfile.asp"--->

or

<!---#include virtual="/ads/adfile.asp"--->


the above prosumes you have it as
- mysite.com/forum/ (or wot ever)
- mysite.com/ads/ (change to wot ever)
 
Hi,

Nope tried that - it don't work :(

I have tried all variations of file paths etc but no joy.

As a matter of fact I think its the same for all asp scripts, cos I tried it in the past and now with various asp scripts (using both access & sql databases) but the same thing happens, loads of silly script errors kicking up.

It will work if one of the scripts is in an iframe but not otherwise. So it still looks like you can't 'include' an asp script in another!

Less there is a slick way of doing it, it looks like I am back to bodging it via an iframe :(

There must be a way of running one script, then the other, then displaying the page so that the two don't trip over each other with the various declarations etc.

Any clues anybody

:)
 
I'm not sure that that's the problem, but you can include one ASP file with another. However, ASP doesn't recognise "<!--#include..." so it won't do anything with the other file.

So in a file called test.asp, which is just

<%
response.write("Hello<br>")
<!--#include file=test2.asp -->
%>

where test2.asp contains
<%
response.write("Hello again")
%>

running test.asp will produce:

Hello

because ASP doesn't know what to do with the <!--#..> bit

But, if test.asp contains

<%
response.write("Hello<br>")
%>
<!--#include file=test2.asp -->

running test.asp will produce

Hello
Hello again

because the server only knows what to do with the include directive when it's not inside script terminators...

So if you want to add an include file in the middle of another script, you need to add it like this:

%><!--#include file="other.asp"--><%


Make sense?
 
Nope didn't work :( Thanks anyway.

It is now officially cracking me up .... ?(

The best it ever does is sort of run the included file but comes up with Category no defined (which is part of the sql search string in the included file) - its like the variables (dim) are not getting defined in the included file

As I said it happens in other scripts I have tried to run - when I put two .asp script out puts on the same page.

There MUST be away to make it work ..... ?(
 
why dont you PM me your site (with the include in) and i'll see wots wrong wit it, and PM (or post here) the answer

as it might be a matter of conflict between the forum and ads system (eg the ads system isnt designed to be included)
 
The following class includes VBScript ASP files.
The first http request you make is slow coz it needs to compile the asp to vbs, but thereafter its faster than using SSI #include because its dynamic. Have fun, e ...

<%

class VbsAspIncluder
' ASP VBScript Class: VbsAspIncluder
' Inludes ASP files unless theyve already been included.
' Doesnt work for files which have already been included using the SSI include directive.
' All SSI include directives in included files will also be included by includer.
' If you edit include file 'example.asp' then you need to delete the file 'example.asp.vbs',..
' not perfect but faster than monitoring the asp file for changes using checksum.
' USAGE: -
' dim includer ' <- must be named includer '
' set includer = new VbsAspIncluder
' includer.includeVirtual "/include/file.asp"
' includer.includeFile "file.asp"
' set includer = nothing
' REVISION: 1.0
' DATE: 27-11-2009
' AUTHOR: "Eugene Kerner" <[email protected]>


' class scope variable declarations ' ...
private rootPath, debug, fso, includedFiles

' init() - dont edit these instead use the set methods ' ...
private sub class_initialize()
' SET rootPath if need be, the dir containing your include dir ' ...
rootPath = reReplace(request.serverVariables("APPL_PHYSICAL_PATH"), "\\www\\.*$", true, false, "\www")
' SET debug to true if you are having problems ' ...
debug = false
set fso = createObject("Scripting.FileSystemObject")
set includedFiles = createObject("Scripting.Dictionary")
end sub

private sub class_terminate()
set includedFiles = nothing
set fso = nothing
end sub

' includes a file given a relative file path ' ...
public sub includeFile(byVal aspFile)
aspFile = request.serverVariables("APPL_PHYSICAL_PATH") & aspFile
parseAndExec(aspFile)
end sub

' includes a file given a virtual path ' ...
public sub includeVirtual(byVal aspFile)
aspFile = rootPath & aspFile
parseAndExec(aspFile)
end sub

private sub parseAndExec(aspFile)
aspFile = replace(aspFile, "/", "\")
if not includedFiles.exists(aspFile) then
if fso.fileExists(aspFile & ".vbs") then
if debug then response.write "Including File: " & aspFile & ".vbs" & "<br>" & vbNewLine
parseAndExecVbs aspFile & ".vbs"
else
if debug then response.write "Including File: " & aspFile & "<br>" & vbNewLine
parseAndExecAsp(aspFile)
end if
includedFiles.add aspFile, true
end if
end sub

private sub parseAndExecVbs(vbsFile)
dim fh
set fh = fso.openTextFile(vbsFile, 1)
executeGlobal fh.readAll()
fh.close()
set fh = nothing
end sub

private sub parseAndExecAsp(aspFile)
dim fh, openVbs, closeVbs, idx, cwl, ch1, ch2, vbsStr, inVbs, startVbs, startHtml, skipNextCh, matches
openVbs = "<%"
closeVbs = "%" & ">"
vbsStr = ""
inVbs = false
startDoc = true
startVbs = false
startHtml = true
skipNextCh = false
set fh = fso.openTextFile(aspFile, 1)
while not fh.atEndOfStream
cwl = fh.readLine() & vbNewLine
' update any SSI include directives to use this includer ...
set matches = reMatch(cwl, "#include (file|virtual)=""([^""]+)", false, false)
if matches.count > 0 then
if lCase(matches.item(0).subMatches(0)) = "file" then
vbsStr = vbsStr & "includer.includeFile """ & matches.item(0).subMatches(1) & """" & vbNewLine
else
vbsStr = vbsStr & "includer.includeVirtual """ & matches.item(0).subMatches(1) & """" & vbNewLine
end if
else ' not an SSI line, parse ...
ch1 = mid(cwl, 1, 1)
for idx = 2 to len(cwl)
ch2 = mid(cwl, idx, 1)
if skipNextCh then
skipNextCh = false
elseif ch1 & ch2 = openVbs then ' startVbs ...
if not startHtml then vbsStr = vbsStr & """ & vbNewLine"
vbsStr = vbsStr & vbNewLine
inVbs = true
startVbs = true
skipNextCh = true
elseif ch1 & ch2 = closeVbs then ' startHtml ...
vbsStr = vbsStr & vbNewLine
inVbs = false
startHtml = true
skipNextCh = true
else ' not skipNextCh or openVbs or closeVbs ...
if inVbs then
' inVbs as apposed to inHtml ...
if startVbs then
' we need to know if the first ch is a "=" ro a "@" ...
if not reMatch(ch1, "\s", true, true) then
' found first ch, account for the "=" sorthand for response.write ...
if ch1 = "=" then
ch1 = "response.write "
elseif ch1 = "@" then
ch1 = "' "
end if
startVbs = false
end if
end if
vbsStr = vbsStr & ch1
else ' inHtml ...
if startHtml then
' occurs beginning of a doc, after a closeVbs tag, and after an inHtml newline ...
vbsStr = vbsStr & "response.write """
startHtml = false
end if
if reMatch(ch1, "[\r\n]", true, true) then
' end of an inHtml line, close off string ...
ch1 = """ & vbNewLine" & vbNewLine
startHtml = true
elseif ch1 = """" then
' convert quotes to double quote cox we are in a string ...
ch1 = """"""
end if
vbsStr = vbsStr & ch1
end if ' end inHtml
end if ' end skipNextCh
ch1 = ch2
next
end if
set matches = nothing
wend
fh.close()
' write vbsStr to file so faster for next request ...
set fh = fso.openTextFile(aspFile & ".vbs", 2, true)
fh.write vbsStr
fh.close()
set fh = nothing
executeGlobal vbsStr
end sub

' returns true or flase ' ...
private function reMatch(scalar, regex, ignore, returnBoolean)
dim re
set re = new regExp
re.ignoreCase = ignore
re.pattern = regex
if returnBoolean then
reMatch = re.execute(scalar).count > 0
else
set reMatch = re.execute(scalar)
end if
set re = nothing
end function

' returns an array of match obects ' ...
private function reReplace(scalar, regex, ignore, global, substitute)
dim re
set re = new regExp
re.ignoreCase = ignore
re.global = global
re.pattern = regex
reReplace = re.replace(scalar, substitute)
set re = nothing
end function

end class

%>
 
Back
Top