Computer Science

ASP Session Examples

The sessions allow us to store data that will be separate for each user.
For example, we can store a variable that tells us how many pages of our website the user has visited, save an id that identifies him with a user registered in the database, etc.

Storing a value in a session is as simple as:

Session (“varname”) = 20

And to display it on the screen:

<% = Session (“counter”)%>

This value will automatically be available on any ASP page.

An example to better understand how it works:

sessions1.asp:

<% If IsEmpty (Session (“counter”)) Then
Session (“counter”) = 0
End If

Session (“counter”) = Session (“counter”) + 1%>
<p>
You carry <% = Session (“counter”)% gt; page views.
</p>
<a href=”sesiones2.asp”> Go to page 2 </a>

sessions2.asp:

<% If IsEmpty (Session (“counter”)) Then
Session (“counter”) = 0
End If

Session (“counter”) = Session (“counter”) + 1%>
<p>
You carry <% = Session (“counter”)% gt; page views.
</p>
<a href=”sesiones1.asp”> Go to page 1 </a>

If you try it you will see how the counter is increasing, on the other hand if you enter from another pc or with another browser, you will see that the counter starts from 0 and does not interfere with the data of the other computer.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button