Application level variable initialization in asp classic - asp-classic

I have legacy ASP classic website. If I dump all the application variables using
For Each item In Application.Contents
Response.Write item & " = " & Application(item)
Response.Write "<br>"
Next
They are all initialized but there is no global.asa file in this application.
Where else the initialization code might be?

just add this code in any .asp file and add this file as a top usercontrol in every page so you will get this variable on all pages
session is the other option , you can assign all variable in session and can access in any page (I do not recommend this.)

Related

Lost ASP Session Variable IIS 6.0 to 7.5

I've read a lot about the topic, but I don't get the solution.
I've tried all the solutions the community has posted, but not success.
I am migrating a site from IIS 6.0 to 7.5. The site has both ASP and ASP.NET applications, so I set two different pools.
The ASP.NET apps work without problems.
Where I have the problems is with the ASP applications. All the ASP apps use another app whose objective is to grant the user. The problem becomes when the granting-user app creates a session ("accepted") variable and it gets lost when redirecting. This feature was working in IIS 6.0
I've traced the problem and confirmed that the error comes when redirecting.
What I've tried and checked to fix the problem:
Session Properties
Enable Session State = true
Time-out: 00:20:00
Mode Settings: In Process
Cookie Settings = Use Cookies
Use hosting identity form impersonation = checked
Port to access the site: :8685
I've also checked the web-gardening and it's not enabled as there's only 1 working process set.
I attach the screen captures I consider relevant
I've coded an ASP page which shows the session variable contents. Before redirecting, the session variable is correctly created, but when the redirection takes place, the information is lost.
I attach the code my enterprise policy let me to post:
response.write("Accepted: " & session("aceptado") & "<br>")
response.write ("SQL: " & mySQL & "<br>")
response.write("Accepted: " & session("aceptado") & "<br>")
dim i,j
j = Session.Contents.Count
Response.Write("Session contents: " & j & "<br>")
Response.Write("Contents: <br>" )
For Each i in Session.Contents
Response.Write(i & "<br>")
Next
Response.write("isNull: " & IsNull(Session) & "<br>")
Response.write("¿Aceptado? " & Session("aceptado"))
With the above code, the result is:
The code to check the session status once the redirect takes place is:
<%response.buffer = true
%>
<html>
<h1>test</h1>
<%
dim i,j
j = Session.Contents.Count
Response.Write("Session contents: " & j & "<br>")
Response.Write("Contents: <br>" )
For Each i in Session.Contents
Response.Write(i & "<br>")
Next
%>
isNull: <%=IsNull(Session)%> <br>
¿Aceptado?: <%=Session("aceptado")%><br>
incidencia : <%=Session("tipoincidencia")%><br>
usuario : <%=Session("usuario")%>
</html>
The result executing the above code:
The redirection takes place between the last two images
My Workaround for this is to simply start a ajax call every X (where X is roughly half the time in wich the session expires, in my case 7.5min ).
It's not super clean, but gets the job done.

Classic ASP render a template and send variables

