classic asp response.redirect to appropriate page - asp-classic

I created a .html maintenance file and I want when someone goes to the site to redirect to the maintenance folder/index.html
my code: (default.asp at the root)
<%
If InStr( UCase(Request.ServerVariables("SERVER_NAME")), UCase("abc.com") ) > 0 Then
Response.Redirect("/maintenance/")
ElseIf InStr( UCase(Request.ServerVariables("SERVER_NAME")), UCase("web.com") ) > 0 Then
Response.Redirect("/web/")
End If
%>
it works fine, if I go to abc.com but if I type in abc.com/blog it goes to the blog page. how do I prevent so it doesn't go to any sub folders.

Maybe using Request.ServerVariables["HTTP_HOST"] can solve your problem.
Have you tried to look in the variable Request.ServerVariables("SERVER_NAME") with a Response.Write in order to see why the check on the string fails?

Are you using this "test > do" method instead of just taking the site down and creating a custom 404.html page because:
(1) Your site receives calls via various domain names and you want some to work but not others; or
(2) You just didn't think of using the 404 method?
Anyway, if you want to do it via code, then put this at the very top (before header-very important) of a page or even use this AS your index.asp or default.asp page:
<%
s_url = Request.ServerVariables("server_name")
s_url = lcase(s_url)
b_found = InStr(1,s_url, "abc.com",1)
if (b_found <> 0) then
response.redirect("/maintenance/")
end if
%>

Related

What is this redirection called and how can it be setup for an ASP based site?

Does this redirection method have a specific name, and how do I set it up for an ASP based site?
http://www.example.com/?URL=www.redirecteddomain.com
Ok, in that case, it not really the web server, but simply your code that can do this.
so, if you web page was:
http://localhost/MyJumpPage.aspx?URL=www.google.com
So, in your code, all you have to do is grab that 1st parameter, and then run code to jump/navigate to that page.
EG:
string strURL = "";
strURL = Request.QueryString("URL");
Response.Redirect("http://" + strURL);
So, the code behind a button, or even on page load can simply pull the query value from the url string, and then jump to that URL.

Access request after upload (ASP classic)

So, as I figured out, when I have a form with enctype="multipart/form-data" and I upload a file, I can no longer access the object request. The following error is shown:
Cannot use the generic Request collection after calling BinaryRead.
After checking some resources, I stumpled upon a statement, which says: "This is by design". Well, okay, not here to judge about design-decisions.
To give you a quick overview, let me walk you through the code:
if request("todo") = "add" then
Set Form = New ASPForm
category = request("category")
title = request("title")
if len(Form("upload_file").FileName) > 0 then
filename = Form("upload_file").FileName
DestinationPath = Server.mapPath("personal/allrounder/dokumente/")
Form.Files.Save DestinationPath
end if
end if
Nothing too special here so far. Later however, when I try to access my request object, the error mentioned above occures:
<% if request("todo") = "new" then %>
...
My question now, how to get rid of it or fix this. I don't want to open the upload in a popup if there is another way around. This is the only solution I could think off.
Perfectly would be an object, which checks Form and request. Alternatively maybe a check at the top of the file, which object I have to use?
Thanks for any suggestions.
There used to be a very popular ASP class/component that solved ASP file uploads. The site for that component has been taken down, but the code is mirrored here:
https://github.com/romuloalves/free-asp-upload
You can include this ASP page on your own page, and on your page instantiate the class to get access to the files in your form, but also to the form variables. Here is a piece of example code (Upload.Form accesses the form fields):
Dim uploadsDir : uploadsDir = server.mapPath(".") ' whatever you want
Dim Upload, ks, fileKey, mailto
Set Upload = New FreeASPUpload
call Upload.Save(uploadsDir)
ks = Upload.UploadedFiles.keys
for each fileKey in ks
Response.write(fileKey & " : " & Upload.UploadedFiles(fileKey).FileName & "<br/>")
next
mailto = Upload.form("mailTo")
Set Upload = Nothing
If you want to stick to your own implementation, you can probably figure out how to get to the form variables in a multipart/form-data encoded data stream by having a look at the code they use to do so.

Passing a variable in ASP using reponse redirect

I'm trying to pass a variable using Response.Redirect I have a page that I'm processing the info on which contains:
divrec = request.QueryString("div")
divstring = "divisions.asp?"&divrec
Response.Redirect divstring
But when I try to retrieve the information in another page by using
<% divrec = request.QueryString("div")
%>
<% =divrec %>
The variable/string does not display
i think you missed the querystring parameter in your divstring variable.
Try this:
divstring = "divisions.asp?div="&divrec
You should be able to access the parameter in the receiving page now.
What you could do is get the parameter from the url.
So if you redirected the user to www.mysite.com/divisions.asp?something
You could parse it up to the ? and get everything after it.
Try looking at http://www.powerasp.net/content/new/get-current-page-url.asp
Also this page might help you out
how to pass values from one page to other?

.txt embed in .asp file

