Run Php Script When Session Is About To Expire

Discussion in 'PHP' started by shiva_rnm, Apr 24, 2009.

  1. #1
    Hi,
    I'm trying to run into mysql database whenever users login and logout. I've manage to register when users login and when the users logout when clicking on the Logout link. The next step is the following:

    How could I detect when a session is about to expire so I can run a php script that inserts the logout info into the database?

    Say everyone gets kicked out of the session when the system has been idle for x hours. So after x hours of the system been unused the system has to login again. Just before the session expires how can I write to my db who was loged out?

    Thanks in advance.
     
    shiva_rnm, Apr 24, 2009 IP
  2. nayes84

    nayes84 Member

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    I guess you need to check on it your self.
    like having a countdown start from the time your code established new session and its length is the time till your sessions expire.
    I think this would be made by javascript but session data could be sent to the javascript code by php
     
    nayes84, Apr 24, 2009 IP
  3. ryandanielt

    ryandanielt Well-Known Member

    Messages:
    1,797
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Funny but no it wont work.

    What I normally do is I log the IP address of where they last logged on from and I create a script that will run the next time they visit. So basically when they load the page the script will load and check to see if they have logged out of not and if they haven't they will get a message saying that there session has expired and to re login.

    I think this is the most simplest way of doing it rather then using cron jobs to load the script itself all the time. This method puts no extra loads on the server and keeps your site loading efficiently.


    Cheers!
     
    ryandanielt, Apr 24, 2009 IP
  4. nayes84

    nayes84 Member

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    why do you think that?
    if so then how about the sites that have this functionality? if they don't use javascript what else can be used?

    I think you misunderstood what he is asking.
    he needs a script to warn users before their session expires. not after it already expired.
    also if he used javascript it is not going to make any load on the server because it runs on the client side (user's browser)
     
    nayes84, Apr 24, 2009 IP
  5. ryandanielt

    ryandanielt Well-Known Member

    Messages:
    1,797
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    185
    #5

    Nope didn't misunderstand anything. The only way you can use javascript to auto log someone out like he is asking is to use ajax with jquery. The reason for this is because he wants to log everything about the logout into the database which requires PHP. Say the user closes the window, how do you expect the site to give them a warning as you say? Well my method you will need to setup a cron job that will run the script every so often or you will have to load a certain PHP script every time to determine the length of how long the person has been idle. If you have it set to say 20 minutes then the PHP script will determine the last time they were idle and if its been more then 20 minutes it will log them out making them have to log back in.

    Just goes to show not everyone know what they are talking about!


    If you need any further help I can assist you in the creation on this script for a small cost. I normally base my projects around $10 USD per hour and this script will take about 2 hours to complete give or take.


    Cheers!
     
    ryandanielt, Apr 24, 2009 IP
  6. nayes84

    nayes84 Member

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    I was talking about when one needs to make a pop up message to tell users to refresh their page before their session times out. I had made a system like before using php and javascript even before ajax had been known.
    so I thought he is asking the same thing.

    concerning his question. the thing is very simple that it doesn't need cron job. a cron job would also cause some load on the server.
    just storing time stamp for the date to log out and making login script just check on the time stamp is enough to achieve the job. if I got to make a script for that I would make the script for free cause it'll not take more than 10 minutes.:D
     
    nayes84, Apr 24, 2009 IP