如何在会话超时时显示弹出窗口

I've been currently working on my website and ran into something difficult on my part.

Basically I want to show a popup after the user has been idle for a certain amount of time; I have no idea how to work with AJAX which seems the popular solution.

My site is mostly PHP and CSS.

This is what i'm currently using, but its not as effective as I would like it to be since the pop up only shows up after the screen is refreshed.

if (isset($_SESSION['username']) && isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > 5)) {
    connect();
    if(!connect()) {
        die('Could not connect: ' . mysql_error());
        header( "refresh:3; url=/index.php" );
    }
    mysql_select_db('www');
    $user = $_SESSION['username'];
    mysql_query("UPDATE users SET online='0'
    WHERE username='$user'");

    echo '<script type="text/javascript">';
    echo 'alert("You were logged off due to inactivity");';
    echo '</script>';

    session_unset();
    session_destroy();
}
$_SESSION['last_activity'] = time(); 

To avoid the refresh issue, you would have to turn to ajax for this to work since PHP is purely server side. It could be as simple as doing the testing in a php page, which you refresh with Ajax. Take a look at the jQuery.ajax() function, and use it in conjuction with the javascript

setTimeout ( expression, timeout );
setInterval ( expression, interval );

For information on .ajax see: http://api.jquery.com/jquery.ajax/

For information on setInterval og setTimeout see: http://javascript.info/tutorial/settimeout-setinterval

I think Popup window is not display using PHP OR CSS..

My suggestion is used javascript or jquery..

Used Javascript function setInterval for checking idel time

Both of these answers are right if you think from client side. From server side approach you need to use push mechanisms.This link( pushing data from server to client in php) will help you to understand how to push data (like you want to show popup ) from server. But this solution will be tougher than these two.