I'm new to ASP and having a little trouble.
A CMS is pushing out data into .txt files. I do not have the option to change what the CMS outputs, so they have to be .txt files.
a text file named textfile.txt looks like this:
widetxt=<P align='left'><B>Hello world!</B></P>&done=1
I need to display the "widetxt" variable on an .asp page.
The directory structure is like this:
ASP file is at the root of a folder, textfile.txt is located in a folder named "txt" off of the root folder.
index.asp
[txt]
|----textfile.txt
I tried the below code in the asp file, but I get a 500 error: "500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed."
<%
Set fs = CreateObject("Scripting.FileSystemObject")
Set wfile = fs.OpenTextFile(Server.MapPath("txt/textfile.txt"),1,true))
filecontent = wfile.ReadAll
wfile.close
Set wfile=nothing
Set fs=nothing
response.write(filecontent)
%>
I know for a fact both files are on the server and are where they are supposed to be.
If i remove the above code, and just put:
<%
response.write("Hello World!")
%>
the asp file works. So something in the OpenTextFile code is wrong, but i do not have the experience to know what it is.
Any help would be appreciated.
Set wfile = fs.OpenTextFile(Server.MapPath("txt/textfile.txt"),1,true))
You have one too many ) at the end of this statement. Every ( should have a matching ).
Set wfile = fs.OpenTextFile(Server.MapPath("txt/AttachmentFix.txt"),1,true)
Also, I don't see the remainder of your code, but after your response.write(filecontent) make sure to set filecontent as Nothing.
Set filecontent = Nothing
Also, when you're developing in Classic ASP #jsobo is right - you should have Friendly Error messages disabled as you can see what errors the script is throwing back.

is it possible to issue dynamic include in asp-classic?

I mean, like php'h include...
something like
my_file_to_be_included = "include_me.asp"
-- >
for what I've seen so far, there are a couple of alternatives, but every one of them has some sort of shortcoming...
what I'm trying to figure out is how to make a flexible template system... without having to statically include the whole thing in a single file with a loooooong case statement...
here there are a couple of links
a solution using FileSysmemObject, just lets you include static pages
idem
yet another one
same thing from adobe
this approach uses Server.Execute
but it has some shortcomings I'd like to avoid... seems like (haven't tried yet) Server.Execute code runs in another context, so you can't use it to load a functions your are planning to use in the caller code... nasty...
same thing
I think this one is the same
this looks promising!!!
I'm not sure about it (couldn't test it yet) but it seems like this one dinamycally handles the page to a SSDI component...
any idea???
No you can't do a dyanmic include, period.
Your best shot at this is a server.execute and passing whatever state it needs via a Session variable:-
Session("callParams") = BuildMyParams() 'Creates some sort of string
Server.Execute(my_file_to_be_included)
Session.Contents.Remove("callParams")
Improved version (v2.0):
<%
' **** Dynamic ASP include v.2.0
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")))
%>
Sure you can do REAL classic asp dynamic includes. I wrote this a while back and it has opened up Classic ASP for me in a whole new way. It will do exactly what you are after, even though people seem to think it isn't possible!
Any problems just let me know.
I'm a bit rusty on classic ASP, but I'm pretty sure you can use the Server.Execute method to read in another asp page, and then carry on executing the calling page. 15Seconds had some basic stuff about it - it takes me back ...
I am building a web site where it would have been convenient to be able to use dynamic includes. The site is all ajax (no page reloads at all) and while the pure-data JSON-returning calls didn't need it, all the different html content for each different application sub-part (window/pane/area/form etc) seems best to me to be in different files.
My initial idea was to have the ajax call be back to the "central hub" main file (that kicks the application off in the first place), which would then know which sub-file to include. Simply including all the files was not workable after I realized that each call for some possibly tiny piece would have to parse all the ASP code for the entire site! And using the Execute method was not good, both in terms of speed and maintenance.
I solved the problem by making the supposed "child" pages the main pages, and including the "central hub" file in each one. Basically, it's a javascript round-trip include.
This is less costly than it seems since the whole idea of a web page is that the server responds to client requests for "the next page" all the time. The content that is being requested is defined in scope by the page being called.
The only drawback to this is that the "web pieces" of the application have to live partly split apart: most of their content in a real named .asp file, but enough of their structure and relationship defined in the main .asp file (so that, for example, a menu item in one web piece knows the name to use to call or load another web piece and how that loading should be done). In a way, though, this is still an advantage over a traditional site where each page has to know how to load every other page. Now, I can do stuff like "load this part (whether it's a whole page or otherwise) the way it wants to be loaded".
I also set it up so each part can have its own javascript and css (if only that part needs those things). Then, those files are included dynamically through javascript only the first time that part is loaded. Then if the part is loaded repeatedly it won't incur an extra overhead.
Just as an additional note. I was getting weird ASCII characters at the top of the pages that were using dynamic includes so I found that using an ADODB.Stream object to read the include file eliminated this issue.
So my updated code for the getMappedFileAsString function is as follows
Function getMappedFileAsString(byVal strFilename)
Dim fso
Set fso = CreateObject("ADODB.Stream")
fso.CharSet = "utf-8"
fso.Open
fso.LoadFromFile(Server.MapPath(strFilename))
getMappedFileAsString = fso.ReadText()
'Response.write(getMappedFileAsString)
'Response.End
Set fso = Nothing
End Function

Resources