Home / code / asp

Applications, sessions and cookies,

A application is the website itself. A session is the connection between the browser and the server. If the user opens 2 pages in different browser-windows at the same time, then there are 2 sessions.

For every object there are 2 events, the action that needs to connected to a event is written in global.asa

GLOBAL.ASA

In this file the data and the script, that the application object or the session object need, are stored. global.asa should be in the start-folder of the website.

Example:

<SCRIPT LANGUAGE="vbscript" RUNAT="server">
SUB Application_OnStart
  Application("visitors")=0
  Application("now1")=now
END SUB
SUB Session_OnStart
  Application("visitors")=application("visitors")+1
END SUB
SUB Session_OnEnd
  Application("visitors")=application("visitors")-1
END SUB
</SCRIPT>

The object Application

With the application object you can let users share certain data. A ASP-application is defined as all ASP-files in a virtual map and the sub-maps of that. You can use the method LOCK and UNLOCK to prevent that users change the same data at the same time.

Application.method

Contents All elements are pointed to the application object (in the global.asa-file). With application.contents("elementname"), you can request the value.
<% =application.contents("visitors") %>
With count you can request the number of elements.
number=application.contents.count
StaticObjects All the object that are added to the session with <OBJECT>
Lock With the method LOCKyou prevent that other users can change the properties of the application object.
Unlock With this command you give other clients the opportunity to change the properties.
Application_OnEnd This event is started when the website is "closed".
Application_OnStart This event is started when the first visitor access the site.

Example:

<%
  Application.lock
  Application("now1")=now
  Application.unlock
%>
This prevents the application from crashing.

Example 2:

SUB Application_OnStart
  Application("myappvariable")=" "
  Application("anotherappvariable")=0
  Application("visitors")=0
END SUB

Sessions

With the session-object you can store information that is needed for a user-session. Variables that are stored in the session-object are not removed if the user goes to another page. You can use the session methods to end a session and to add a time-outperiode for a inactive session.

Contents All elements that connected to a session object (in the file global.asa). With session.contents("elementname") you can request the value.
StaticObjects Holds all the objects that are made with the <object> and that have a sessionlimit
CodePage The codepage that is used for pointing symbols.
LCID The local ID. Don't use it with CodePage
SessionID The session-Id for this user. You can not use this for a database.
Timeout The Time-out time for this session, in minutes. Standard 20 min.
Abandon With this method A session-object is destroyed and in the session used sources are released.
Session_OnEnd Is executed at the end of a session.
Session_OnStart Is started when the user requests the first page.

Cookies

A cookie is a small file that is saved on the server's computer. It is used to collect data and to save it for later use.

Create cookies

cookies with 1 value:
RESPONSE.COOKIES("cookiename")=value

Cookies with more values:
RESPONSE.COOKIES("cookiename")("fieldname")=value

Example:

response.cookies("clientnr")=request.form("clientnr")
response.cookies("client")("clientnr")=request.form("clientnr")
response.cookies("client")("name")=request.form("name")
response.cookies("client")("address")=request.form("address")
response.cookies("client")("postcode")=request.form("postcode")
response.cookies("client")("city")=request.form("city")

Read cookies
With this request you can read cookies placed by your server.

cookies with 1 value:
value=REQUEST.COOKIES("cookiename")

Cookies with more values:
value=REQUEST.COOKIES("cookiename")("fieldname")

Example:

clientnr <% =request.cookies("clientnr") %>
clientnr <% =request.cookies("client")("clientnr") %>
name <% =request.cookies("client")("name") %>
address <% =request.cookies("client")("address") %>
postcode <% =request.cookies("client")("postcode") %>
city <% =request.cookies("client")("city") %>

Testing if the Cookie already exists:
REQUEST.COOKIES("cookiename").haskeys
Returns TRUE or FALSE. TRUE if the cookie exists.

Enter Expire-data
RESPONSE.COOKIES("cookiename").EXPIRES=date
It is recommended to use relative date(today+x days) instead of fixed date.

Example:
response.cookies("client").expires=Date+1

Remove a cookie
The easiest way to remove a cookie is to put the expire-date on yesterday.
response.cookies("client").expires=Date-1

 

TOP

Latest script:

 

Books: