• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

How to Create/Set a Server variable

How do I create a Server variable?  I assume the syntax to use would be:
localvar = Server("globalvar")

This should be an easy one, but I need it fairly quickly.
0
SkipFire
Asked:
SkipFire
  • 14
  • 13
  • 7
  • +1
1 Solution
 
hongjunCommented:
Server variable?
Or are you refering to Application / Session variables?

<%
Session("mySession") = "1"
Response.Write Session("mySession")
Response.Write "<br>"

Application("myApplication") = "2"
Response.Write Application("myApplication")
Response.Write "<br>"
%>
0
 
randeepsCommented:


I think you mean application level variables....

Application("SiteName") = "www.experts-exchange.com

you can now use this variable Application("SiteName")  throughout the site and will be accessible to all sessions. There will only be 1 copy of this variable throughout the application's lifecycle..

However if youwish to have a variabe which is inly accessible in a session then you use
Session("SiteName") = "www.experts-exchange.com"

Session variables have different copy for different sessions. So if 10 users are visiting the site then each user will have acess to his own session variable  Session("SiteName") .
0
 
hongjunCommented:
Or if you are refering to ServerVariables, then it is readonly.
We can only get them and not set them.

Eg.

<%
Response.Write Request.ServerVariables("SCRIPT_NAME")
%>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
hongjunCommented:
A list of ServerVariables in a loop

<HTML>

<HEAD>

<title>Request.serverVariables Collection: Copyright &#169; Loh Hon Chun</title>

<script language="JavaScript">
<!--
window.defaultStatus = 'Request.ServerVariables Collection: Copyright © Loh Hon Chun';
//-->
</script>

<style>

td {
      font-size:12px;
      color: #2F4F4F;
      FONT-FAMILY: Arial;
}

