0% found this document useful (0 votes)
25 views16 pages

Managing JavaScript Session Variables

Session variables allow values to persist across multiple pages during a user's session. They are declared using Session("variable") and can be accessed on subsequent pages during that session. Variables are destroyed when the session ends, such as when the browser is closed. This document discusses declaring, accessing, checking if, and destroying session variables in ASP.

Uploaded by

Harshita Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views16 pages

Managing JavaScript Session Variables

Session variables allow values to persist across multiple pages during a user's session. They are declared using Session("variable") and can be accessed on subsequent pages during that session. Variables are destroyed when the session ends, such as when the browser is closed. This document discusses declaring, accessing, checking if, and destroying session variables in ASP.

Uploaded by

Harshita Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

Session Variables :

The variables which we have used till now are considered


as local variables , they will get declared
each time ,we load the asp page containing those
variables.
Sometimes we require some global variables , which once
declared should exists or accessable till the entire session.
Session is the duration during which client is in contact
with the server.
Here,in our case session will continue as long as we not
close the Internet Explorer window.

Decalaring the Session variables:


==========================

To declare the session variable,the general form is ,

Session("VARIABLE NAME")=expression

e.g.
Session("COUNT")="1"

Accessing the value of the session variable


===============================
To access the value of the session variable , we will
simply use,
Session("variable name")
e.g.
[Link](Session("COUNT"))

Checking the Existance of a session variable


===================================
isEmpty( ) function is used for checking the existance of
a [Link] function will return true if the variable
contains null or in other words it doesnot exists and it
returns false if it exists.

The general form is ,


isEmpty(variablename)

e.g.
isEmpty(Session("COUNT"))

If it returns true then the session variable COUNT


doesnot exists.

Destorying the Session variable


=======================
To destroy the Session variable ,we can use on of the
following statements,

(i) Set Session("variablename")=Nothing

(ii) Set Session("variablename")=null


(iii) Session("variablename")=""

Q Write the ASP code for implementing the page


counter.

<html>
<head>
<title>Session Variables : Page Counter</title>
</head>
<body bgcolor="#ccffee">
<%
Dim cnt

if (isEmpty(Session("COUNT"))) then
Session("COUNT")="1"
cnt="1"
else
cnt=Session("COUNT")

cnt=cnt+1

Session("COUNT")=cnt
end if

[Link]("You have visited the page " & cnt &


" times")
%>
</body>
</html>

Question :
<html>
<head>
<title>Session variables : Access Denied</title>
</head>
<body bgcolor="#ccffee">
<form name="myform" action="[Link]">
<center>
<br>
<br>
<marquee>[Link]</marquee>
<br>
<table border=2>
<tr>
<th colspan=2>Login Details</th>
</tr>
<tr>
<th>User Name :</th>
<td><input type="text" name="txtuname" size=30></td>
</tr>
<tr>
<th>Password : </th>
<td><input type=password name="txtpass"
size=30></td>
</tr>
<tr>
<td colspan=2><pre> <input type=submit> <input
type=reset></td>
</tr>
</table>
</center>
</form>
</body>
</html>
<html>
<head>
<title>Validation</title>
</head>
<body bgcolor="#ccffee">
<%
Dim uname,upass

uname=[Link]("txtuname")
upass=[Link]("txtpass")

if uname="admin" and upass="1234" then


Session("USERS")="admin"

[Link]("<center>")
[Link]("Welcome , Administrator <br>")
[Link]("<a href=[Link]>Start</a>")
else
[Link]("Invalid User Name or Password")
end if
%>
</body>
</html>
<html>
<head>
<title>Photo Gallary</title>
</head>
<body bgcolor="#ccffee">
<%

