How in code to detect if session is enabled rather than just get an error











up vote
1
down vote

favorite












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.










share|improve this question




















  • 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?
    – Lankymart
    10 hours ago












  • Had not considered that - might still throw an error in being undefined though. I'll try it and post back.
    – Vanquished Wombat
    9 hours ago










  • Possible duplicate of Check if an Object exists in VBScript
    – Lankymart
    6 hours ago










  • Lankymart is right. Do if isobject(session) then session("foo") = "bar" end if
    – Daniel Blais
    5 hours ago

















up vote
1
down vote

favorite












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.










share|improve this question




















  • 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?
    – Lankymart
    10 hours ago












  • Had not considered that - might still throw an error in being undefined though. I'll try it and post back.
    – Vanquished Wombat
    9 hours ago










  • Possible duplicate of Check if an Object exists in VBScript
    – Lankymart
    6 hours ago










  • Lankymart is right. Do if isobject(session) then session("foo") = "bar" end if
    – Daniel Blais
    5 hours ago















up vote
1
down vote

favorite









up vote
1
down vote

favorite











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.










share|improve this question















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.







session vbscript asp-classic session-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago









Lankymart

11.4k43896




11.4k43896










asked 18 hours ago









Vanquished Wombat

3,48811034




3,48811034








  • 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?
    – Lankymart
    10 hours ago












  • Had not considered that - might still throw an error in being undefined though. I'll try it and post back.
    – Vanquished Wombat
    9 hours ago










  • Possible duplicate of Check if an Object exists in VBScript
    – Lankymart
    6 hours ago










  • Lankymart is right. Do if isobject(session) then session("foo") = "bar" end if
    – Daniel Blais
    5 hours ago
















  • 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?
    – Lankymart
    10 hours ago












  • Had not considered that - might still throw an error in being undefined though. I'll try it and post back.
    – Vanquished Wombat
    9 hours ago










  • Possible duplicate of Check if an Object exists in VBScript
    – Lankymart
    6 hours ago










  • Lankymart is right. Do if isobject(session) then session("foo") = "bar" end if
    – Daniel Blais
    5 hours ago










2




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?
– Lankymart
10 hours ago






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?
– Lankymart
10 hours ago














Had not considered that - might still throw an error in being undefined though. I'll try it and post back.
– Vanquished Wombat
9 hours ago




Had not considered that - might still throw an error in being undefined though. I'll try it and post back.
– Vanquished Wombat
9 hours ago












Possible duplicate of Check if an Object exists in VBScript
– Lankymart
6 hours ago




Possible duplicate of Check if an Object exists in VBScript
– Lankymart
6 hours ago












Lankymart is right. Do if isobject(session) then session("foo") = "bar" end if
– Daniel Blais
5 hours ago






Lankymart is right. Do if isobject(session) then session("foo") = "bar" end if
– Daniel Blais
5 hours ago














1 Answer
1






active

oldest

votes

















up vote
0
down vote













You should be able to determine if the Session object is present using the VBScript function IsObject().



If IsObject(Session) Then Session("foo") = "bar"


This could also become cumbersome to maintain, but you could take it a step further and build a wrapper for setting the Session keys;



Sub SetSessionValue(key, value)
If IsObject(Session) Then Session(key) = value
End Sub


Usage:



Call SetSessionValue("foo", "bar")


This is just a basic example but should give you are starting point to work from.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204393%2fhow-in-code-to-detect-if-session-is-enabled-rather-than-just-get-an-error%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You should be able to determine if the Session object is present using the VBScript function IsObject().



    If IsObject(Session) Then Session("foo") = "bar"


    This could also become cumbersome to maintain, but you could take it a step further and build a wrapper for setting the Session keys;



    Sub SetSessionValue(key, value)
    If IsObject(Session) Then Session(key) = value
    End Sub


    Usage:



    Call SetSessionValue("foo", "bar")


    This is just a basic example but should give you are starting point to work from.






    share|improve this answer



























      up vote
      0
      down vote













      You should be able to determine if the Session object is present using the VBScript function IsObject().



      If IsObject(Session) Then Session("foo") = "bar"


      This could also become cumbersome to maintain, but you could take it a step further and build a wrapper for setting the Session keys;



      Sub SetSessionValue(key, value)
      If IsObject(Session) Then Session(key) = value
      End Sub


      Usage:



      Call SetSessionValue("foo", "bar")


      This is just a basic example but should give you are starting point to work from.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        You should be able to determine if the Session object is present using the VBScript function IsObject().



        If IsObject(Session) Then Session("foo") = "bar"


        This could also become cumbersome to maintain, but you could take it a step further and build a wrapper for setting the Session keys;



        Sub SetSessionValue(key, value)
        If IsObject(Session) Then Session(key) = value
        End Sub


        Usage:



        Call SetSessionValue("foo", "bar")


        This is just a basic example but should give you are starting point to work from.






        share|improve this answer














        You should be able to determine if the Session object is present using the VBScript function IsObject().



        If IsObject(Session) Then Session("foo") = "bar"


        This could also become cumbersome to maintain, but you could take it a step further and build a wrapper for setting the Session keys;



        Sub SetSessionValue(key, value)
        If IsObject(Session) Then Session(key) = value
        End Sub


        Usage:



        Call SetSessionValue("foo", "bar")


        This is just a basic example but should give you are starting point to work from.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 3 hours ago

























        answered 3 hours ago









        Lankymart

        11.4k43896




        11.4k43896






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204393%2fhow-in-code-to-detect-if-session-is-enabled-rather-than-just-get-an-error%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)