| << 8.4.1- Session Object Collections | Chapter8 | 8.4.3- Session Object Methods >> |
Session Object Properties
The Session object has four properties
- SessionID
- Timeout
- CodePage
- LCID
SessionID Property
The SessionID property is a read-only property that returns the session identification number for each user. Each session has a unique identifier, generated by the application when the session is created. The session identification number is actually a cookie that is stored on the user's machine. It will expire only when the session times out. One rather large problem with the Session object is that it simply won't work if the user has cookies turned off. The session won't get started when the user browses the site and they won't get access to the Session object.
To retrieve the current user's SessionID, use:
<%= Session.SessionID %>
Within the same server cycle, all SessionID's have unique values. If the Web server is stopped and restarted, some SessionID values may be the same as values generated before the server was stopped. For that reason, you should not use the SessionID property to generate primary key values for a database application. You could use it as a way of tracking all of the currently active users in an application-level variable such as an array.
Timeout Property
The Timeout property sets the timeout period assigned to the Session object for any application, in minutes. If the user does not request or refresh a page before the timeout period expires, the session ends.
You can set the Timeout value by:
<% Session.Timeout = 30 %>
This will cause the session to timeout in 30 minutes if there is no activity. If you want to retrieve the value of the Timeout property, you can use this:
<%= Session.Timeout %>
You should exercise caution when setting Timeout values. If you set Timeout to too short a duration, the server will terminate user sessions too quickly. If you rely on session variables to process user data and the user has not been able to complete the processing of an identification form, for example, the loss of session variables will cause all sorts of problems.
Setting Session Timeout to too long a duration also has its problems. All Session variables are stored in the server's memory. Since the server has no way to determine if the user is still viewing your sites' pages, you are probably going to have quite a few user sessions eating up server memory resources, if sessions last for too long a time. This is particularly critical if you store database query results in Session arrays. These results can hold a lot of data sometimes, and having multiple unused sessions still open with large amounts of data stored in them could slow down your server.
The best strategy is to analyze each application, by conducting average user browsing time tests, and then set the session timeout to a more appropriate value where needed. Generally though, the 20 minute timeout is a good compromise for most applications.
CodePage Property
The CodePage property determines the code page that will be used to display content. A code page is used to map between the characters that are displayed on the screen and an internal table. Unless you are developing sites that use non-Roman alphabets, such as Russian or Japanese, you will not have to worry about setting this property.
In order to be able to set the code page property you must have first enabled code page support with:
<%@ CODEPAGE = CodePage %>
Then, to set the code page to the Roman alphabet, use:
<% Session.CodePage=1252 %>
The LCID Property
A locale identifier determines time-zone and language rules for the system. The LCID property specifies the system's location identifier, which will be used to display content. LCID uniquely identifies one of the installed system-defined locales. If a location identifier has not been installed, it cannot be set.
To set the LCID to U.S. English, use:
<% Session.LCID = 1033 %>
This also happens to be the default value for the LCID property for servers installed in the US, meaning you don't need to explicitly set it. The various LCID values are usually defined in the documentation as hexadecimal values. You must convert these values to a decimal value to use when setting the LCID property.
| << 8.4.1- Session Object Collections | Chapter8 | 8.4.3- Session Object Methods >> |

RSS

