Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 12th, 2008, 09:45 PM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default session variable not populating

I am attempting to grab the session variable username and populate on my default.asp after the user logs in the following manner:

welcome [username],

it seems that some of the time the variable doesnt populate. once the user logs out (session.abandon), and log back in it works.

Any ideas?

Here is what I have in my global.asa

Sub Session_OnStart
application.lock
application("activevisitors")=application("activev isitors")+1
application.unlock
username=Request.Form("username")
password=Request.Form("password")
session("username")=username
session("password")=password
End Sub

Sub Session_OnEnd
application.lock
application("activevisitors")=application("activev isitors")-1
application.unlock
End Sub

and here is what is in my default.asp to invoke the session variable:

<% username = session("username") %>
<% password = session("password") %>

Welcome <%= username %>,

any ideas why this is not grabbing the session variable each time, I mean anything obvious that I am not doing or that I am doing wrong? Do I need something in the Session_OnEnd

thanks for reading this.
:)
__________________
keep your kneeze in the breeze
 
Old March 12th, 2008, 10:01 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

very strange - im confused. is this global.asa file running trouble free?

I dont think you can use the request object at all in this file. Additionaly IMO you are missing sone reccomended syntax for a global.asa file - There is loads of info around on this file, have a read of good global.asa practices.

Wind is your friend
Matt
www.elitemarquees.com.au
 
Old March 12th, 2008, 10:19 PM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well it seems to be running okay but obviously im missing a few things or my page would be working as expected. i will take your advice and keep reading the global.asa info.

btw here is the entire asa file

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
application("activevisitors")=0
Application("strCon") = "Provider=SQLOLEDB.1;Data Source=computername;Initial Catalog=table;User ID=username;pwd=password;"
End Sub

Sub Application_OnEnd
End Sub

Sub Session_OnStart
application.lock
application("activevisitors")=application("activev isitors")+1
application.unlock
username=Request.Form("username")
password=Request.Form("password")
session("username")=username
session("password")=password
End Sub

Sub Session_OnEnd
application.lock
application("activevisitors")=application("activev isitors")-1
application.unlock
End Sub

</SCRIPT>

*****very strange behavior indeed, when i removed the

username=Request.Form("username")
password=Request.Form("password")

from the file the variable doesnt work at all.. but when i put it back it works after i log out and back in!**** so confusing
 
Old March 12th, 2008, 10:56 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

You do realize session variables can be set anywhere?

Why try to set these in this file? Just post your login form to any page, validate the user then put your:

username=Request.Form("username")
password=Request.Form("password")

after they are validated. Your session vaiables will now be set and persist until the end of the session.

Additionaly - are you sure your global.asa file is working? to make sure:

>cloase all browswers
>Place this in the global.asa:
session("testVar") = "somevalue" & "=="
>now go to a page on your site. Do you see?:
somevalue
or do you see?:
==



Wind is your friend
Matt
www.elitemarquees.com.au
 
Old March 12th, 2008, 10:59 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

sorry I made a mistake in my previous post change:

username=Request.Form("username")
password=Request.Form("password")

to

session("username")=trim(Request.Form("username"))
session("password")=trim(Request.Form("password"))


Wind is your friend
Matt
www.elitemarquees.com.au
 
Old March 12th, 2008, 11:11 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

IMO only use a global.asa if you need to. Not all systems need one. Example - Here is the global.asa from the system I am working on at the moment. The only reasons I use one here is:

> extend the session timout for this web system only (can be done in IIS of course)
> set the LCID to ensure a dd/mm/yyyy format is used no matter what the server is set to
  - this is very handy, no more date format problems!
> to ensure I have a session based hit couter
  -ONE entry is made to the database for each unique visitor, as you can see in the code.

Anhow here is the cut n paste code:

<script runat=server language=VBScript>
Sub application_onStart

end sub

sub session_onStart
    Session.Timeout = 40
    Session.LCID = 3081
    application.lock
     Set Conn = Server.CreateObject("ADODB.Connection")
     GetConnectionString = "Provider=SQLOLEDB.1; Password=XXX; Persist Security Info=True; User ID=XX; Initial Catalog=myDBName; Data Source=myMachineName"
     Conn.Open GetConnectionString
     SQL = "update hits SET hits=(hits+1) WHERE id=1;"
     Conn.execute(SQL)
     Conn.close
      set Conn = nothing
    application.unlock
End Sub

Sub Session_OnEnd

End Sub

Sub Application_OnEnd

End Sub

</script>

Wind is your friend
Matt
www.elitemarquees.com.au
 
Old March 13th, 2008, 09:54 AM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

okay after following your instructions EXPLICITLY :), I did get it working. I was putting the
<%
dim username
dim password
username=Request.Form("username")
password=Request.Form("password")
session("username")=username
session("password")=password
%>

after the entire code block where it was error checking and validating, i slipped the above code in immediately after validation and it worked just as you said it would. thanks mat41 for being patient and giving me the guidance I needed. I will read up in the global.asa file now.

-=ph0neman=-

keep your kneeze in the breeze
 
Old March 13th, 2008, 10:49 AM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mat41
 >cloase all browswers
>Place this in the global.asa:
session("testVar") = "somevalue" & "=="
>now go to a page on your site. Do you see?: somevalue
or do you see?: ==
Once I took care of the other issue I did as you suggested and tried the global.asa "somevalue" test and I saw both the == and the "somevalue". :)

keep your kneeze in the breeze
 
Old March 13th, 2008, 04:41 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Cool glad you got it working.

IME you can not set session variables inside the global.asa file after a user has already visited your site during the same session. This file gets run once as soon as the users session starts, it does not and can not be referenced after this in the same session. (mmmm that reads a bit wierd sorry, im sure you know waht I mean though)

A good day to you. After a quick look at my emails, im off for the day...

Wind is your friend
Matt
www.elitemarquees.com.au





Similar Threads
Thread Thread Starter Forum Replies Last Post
Session variable or other soulfly73 Classic ASP Basics 4 December 4th, 2008 12:52 PM
session and variable... badboy1 ASP.NET 3.5 Basics 1 August 3rd, 2008 09:14 AM
Populating a Select Control from an XSL:Variable pcahaus XSLT 1 October 31st, 2007 04:21 AM
Session variable anuradha80 General .NET 3 November 4th, 2004 12:39 AM
Session Variable mcdowell BOOK: Beginning ASP 3.0 5 June 20th, 2004 05:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.