Results 1 to 2 of 2

Thread: session logout from inactivity

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default session logout from inactivity

    i found this code (http://phpsnips.com/snippet.php?id=39) and it seems to work for others but i get an error in php 5.3
    "Undefined index:"
    on the highlight line

    can anyone give any suggestions as to why its having an issue please

    Code:
    <?php
    # Start a session
    session_start();
    # Check if a user is logged in
    function isLogged(){
        if($_SESSION['logged']){ # When logged in this variable is set to TRUE
            return TRUE;
        }else{
            return FALSE;
        }
    }
    
    # Log a user Out
    function logOut(){
        $_SESSION = array();
        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), '', time()-42000, '/');
        }
        session_destroy();
    }
    
    # Session Logout after in activity
    function sessionX(){
        $logLength = 1800; # time in seconds :: 1800 = 30 minutes
        $ctime = strtotime("now"); # Create a time from a string
        # If no session time is created, create one
        if(!isset($_SESSION['sessionX'])){ 
            # create session time
            $_SESSION['sessionX'] = $ctime; 
        }else{
            # Check if they have exceded the time limit of inactivity
            if(((strtotime("now") - $_SESSION['sessionX']) > $logLength) && isLogged()){
                # If exceded the time, log the user out
                logOut();
                # Redirect to login page to log back in
                header("Location: /login.php");
                exit;
            }else{
                # If they have not exceded the time limit of inactivity, keep them logged in
                $_SESSION['sessionX'] = $ctime;
            }
        }
    }
    # Run Session logout check
    sessionX();
    ?>
    Last edited by ggalan; 07-08-2011 at 07:00 PM.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    That should just be a warning it will occur because in the session array there is no logged for unlogged in users.

    You can avoid the message by chaning it to

    PHP Code:
    if(!empty($_SESSION['logged'])) 
    or maybe

    PHP Code:
    if(@$_SESSION['logged']) 
    Corrections to my coding/thoughts welcome.

  3. The Following User Says Thank You to bluewalrus For This Useful Post:

    ggalan (07-08-2011)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •