1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Checking Session presence in ASP

Discussion in 'C#' started by smo, Apr 6, 2007.

  1. #1
    Just started using ASP..

    At the starting of the page we need to check that only logged in member should access.

    In PHP to check if session exist or not we use isset() . Similarly what is the equivalent function for checking session in ASP?

    There is a function isnull but I doubt is it a better way for checking session presence?

    IsNothing(Session("UserName")) this function is giving me error message.

    I will write a common header file and use it for different files inside member area.
     
    smo, Apr 6, 2007 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    if obj is Nothing should work, but a trick I always use in asp (and .NET) is the trim. trim auto-casts as String and removes spaces so you could do:

    If trim(Session("UserName")) = "" then
    ' Redirect to Login page
    end if
     
    ccoonen, Apr 6, 2007 IP
  3. tonyrocks

    tonyrocks Active Member

    Messages:
    1,574
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    88
    #3
    You should look at ASP.NET 2.0. It has built in features to create logged in views for websites...and user/role management. Good stuff to check out. session timeouts can be controlled through the web.config
     
    tonyrocks, Apr 6, 2007 IP
  4. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #4
    tonyrocks: yes, the memberships and roles are a beautiful thing if you don't want a custom authentication system :) - they make it pretty sound and include strongly typed elements for you to directly hook into!
     
    ccoonen, Apr 6, 2007 IP
  5. tonyrocks

    tonyrocks Active Member

    Messages:
    1,574
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    88
    #5
    OH yes! I dig it completely. I've even been able to use MS Access as the user/role provider (since some hosting companies will not let you use SQL SErver Express...which handles users/roles by default)
     
    tonyrocks, Apr 6, 2007 IP
  6. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #6
    I think you are bluffing my friend... to setup the standard memberships it requires Stored Procedurs which are only offered in SQL Server or SQL Server express (not access) - but hell... I could be wrong I haven't touched access since the version included in office pro 2003 :) Please enlighten me good sir.
     
    ccoonen, Apr 6, 2007 IP
  7. smo

    smo Well-Known Member

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #7
    I forgot to mention that I am not searching for solution in Dot Net. It is in pure ASP. Also I am not looking toward any database here. I will authenticate and then create session and redirect to member area.

    Now inside member area I have to ensure session(“userid”) is valid or not.

    Checking for a null testing or testing like If trim(Session("UserName")) = "" then is enough ?
     
    smo, Apr 6, 2007 IP
  8. webcosmo

    webcosmo Notable Member

    Messages:
    5,840
    Likes Received:
    153
    Best Answers:
    2
    Trophy Points:
    255
    #8
    I am a C# person. Based on that, you have to also check for Null value(Nothing in asp I guess)
    so you have to do something like this(in C# for example):
    try
    {
    if(Session["userName"]==null)
    {
    //do something
    }
    else if(Session["userName"].ToString().Trim()=="")
    {
    //do something
    }
    else
    {
    String userName=Session["userName"].ToString();
    //check validity
    }
    }
    catch(Exception ex)
    {
    //fetch exception
    }
     
    webcosmo, Apr 7, 2007 IP