I have a classic ASP page, and I need to create a loop for each row on a table and then create an html document and save it to the hard drive, but I want to create a template so I just send the two variables to the template so I don't have to write the HTML document each time on the loop.
This is what I have so far:
SQL = "select Title, Article from [ASPTest].[dbo].[articles]"
set rs = conn.execute(SQL)
arrRecs = rs.GetRows
For row = 0 To UBound(arrRecs, 2) 'Rows
For col = 0 To UBound(arrRecs, 1) 'Columns
Response.Write rs.Fields(col).Name & " = " & arrRecs(col, row) & " "
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.CreateTextFile("C:\Users\User\Documents\ASP Pages\"+arrRecs(col, row)+".html",true)
f.write("<html><body><div>It kinda works</div></body></html>")
f.close
set f=nothing
set fs=nothing
Next
Response.Write "<br />"
Next
Is there a way to use a template that has 2 variable holders and send the article name and title to the template and then save it to the disk?
Thank you.
I think you could probably achieve what you want using a template stored as a text file, and the Replace function.
Your template should be a fully-formed html page, but with placeholder values for the title and article. The placeholders need to be unique, so something like [[[~~~Title~~~]]] or a similar sequence that will not occur in your actual titles, articles, or the template itself.
<html>
<head><title>[[[~~~Title~~~]]]</title></head>
<body>
<h1>[[[~~~Title~~~]]]</h1>
<div id="article">[[[~~~Article~~~]]]</div>
</body>
</html>
In your code, read the template from the file and store it in a variable. (So technically, you could just write it to a variable in the first place, but VBScript is bad at string concatenation... anyway.) Get your array of titles & articles and loop through it (though only once: I'm not sure why you're looping through both rows and columns in your attempt). For each row, make a copy of the template, replace the title placeholder with the current row's title, replace the article placeholder with the current row's article, and write the result to a file.
Dim template, t
Dim fso, file
Dim rs, conn, SQL
Dim records, row
SQL = "SELECT ID, Title, Article FROM [ASPTest].[dbo].[articles]"
'[...database stuff...]
records = rs.GetRows
'[...close database...]
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("path/to/template.txt",1) '- 1 = For reading
template = file.ReadAll
file.Close
Set file = Nothing
For row = 0 to UBound(records,2)
t = template
t = Replace(t,"[[[~~~Title~~~]]]",records(1,row))
t = Replace(t,"[[[~~~Article~~~]]]",records(2,row))
Set file = fso.CreateTextFile("path/to/html/" & records(0,row) & ".html")
file.Write(t)
file.Close
Set file = Nothing
Next
Set fso = Nothing
Back in the day I created the KudzuASP template engine to solve this rather complex deficiency in Classic ASP. In KudzuASP you can have ASP code pages that have absolutely NO HTML in them.
KudzuASP is as small include file roughly under 1000 lines of code that turns your hosting ASP page into an event driven object used by the template engine.
In short you create an instance of the template engine, set some variables, install custom code objects, and invoke it after which the template engine reads your template and make callbacks to your ASP page when and where appropriate. It has a library system so you can load libraries of custom tags handlers/components via code or by tags placed in your HTML template.
One of the best features is that for those still under the Classic ASP umbrella it makes 100% separation of application code and logic from presentation possible. Coding Classic ASP pages using KudzuASP is much easier than without and because of the way ASP compiles pages the callbacks are "native" and very fast.
You can find it here KudzuASP where the project is still maintained.

Read ASP.Net Cookie from Classic ASP

I'm having some trouble reading the ASP.NET cookie from a Classic ASP file. Here is the code i'm using:
First off, I have the ASP.NET site setup in IIS. I setup an Application in the ASP.NET site and directed it to another folder in inetpub. This Application is called '/classicasp' because its all classic asp inside that application.
In the .aspx file, it is executing this code:
Response.Cookies["testcookie"].Path = "/classicasp";
Response.Cookies["testcookie"].Value = "test";
In the .asp file, it is executing this code:
<%
for each cookie in Request.Cookies
Response.Write( cookie & "=" & Request.Cookies(cookie) & "Path:" &
"<br>")
next
%>
But the .asp page is empty, there are no results displayed on the page from this For Next statement.. Any help on why this is happening? I think i followed instructions and am doing this how its supposed to be, but i guess not..
try
dim x,y
for each x in Request.Cookies
response.write("<p>")
if Request.Cookies(x).HasKeys then
for each y in Request.Cookies(x)
response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
response.write("<br />")
next
else
Response.Write(x & "=" & Request.Cookies(x) & "<br />")
end if
response.write "</p>"
next
I think this is what you are looking for:
Updating ASP cookie from ASP.NET (vice versa)

classic asp testbed for quick function/unit tests

Is there any kind of online "quick" asp/vbscript test tool? It's a pain to load up a test page, put all the stuff in, when I just want to test some ASP VBScript.
Yes, they are called .vbs files. These files are plain text files with a .vbs extenstion. You can run them from the command line. There are some minor differences, just google vbs or WSCript.
One of the most annoying issues is you cannot use response.write instead you use WScript.Echo. This displays the result in a windows style popup messagebox. So, if you use it in a loop to display values, you will have to close the box once for each value that is displayed. Can be a pain. I just contcatonate the results and display one message.
For example save the below code as myloop.vbs
Dim strMsg
Dim i
strMsg = ""
for i = 1 to 10
strMsg = strMsg & CStr(i) & vbCrLf
next
WScript.Echo strMsg
Double-click on myloop.vbs and it runs.
This is a pretty cool way to test out a concept without having to create an asp page and load it on the server.
I realize this isn't a very good/correct answer, but I typically use VB6 (Visual Studio 6) as a quick interactive debuggable VBScript-equivalent testbed. Some syntax is different (CreateObject), and you need to add certain references to your "Default" testing project, but it does allow me to answer any question about type conversions etc within a few seconds (VB6 startup time + type/paste time + F5) rather than the few minutes it will take me to set up an ASP page, and even worse start up ASP debugging, which as of VS2008 and Windows 2003 is just a huge pain.

Conditional includes in Classic ASP - where the file may not exist on the server

I am currently in a situation where I have to make some additions to an application written in classic ASP using server-side JScript on IIS.
The additions that I need to make involve adding a series of includes to the server-side code to extend the application's capabilities. However, the inc files may not exist on the server in all cases, so I need the application to fall back to the existing behavior (ignore the includes) if the files do not exist, rather than generating an error.
I know that this can't be accomplished using if statements in the JScript code because of the way that SSI works, and have not come across any ways of dynamically including the code on the server side, where the files may not exist.
Does anyone know of a way to accomplish this in classic ASP? Any help would be much appreciated.
Here's a script to dynamically include asp files:
<%
' **** Dynamic ASP include v.2
function fixInclude(content)
out=""
if instr(content,"#include ")>0 then
response.write "Error: include directive not permitted!"
response.end
end if
content=replace(content,"<"&"%=","<"&"%response.write ")
pos1=instr(content,"<%")
pos2=instr(content,"%"& ">")
if pos1>0 then
before= mid(content,1,pos1-1)
before=replace(before,"""","""""")
before=replace(before,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
before=vbcrlf & "response.write """ & before & """" &vbcrlf
middle= mid(content,pos1+2,(pos2-pos1-2))
after=mid(content,pos2+2,len(content))
out=before & middle & fixInclude(after)
else
content=replace(content,"""","""""")
content=replace(content,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
out=vbcrlf & "response.write """ & content &""""
end if
fixInclude=out
end function
Function getMappedFileAsString(byVal strFilename)
Dim fso,td
Set fso = Server.CreateObject("Scripting.FilesystemObject")
Set ts = fso.OpenTextFile(Server.MapPath(strFilename), 1)
getMappedFileAsString = ts.ReadAll
ts.close
Set ts = nothing
Set fso = Nothing
End Function
execute (fixInclude(getMappedFileAsString("included.asp")))
%>
The last line (the one starting with "execute") is equivalent to an "include" directive, with the difference that it can be included inside an "if" statement (dynamic include).
Bye
If you are really brave, you can read the contents of the file and then Eval() it.
But you will have not real indication of line numbers if anything goes wrong in the included code.
As a potentially better alternative: Can you not create some sanity check code in global.asa to create the include files as blanks if they do not exist?
Put simply, no. Why would the files not exist? Can you not at least have empty files present?
What you could do is something like this:
Use Scripting.FileSystemObject to detect the presence of the files
Use Server.Exeecute to "include" the files, or at least execute the code.
The only problem is that the files cannot share normal program scope variables.
The solution to this turned out to be to use thomask's suggestion to include the files and to set a session variable with a reference to "me" as per http://www.aspmessageboard.com/showthread.php?t=229532 to allow me to have access to the regular program scope variables.
(I've registered because of this, but can't seem to associate my registered account with my unregistered account)

Resources