14

在php中我曾经使用

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

但是我在 asp.net 中这样做吗?我是说。什么代码可以告诉我们是否设置了会话

例如:asp.net c#

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

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

}
4

3 回答 3

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

您可以像这样测试您的会话值:

if (Session["USER"] != null)
{
    //do something interesting
}
于 2013-02-25T21:15:18.957 回答
2

如果您想检查会话变量的存在,这很好:

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

尽管可以在 asp.net 应用程序中禁用会话状态,但这种情况很少见。

于 2013-02-25T21:13:53.797 回答
2

从 php 方面来看,cince isset函数

确定变量是否已设置且不为 NULL。

只需检查此会话是否null喜欢:

if(Session["USER"] != null)
{
  // Do something
}
于 2013-02-25T21:14:52.063 回答