SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session Trauma..

    Hi!
    Whenever I close the browser without logout, the session stays as it was before.Is there any way out to automatically destroy session after some time??
    I found the following code on internet but cudnt understand what is $_SESSION['last_seen']
    Code:
    <?php
    session_start();
    if(isset($_SESSION['last_seen']) && (time() - $_SESSION['last_seen']) > 1800) {
        session_destroy();
    }
    else {
        $_SESSION['last_seen'] = time();
    }
    ?>

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Sessions automatically get destroyed by the browser upon exit, unless you set a cookie or changed the session expire time via your php.ini file its not possible for it to still exist.

    As for the code above last_seen is the array key for the session, it sounds to me like you are fairly new to this and i recommend you read through this to get a better understanding of them.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    I commented the code below, which should help.

    Quote Originally Posted by anita_86 View Post
    Hi!
    Whenever I close the browser without logout, the session stays as it was before.Is there any way out to automatically destroy session after some time??
    I found the following code on internet but cudnt understand what is $_SESSION['last_seen']
    Code:
    <?php
    session_start(); //this starts the session
    if(isset($_SESSION['last_seen']) && (time() - $_SESSION['last_seen']) > 1800) {
    //if the "last_seen" variable is set AND the current date/time minus last_seen date/time is greater than 1800 seconds
        session_destroy(); //destroy the session
    }
    else {
        $_SESSION['last_seen'] = time(); //the last_seen session variable is set to the current date/time
    }
    ?>
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  4. #4
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thx for your help.But still cudnt understand wts "last_seen" variable? Should I have an extra field in database to track the session??

  5. #5
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by SgtLegend View Post
    As for the code above last_seen is the array key for the session
    Like i said above last_seen is the array key in the super global $_SESSION, there is no database involvement with sessions unless you want to track a users movement around your website. Again it sounds like you are fairly new to all of this and i do recommend you read the link i gave you as it will help.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  6. #6
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okey SgtLegend.I got it now.Thx a lot


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
  •