ASP — Session Object
Session Properties
<%
' Session ID
Response.Write(Session.SessionID)
' Timeout in minutes
Session.Timeout = 30
' Abandon session
Session.Abandon
%>
Session Data
<%
' Store data
Session("username") = "John"
Session("loginTime") = Now
Session("preferences") = Array("dark", "large")
' Retrieve data
Response.Write("Hello, " & Session("username"))
%>
Session Events
<!-- Global.asa -->
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Session_OnStart
Session("startTime") = Now
End Sub
Sub Session_OnEnd
' Cleanup code here
End Sub
</SCRIPT>
Check Session
<%
If Session("username") <> "" Then
Response.Write("Logged in as: " & Session("username"))
Else
Response.Redirect("login.asp")
End If
%>
Session vs Application
| Feature | Session | Application |
|---|---|---|
| User | Single | All |
| Storage | Server | Server |
| Lifetime | User session | Server restart |
| Size | Limited | Limited |
Mini Practice
- Store session data
- Retrieve session data
- Add session events
- Check session state
Up Next
Continue with Application Object — application object details.
Related Topics
Frequently Asked Questions about Session Object
What is Session Object in ASP?
Session Object is a fundamental concept in ASP. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Session Object?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Session Object.
Why is Session Object important in ASP?
Session Object is essential for ASP development. Understanding this concept will help you write better code and solve real-world problems more effectively.