Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To check if session is available or not

Tags:

c#

asp.net

I tried some code in Application_Error like this

Session["mysession"] = "Some message";

but the problem is sometimes session is not available in Application_Error. So I want to check whether session is available or not.

like image 891
Mohammad Nadeem Avatar asked Sep 22 '10 13:09

Mohammad Nadeem


People also ask

How do you check whether a session is available or not?

You can check whether a variable has been set in a user's session using the function isset(), as you would a normal variable. Because the $_SESSION superglobal is only initialised once session_start() has been called, you need to call session_start() before using isset() on a session variable.

How do you check if there is a session in Java?

Use request. getSession(false) to check if a user has a session. With this method you don't create a new session if you're going to render a view based on if a user is authenticated or not.

How check session is empty in PHP?

If you want to check whether sessions are available, you probably want to use the session_id() function: session_id() returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists).

How can I access session in PHP?

Starting a PHP Session Before you can store any information in session variables, you must first start up the session. To begin a new session, simply call the PHP session_start() function. It will create a new session and generate a unique session ID for the user.


1 Answers

Session doesn't always exist within the context of the current Application_Error. Try the following:

protected void Application_Error(object sender, EventArgs e)
{
    if (Context.Handler is IRequiresSessionState || 
        Context.Handler is IReadOnlySessionState)
    {
         // Session exists
         Session["mysession"] = "Some message";
    }
}
like image 145
djdd87 Avatar answered Oct 05 '22 05:10

djdd87