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.
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.
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.
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).
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.
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";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With