h1 {
      font-weight: bold;
      font-size;14pt;
      font-family: Verdana;
      color: #BC8F8F;}
</style>

</HEAD>

<body>

<h1>
Request.ServerVariables Collection Demo
</h1>


<TABLE WIDTH="100%" BORDER="0" CELLSPACING="2" CELLPADDING="2" align="center">
<%
Dim strSV, intRowColor
For Each strSV In Request.ServerVariables
      If intRowColor = 0 Then
            Response.Write "<TR bgcolor=""#ddddfc"">"
            intRowColor = 1
      Else
            Response.Write "<TR bgcolor=""#ffffff"">"
            intRowColor = 0
      End if
      Response.Write "<TD valign=""top"">" & strSV & "</TD>"
      Response.Write "<TD>" & Request.ServerVariables(strSV) & "</TD>"
      Response.Write "</TR>"
Next
%>
</TABLE>

</body>

</html>
0
 
SkipFireAuthor Commented:
I do not mean session or application variables, I mean server variables.  I want a scope above the application level.
0
 
SkipFireAuthor Commented:
Also I do not want to list them, I want to set one.
0
 
hongjunCommented:
>>I mean server variables.
Server Variables cannot be set.
It is readonly


The highest level is Application level.
0
 
randeepsCommented:
there is no scope above application level..

hongjun is right and server variables are populated by the asp engine from IIS metabase along wth some info that comes with the request.


0
 
hongjunCommented:
Another alternative is to save into a database but then I think since you want such a high level, I assume it is to be available to all. Then just use Application variable
0
 
randeepsCommented:
what is your exact requirement?
0
 
SkipFireAuthor Commented:
I want to put the address of the email relay server into an absolutely globally accessible variable as in our environment the address to use is the same across an entire server, but may change between servers.  And it does change between our development, internal prod, and external prod environments.
0
 
randeepsCommented:
use Application  variable as those arealso specific to IIS server
0
 
SkipFireAuthor Commented:
Application variables are not global to a server.
0
 
randeepsCommented:
ifyou have multiple applications from where you wish to get this same info, then the best way is to create an entry into the Windows registry andread it on applicaiton_onstart of each application. That way it will resolve this issue across applications.

hth
0
 
randeepsCommented:
i know they are not server specific - dont take that literally...
>>use Application  variable as those arealso specific to IIS server

means it is specific to IIS Servcer for 1 application. However for cross-applications, there is no way other than persisting the data in flat file, database or registry.
0
 
kevp75Commented:
in your global.asa file

add in
Application("email") = "theemail@theserver.com"

then when you need to call it do something like this

response.write Application("email")

if you do not have a global.asa file....make one
0
 
SkipFireAuthor Commented:
kevp75, it looks like you totally missed the whole conversation.

randeeps,
You said that the servervariables are read from the metabase, do you know where in the metabase?  I looked through and do not see them.  Plus many of them are more dynamic than metabase reads would cover.
0
 
randeepsCommented:
i did read once long ago that some info come from mettabase ... though do not know from where. I'll see if i can get this info foru

But I think you will not be able to create or set your own variables using this method.

Try registry instead.
0
 
kevp75Commented:
I didn't miss the conversation at all, what you are trying to do is near impossosible, as no-one should go and edit their metabase, becasue 1 typo will make your entire server crash.

Your best bet is to set the variable in the global.asa file for each site.

Unless, that is, you set up an SMTP server within IIS to use CDONTS.

Right Click 'My Computer', go to 'Manage', right click 'Internet Information Services' and click 'Properties'  on the second tab (Server Extensions), right in the middle shows 'Options' Specify how mail is sent.  Click on settings and fill out the info.

Please note that you will have to have an SMTP server setup
0
 
SkipFireAuthor Commented:
And how does one access the registry in ASP?  I never had to do that and my ASP is a bit rusty after not using it for 3 years.  I've only been working in .NET lately.
0
 
SkipFireAuthor Commented:
I've reviewed the servers and doing it application level is not going to work, some of the applications do not have a global.asa file, and the shear number of applications makes adding new code rather infeasible, replacing a mail server address with a new value, or a built-in function call to get the value, is feasible.
0
 
randeepsCommented:
keepin mind that no matter what solution u go for, you have to change the mailserver code in all the apps to pickup frmo the registry.
0
 
randeepsCommented:
>>keepin mind that no matter what solution u go for, you have to change the mailserver code in all the apps to pickup frmo the registry.

I mean

keepin mind that no matter what solution u go for, you have to change the mailserver code in all the apps.

The registry has a better solution over just using application variable... as all apps will again require change if mailserver changes. so better solution to incorporate performance will be as I suggested before is to store the mailserver in registry and then in applcation_onstart to pick the mailserver from registry and store in app varaible.. but you haveto look into how frequently the maislerver info will change... if very frequently then just access registry directly everytime you want the mailserver info.

0
 
SkipFireAuthor Commented:
I'm sure the web server will have a maintenance reboot between the time we find out we need to change the SMTP address and the time the old one goes offline, so application level storage is ok, but there is no feasible way of getting the code into every existing (and non-existing) global.asa.
0
 
kevp75Commented:
either route you go, you will end up having to code something in each application, unless like you want to edit the metabase (which I strongly recommend against), besides I don't believe you can set a variable in the metabase
0
 
SkipFireAuthor Commented:
Ok, I have a snag in using the registry.  I created the key and string value that I want to read, but of course the web site does not have permission to that location in the registry.  Rather than changing permissions on the registry, is there a location in the registry than the default anonymous account has access to?
0
 
randeepsCommented:
@SkipFire - you have no other option then to use registry/flatfile/db for primary store of mailserver addr.
0
 
kevp75Commented:
randpeeps is right, and seeing as such, you would have to allow IUSR_machinename access to the registry which is a big no-no
0
 
SkipFireAuthor Commented:
I don't want to give IUSR access to the whole registry, that's why I'm asking if there is a spot that is already should have access to.
0
 
kevp75Commented:
no there isn't

I would suggest using one of our other methods  (preferably the global.asa one, but whatever)
unless, of course, you would rather allow the world to have access your server's registry file
0
 
kevp75Commented:
it's simple.  You have to allow file access to it, which means, the entire file
0
 
SkipFireAuthor Commented:
As I have said over and over, using global.asa is not going to work.
0
 
kevp75Commented:
then you are S.O.L., because unless you want to give the world access to your servers registry, there is no other way other than stated above.  Which means you will have to add code to every single application, whether you use a global.asa file, or a function, or whatever you decide to use.  You will have to implement it in every single application on your server.

But...then again, you don't have to listen to us.  Devise your own method, or allow anonymous access to your servers registry and see what happens, someone will eventually realize that you have done this, and bring down your server.

I only mention global.asa, because having this file in an ASP application is global, meaning it effects the entire site, and not just the files you apply it to.

Building a function is great, because you can call it from any file (so long as it is included in the file), but it is not global to the application.

Whatever method you decide to use, good luck to you, but please, do let us know if you decide to allow anonymous access to your machines server, I know plenty of people who I am sure would love to know this
0
 
randeepsCommented:
Here is the solution:

1. Create a string directly under HKLM i.e Local Machine. Left click on the HKEY_LOCAL_MACHINE. Right click onthe right panel and create a new STRING and name it MAILSERVER. Enter the value of the mailserver and close the registry.

2. create a Test.asp and paste the code below
<%
on error resume next
Set WshShell = Server.CreateObject ("Wscript.Shell")
If Err <> 0 Then
Response.write Err.description & "<br>" & Err.number
response.end
end if

sregval = WshShell.RegRead("HKLM\MAILSERVER")

Response.Write sregval
%>

3. Dont need to change any security/permissions for iusr ( ihave not done it)

4. run the test.asp in the browser and Voila!!!  you will find the value of the mailserver string displayed to the user.

I think access to strings directly under HKLM does not require special permissions. atlease the same above proves it if it also works at your end.

5. Using registry or flat file only solves the problem of centrally storing the mailserver details. You will haveto create a global.asa file under each app (if not available) and then access this registry  key in application_onstart and set in application variable. So any reference of mailserver data in any page should be made using the value stored in the app variable as iut contains the maislerver detail stored in the registry.

Now the problem will be how to notify your applications of updates to new mailserver details if your existing mailserver goes  down - this can be done by creating a new asp page which will do nothing but uipdate the appliocation variable with the new value from the registry. Simple as that. Howeve when you do this make sure you lock the application variable and thneset the new value. Make sure you unlock the variable after setting so that the pages can access the variable.



0
 
randeepsCommented:
have u not tried the solution above?
0
 
SkipFireAuthor Commented:
No I didn't try it, I instead built a DLL to do the work and I have it working in my test environment.  The syntax to do what I need makes it workable as I can do Server.CreateObject("MyObject").GetSMTPAddress and just replace the current SMTP address inline.  Not the solution I was hoping for, but it is close enough and will work once I figure out how to deploy it on non-development servers.
0
 
randeepsCommented:
dont forget to destroy the object after use to avoid memory leak
0
 
SkipFireAuthor Commented:
The object is not put into a variable, so it will be destroyed automatically.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 14
  • 13
  • 7
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now