if(isEmpty(Session("USERS"))) then
[Link]("Access Denied")
else
[Link]("<center>")
[Link]("<br><br><Marquee><h3>Photo
Gallary</h3></marquee>")
[Link]("<br><br><img src=[Link]
height=100 width=100><a href=[Link]>Cars</a>")
[Link]("<br><br><img src=[Link]
height=100 width=100><a href=[Link]>Actors</a>")

[Link]("<br><br><img src=[Link]
height=100 width=100><a href=[Link]>God </a>")
[Link]("<br><br><img src=[Link]
height=100 width=100><a
href=[Link]>Hollywood</a>")
[Link]("</center>")
end if
%>
</body>
</html>
Q . Desing the following application

<html>
<head>
<title>Employee Information System</title>
</head>
<body background=bg_057.gif>
<%
'Maintain the employees record in array
Dim empid(5),names(5),salary(5)

empid(1)="101"
names(1)="Kapil"
salary(1)="18900"

empid(2)="102"
names(2)="Lalit"
salary(2)="19200"

empid(3)="103"
names(3)="Vinay"
salary(3)="9000"

empid(4)="104"
names(4)="Sudeep"
salary(4)="8900"

empid(5)="105"
names(5)="Harsh"
salary(5)="7900"

Dim rno

if (isEmpty(Session("RECNO"))) then
'place record pointer on the first record
rno="1"

Session("RECNO")="1"
else
'determine on which button the user have clicked

Dim bname

bname=[Link]("cmdbutton")

rno=Session("RECNO")

if bname="Next" then
rno=rno+1
if rno>5 then
rno=1
end if
else
if bname="Previous" then
rno=rno-1
if rno<1 then
rno=5
end if
else
if bname="First" then
rno=1
else
rno=5
end if
end if
end if

'update the session variable

Session("RECNO")=rno

end if
%>
<br>
<img src=[Link]>
<br>
<br>
<center>
<form name="myform" action="[Link]">
<img src=bar_119.gif>
<br>
<br>
<table border=2>
<tr>
<th colspan=2>Employee Information System</th>
</tr>
<tr>
<th align=left>Employee Id</th>
<td><input type=text name="txtempid" size=30 value=<
%= empid(rno) %> disabled></td>
</tr>
<tr>
<th align=left>Name</th>
<td><input type=text name="txtname" size=30 value=<
%= names(rno) %> disabled></td>
</tr>
<tr>
<th align=left>Salary</th>
<td><input type=text name="txtsalary" size=30 value=<
%= salary(rno) %> disabled></td>
</tr>
<tr>
<td colspan=2><pre> <input type=submit
name=cmdbutton value=First> <input type=submit
name=cmdbutton value=Previous> <input type=submit
name=cmdbutton value=Next> <input type=submit
name=cmdbutton value=Last> </td>
</tr>
</table>
<br>
<br>
<img src=bar_119.gif>
</form>
</center>
</body>
</html>

Common questions

Powered by AI

Session variables store the current record number, 'Session("RECNO")', to track the user's position in the dataset. Navigation logic uses conditional checks on button clicks ('Next', 'Previous', 'First', 'Last') to update 'rno'. Cyclic navigation is ensured by resetting 'rno' to 1 when it exceeds the maximum record number, and to 5 when it goes below 1 .

User interaction is managed through form submission handled by the 'myform' action targeting 'ems.asp'. ASP script analyzes 'Request.QueryString("cmdbutton")' to detect button actions ('Next', 'Previous', etc.) and alters 'Session("RECNO")' to navigate through records. ASP constructs like conditional statements and session management facilitate this seamless interaction .

Session variables maintain state by storing information that persists across multiple page requests during a user's session. They act as global variables accessible throughout the session, lasting until the session ends, such as when the browser is closed .

The application uses session variables to determine access rights. For instance, checking 'isEmpty(Session("USERS"))' dictates whether the photo gallery is accessible. Only if the variable is not empty, indicating a valid user session, the gallery content is presented; otherwise, access is withheld with a message like "Access Denied" .

The ASP code utilizes the 'isEmpty()' function to check if the 'Session("COUNT")' variable exists. If it returns true, it indicates the user is visiting for the first time, setting 'Session("COUNT")' to '1'. For subsequent visits, the existing count stored in 'Session("COUNT")' is incremented by 1 and updated accordingly .

A session variable can be destroyed using 'Set Session("variablename")=Nothing', 'Set Session("variablename")=null', or by assigning an empty string 'Session("variablename")=""'. Destroying session variables is necessary to free up server resources and manage state effectively, particularly when a session ends or when the data is no longer needed .

Session variables are stored on the server, providing advantages like security and avoiding client's disk space, unlike cookies which can be accessed by the client-side and are limited in size. This enhances confidentiality but also increases server load. Sessions end when the browser closes, which helps maintain shorter-lived state, whereas cookies persist based on expiration time .

High-traffic applications might face challenges such as increased server memory usage due to numerous concurrent sessions, leading to potential performance degradation. Managing session data scalability can be complex, especially if not distributed across servers or managed efficiently, risking state inconsistency during server failures .

The 'isEmpty' function is used to check whether the 'Session("USERS")' variable is empty to determine if a user is authenticated. If 'isEmpty(Session("USERS"))' returns true, it prevents access by denying entry to the photo gallery, contributing to access control and ensuring only authenticated users can view restricted content .

Storing user credentials directly within script logic, such as hardcoding 'username' and 'password' in the ASP validation example, increases the risk of unauthorized access if the code is exposed. This practice is vulnerable to code inspection attacks. Mitigation strategies include externalizing credentials to secure configuration files, applying encryption, and enforcing authentication via secure databases .

You might also like