Session 如何在代码中检测会话是否已启用,而不仅仅是获取错误

Session 如何在代码中检测会话是否已启用,而不仅仅是获取错误,session,vbscript,asp-classic,session-variables,Session,Vbscript,Asp Classic,Session Variables,如果我设定 @ENABLESESSIONSTATE = false 然后 那么结果是 Microsoft VBScript运行时错误“800a0114” 变量为未定义的“会话” ... 文件和行号 它通常表明关于程序流的错误假设,我将跟踪并修复该问题 然而,在一组特定的情况下,我会遇到这样一种情况:每次页面请求都会首先调用使用会话的代码。这与性能监控有关 这段代码包含一个fork-如果用户有会话,我们就走一条路,如果没有,我们就走另一条路 但当然,如果由于我们引入了一些在禁用会话的情况下运行的

如果我设定

@ENABLESESSIONSTATE = false
然后

那么结果是

Microsoft VBScript运行时错误“800a0114”
变量为未定义的“会话”
... 文件和行号

它通常表明关于程序流的错误假设,我将跟踪并修复该问题

然而,在一组特定的情况下,我会遇到这样一种情况:每次页面请求都会首先调用使用会话的代码。这与性能监控有关

这段代码包含一个fork-如果用户有会话,我们就走一条路,如果没有,我们就走另一条路

但当然,如果由于我们引入了一些在禁用会话的情况下运行的代码而缺少用户会话,那么就会出现崩溃

我可以用计算机解决它

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
出错时继续下一步
会话(“foo”)=“bar”
如果错误号为0,则
'是否没有会话分叉
其他的
'是否有会话分叉
如果结束
错误转到0

但我想知道是否有一种不那么老套的方法。

为了让这个问题显示出一个公认的答案

关于使用isObject()方法的建议,结果并不好。以下asp

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

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

%>

您可以将逻辑封装在一个函数中,使其更易于使用,但对于使用OERN进行检查的方法,我将这样做。但是,您可能不想做一个
IsObject(Session)
检查吗?没有考虑到这一点-可能仍然会在未定义时抛出一个错误。我会试试,然后再发回。可能是Lankymart的复制品是对的。如果isobject(session)那么session(“foo”)=“bar”结束,如果Ok不知道,谢谢。只是想在2030年ASP还没有死的时候,为通过这条路的人们留下一个有用的足迹!
<%@EnableSessionState=False%>
<% option explicit

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

%>
<%@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

%>
If isSessionEnabled() then
    ' do something with session 
else
    ' don't be messin with session.
end if