3

If I set

@ENABLESESSIONSTATE = false

then

session("foo") = "bar"

then the result is

Microsoft VBScript runtime error '800a0114'
Variable is undefined 'Session'
... file and line no

It would usually indicate a mistaken assumption regarding program flow and I would trace and fix the issue.

However, in a specific set of circumstances I have a case where a piece of code that uses session is always invoked first on every page request. It is to do with performance monitoring.

This code includes a fork - if the user has a session we go one way, if not we go another.

But of course where the users session is absent because we introduced some code that runs with session disabled, we get the crash.

I could solve it with

on error resume next 
session("foo") = "bar"
if err.number <> 0 then

   ' do the no-has-session fork

else

   ' do the has-session fork
end if
on error goto 0

But I wondered if there is a less hacky approach.

user692942
  • 16,398
  • 7
  • 76
  • 175
Vanquished Wombat
  • 9,075
  • 5
  • 28
  • 67
  • 2
    You could wrap the logic in a function to make easier to work with, but as for the approach of checking using OERN this would be what I would do. Wonder though, could you not perhaps do a `IsObject(Session)` check? – user692942 Nov 08 '18 at 17:18
  • Had not considered that - might still throw an error in being undefined though. I'll try it and post back. – Vanquished Wombat Nov 08 '18 at 17:47
  • 1
    Possible duplicate of [Check if an Object exists in VBScript](https://stackoverflow.com/questions/4100506/check-if-an-object-exists-in-vbscript) – user692942 Nov 08 '18 at 21:35
  • 1
    Lankymart is right. Do `if isobject(session) then session("foo") = "bar" end if` – DanB Nov 08 '18 at 21:48
  • [Deleted my answer](https://stackoverflow.com/a/53217850/692942) because it obviously has no value now *(not sure the down-vote was justified)*. It was only hastily put together on my mobile phone at 23:45 but hey no biggy. – user692942 Nov 09 '18 at 12:17
  • @VanquishedWombat Wouldn't worry about the close vote, it does say "Possible duplicate" and to [close it takes 5 close votes](https://stackoverflow.com/help/duplicates) anyway. – user692942 Nov 09 '18 at 12:46
  • 1
    Ok didn't know that thanks. Just wanting to leave a useful footprint for people passing this way in 2030 when ASP still isn't dead! – Vanquished Wombat Nov 09 '18 at 13:09
  • @VanquishedWombat no problem, try not to edit answers into the original question or the context of the existing answers may be lost. Questions are for questions and answers are for answers, don't confuse the two. Also, using a question as a sounding board for the mods isn't recommended, instead [use a custom flag](https://stackoverflow.com/help/privileges/flag-posts) or raise a "support" post on [meta]. – user692942 Nov 09 '18 at 13:38

1 Answers1

4

For the sake of having this question show an accepted answer....

Regarding the suggestions for use of the isObject() approach, the results are not good. The following asp...

<%@EnableSessionState=False%>
<% option explicit

response.write "session enabled=" &  IsObject(Session) 
response.end

%>

results in

Microsoft VBScript runtime error '800a01f4'

Variable is undefined: 'Session'

/errortest.asp, line 6

Therefore it would appear that the session object is marked as truly not having been declared.

My conslusion is to construct a function as below.

<%@EnableSessionState=False%>
<% option explicit

response.write "session enabled=" &  isSessionEnabled()  ' <-- returns false 
response.end

function isSessionEnabled()
    dim s

    isSessionEnabled = true     ' Assume we will exit  as true - override in test 
    err.clear()                 ' Clear the err setting down
    on error resume next        ' Prepare to error

    s = session("foobar")       ' if session exists this will result as err.number = 0 

    if err.number <> 0 then 
        on error goto 0         ' reset the error object behaviour                  
       isSessionEnabled = false ' indicate fail - session does not exist.
       exit function            ' Leave now, our work is done
    end if
    on error goto 0             ' reset the error object behaviour
end function                    ' Returns true if get to this point

%>

This is then used as

If isSessionEnabled() then
    ' do something with session 
else
    ' don't be messin with session.
end if
Vanquished Wombat
  • 9,075
  • 5
  • 28
  • 67