Session Variables

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 16

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.
Response.Write(Session("COUNT"))

Checking the Existance of a session variable


===================================
isEmpty( ) function is used for checking the existance of
a variable.This 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

Response.Write("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="valid.asp">
<center>
<br>
<br>
<marquee>www.PaceInfotech.com</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=Request.QueryString("txtuname")
upass=Request.QueryString("txtpass")

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


Session("USERS")="admin"

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

if(isEmpty(Session("USERS"))) then
Response.Write("Access Denied")
else
Response.Write("<center>")
Response.write("<br><br><Marquee><h3>Photo
Gallary</h3></marquee>")
Response.Write("<br><br><img src=car.jpg
height=100 width=100><a href=photo.asp>Cars</a>")
Response.Write("<br><br><img src=actor.jpg
height=100 width=100><a href=photo.asp>Actors</a>")

Response.Write("<br><br><img src=god.jpg
height=100 width=100><a href=photo.asp>God </a>")
Response.Write("<br><br><img src=holly.jpg
height=100 width=100><a
href=photo.asp>Hollywood</a>")
Response.Write("</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=Request.QueryString("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=100l.jpg>
<br>
<br>
<center>
<form name="myform" action="ems.asp">
<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>

You might also like