15

In php i used to use

session_start();
if(isset(SESSION["user"]))
{
   //session is set
}
els{
    // there is no session 
}

but do i do that in asp.net? I mean. What code can tells wheather a session is set or not

ex: asp.net c#

//login.aspx
SESSION["USER"];

//user_profile.aspx
if(SESSION["USER"])// how do i validate that??
{

}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Misters
  • 1,337
  • 2
  • 16
  • 29

3 Answers3

25
SESSION["USER"]; //this should throw an error since it's not setting a value and not a method.

You can test your session values like this:

if (Session["USER"] != null)
{
    //do something interesting
}
jTC
  • 1,340
  • 9
  • 17
2

If you want to check for the existance of a session variable this will be fine:

if(Session["USER"] != null)
{
    //If you get here a session variable "USER" exists...
}

Though it is possible to disable session state in an asp.net application it is very rare to see that.

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
2

From php side, cince isset function

Determine if a variable is set and is not NULL.

Just check if this session null or not like:

if(Session["USER"] != null)
{
  // Do something
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364