Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session Variable Dilemma 1

Status
Not open for further replies.

Scanlan

Technical User
Sep 7, 2001
35
0
0
US
Hello all,

Hoping you can give me some opinions on how best to handle some session variables. I don't want to be too long-winded about this, so I am going to skip the long drawn out explanation of what I am trying to do and just boil this down to my dilemma.

Say I have a web application that allows users to do all sorts of things with no particular order or work flow. Sometimes, a user is going to be coming from a page where they entered some stuff, and they may want that stuff carried over.

So say I have 3 session variables (one holds a flag, the other two hold data). I set the flag to 0 at log-in and I set it to 1 when they hit the page where they enter relevent data. Now, if they want to go onto to page where they will use said data, I want to put a form that will set the two session variables that hold the data and set the flag to 1. Then they can click a submit button to take them to the page where they will use the data. After they use the data, I want to destroy the two data variables. (Which I think I can do by doing a Contents.Remove for those variables, or something like that. I am hazy on the details but I can work that out.)

My dilemma is what to do with those two variables (and the flag, which will have to get set back to 0) if the user goes anywhere else other than the relevent page. I have three possible solutions that I need advice on:

1. Is there a way to create those two session variables only if the form is submitted?
2. I could put code on the top of every other page to check the flag, and if it is set to 1, to set it to 0 and destroy the two other variables, but I have alot of pages, so this would be a major change.
3. Is there a way to set a timeout value just for those two variables so that just they will timeout after 5 or 10 minutes?

Any advice will be greatly appreciated.

Thanks,
Ann
 
session.timeout = valueInMinutes

will set your default session timeout --

But you can also only set the session variables if they hit submit and here's how:

Set the action of the form to itself -- a recursive form, put a hidden form variable there that you can check for a value to determine if the load of the page is a recursive one, and then you'll have some stuff at the top of the page that looks like this:

<%
if request.form(&quot;recursive&quot;) = &quot;1&quot; then 'check for reload
dim var1, var2
var1 = request.form(&quot;var1&quot;)
var2 = request.form(&quot;var2&quot;)
session(&quot;var1&quot;) = var1
session(&quot;var2&quot;) = var2
session(&quot;flag&quot;) = 1
response.redirect(&quot;destination.asp&quot;)
end if
%>

As far as resetting the flag to 0, I think you're going to have to do that no matter what -- otherwise, how would it get reset? My advice for that would be to create an include file that would be generic enough for you to include it on every page, and then set some local variable that you could access on those pages to know whether or not do something....

&quot;reset.inc&quot;
<%
dim localFlag
localFlag = 0
if session(&quot;flag&quot;) = 1 then
session(&quot;flag&quot;) = 0
localFlag = 1
end if
%>

Then include it on the top of the pages like:
<!-- #include file=&quot;reset.inc&quot; -->

Where you could then just access the value of 'localFlag' on the pages in question, and then access your session variables if you need to.

hope that helps! :)
Paul Prewett
penny.gif
penny.gif
 
Sorry, forgot to show you what to do with your form on the original page:

<form name=theForm method=post action=&quot;samePage.asp&quot;>
<!--
put in your normal form elements, but then be sure to include your hidden one that will tell you whether the page is a reload or a first load
-->
<input type=hidden name=recursive value=1>
</form>

You should be all set up after that.

:)
paul
penny.gif
penny.gif
 
A recursive form-- I never would have thought of that. I like that idea a lot, since there's not form data to transmit. Very clever.

I am going to have to think on how best to handle the flag. I am a little unsure why you recommend using the local variable. Why wouldn't I just be able to use the session variable on the relevent pages? Perhaps I am missing something.

Thank you for a most excellent and informative post.

Thanks,
Ann
 
I just prefer local variables -- Although it's generally considered good programming practice, it's just a design preference, but above that, it keeps you from having to type session(&quot;soAndSo&quot;) every time you want to access a variable.

:)
penny.gif
penny.gif
 
If i'm not mistakin you don't have to use a hidden element

Here is part of a code i made:

Code:
<%
if Request.Querystring(&quot;no&quot;) = &quot;&quot; then

%>
	<html>
	<head>
	<title>Login</title>
	</head><body bgcolor=&quot;#000000&quot; text=&quot;#FFFF00&quot;><p><br></p>
	<p> </p>
	<p> </p>
	<p> </p>
	<p><br><br></p>
	<form name=&quot;frm1&quot; method=&quot;post&quot; action=&quot;login.asp?no=1&quot;>
	<table width=&quot;60%&quot; height=&quot;100&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; align=&quot;center&quot; bordercolor=&quot;#000000&quot; bgcolor=&quot;#666666&quot;>
	<tr>
	<td align=&quot;right&quot;> <font size=&quot;3&quot;><b>Votre adresse de courriel:</b></font> 
	</td>
	<td align=&quot;center&quot;> 
	<input type=&quot;text&quot; name=&quot;txtemail&quot; size=&quot;30&quot; maxlength=&quot;30&quot;>
	</td>
	</tr>
	<tr> 
	<td align=&quot;right&quot;> <font size=&quot;3&quot;><b>Votre mot de passe:</b></font> </td>
	<td align=&quot;center&quot;> 
	<input type=&quot;password&quot; name=&quot;txtpass&quot; size=&quot;30&quot; maxlength=&quot;15&quot;>
	</td>
	</tr>
	<tr> 
	<td colspan=&quot;2&quot; height=&quot;36&quot;> 
	<div align=&quot;center&quot;>
	<input type=&quot;submit&quot; name=&quot;send&quot; value=&quot;Entrer&quot;>
	</div>
	</td>
	</tr>
	</table>
	<form>
	</body>
	</html>

<%

elseif Request.Querystring(&quot;no&quot;) = &quot;1&quot; then
dim temp, VOIR
VOIR = CHECKITOUT(temp)
and so on....

Hope it'll help

Have fun....

Sharky99 >:):O>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top