14

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
  • 173,679
  • 45
  • 326
  • 490
Misters
  • 1,287
  • 2
  • 16
  • 28
  • http://www.google.com/#q=checking+if+a+session+variable+exists – Robert Harvey Feb 25 '13 at 21:15
  • If you're going to downvote...leave a comment so that our members can learn. – David L Feb 25 '13 at 21:16
  • The "duplicate" question indicated is from 2008! C#/ASP.NET has undergone numerous changes since then, so while it is the case that this particular answer hasn't changed, it should be completely valid to re-ask the question 5 years later. – leanne Jan 06 '15 at 16:39

3 Answers3

24
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,300
  • 8
  • 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
  • 79,479
  • 96
  • 291
  • 470
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
  • 94,086
  • 102
  • 195
  • 339