Hi,


I want to know, how to close a session when a user is idle (around 'n' time) in asp.net.

Recommended Answers

All 6 Replies

You can make use of SessionState propety of Web.config under <System.Web> section.

i.e.

<system.web>  
<sessionState timeout="30"></sessionState>  
</system.web>  

<system.web>
<sessionState timeout="30"></sessionState>
</system.web>

try this also

session_start();
if (isset($_SESSION['LAST_REQUEST_TIME'])) {
    if (time() - $_SESSION['LAST_REQUEST_TIME'] > 180) {
        // session timed out, last request is longer than 3 minutes ago
        $_SESSION = array();
        session_destroy();
    }
}
$_SESSION['LAST_REQUEST_TIME'] = time();

The idle timeout is based from the last time the server received a request from a client with a given session ID.
You can use Session.Abandon() to force a log off

or for this you can give a trimout for a session in webconfig file for e.g.

<system.web>  
<sessionState timeout="30"></sessionState>  
</system.web>

In above code after 30 minutes Session will be closed . after this perticulaer session redirect user to home page or you can force him to login again.

Thank you for your response.

I am new to this. Could you explain in details. where should I place those file like.....

you don't need to put this file. Web.config file is already there in root directory. you just need to put the session related code in web.config file under <System.Web> tag.

Thank you for your response.

I am new to this. Could you explain in details. where should I place those file like.....

there is web.config file in your application open it and copy code form here and pest in between<System.Web></System.Web>

if got solution dont forgot to mark as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.