Execute code when window closes?

Discussion in 'PHP' started by Andy Peters, May 4, 2007.

  1. #1
    I'm not even sure if this is possible, but basically, when a user leaves my site, i'd like to execute some PHP code (say, delete from mysql database for example)
    Is this even possible?
     
    Andy Peters, May 4, 2007 IP
  2. reza56

    reza56 Active Member

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #2
    This is not possible. but you can use cron jobs to do some works like it.
     
    reza56, May 4, 2007 IP
  3. TVCokeCan

    TVCokeCan Peon

    Messages:
    1,208
    Likes Received:
    147
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The best idea here would be to set a session and then set it to class a user as exited if he does not change page or do anything within X mins, somewhat like what VB does with user logged in.
     
    TVCokeCan, May 4, 2007 IP
  4. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Why you want to delete the data after user quit from your site? If those data which are useless don't store in database store it in cookies deleting data from database each and every time time you are creating pressure to your mysql server. If number of user is less for your website then it is ok. But for mass user deleting data from database is not good what I think.
     
    Subikar, May 4, 2007 IP
  5. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #5
    That depends on what he is storing and who he wishes to have access to the information. Though there are better ways of doing it, say he wants to ensure that a user does not perform an action three times within a browser session based on a user's IP... this information would be pretty useless to store in a database for long term, however it would be unreasonable to store it in a cookie because this would open the application up to cookie tampering and would therefore render the restriction useless. As a solution he could then store the information in the database and remove it when the user exits out. Though a temporary table, or some equivalent may be more suitable.
     
    Louis11, May 5, 2007 IP
    GTech and Subikar like this.
  6. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #6
    You can do that using one of those "exit popup" javascripts and initiating an XMLHTTP request to a php file that does what you want to do
     
    Kendothpro, May 5, 2007 IP
  7. Andy Peters

    Andy Peters Peon

    Messages:
    430
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hmm, i actually think cron would be what i need, but i've never used it before so i'll have to go out and learn it ;)

    I've realised that simply having it on exit wouldn't work because i need my users to refresh the window:
    http://www.emailondemand.info :)
     
    Andy Peters, May 5, 2007 IP
  8. kaisellgren

    kaisellgren Well-Known Member

    Messages:
    472
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #8
    You could try make javascript to forge http request after window close to a .php file. This works IF visitor/user has JS enabled.
     
    kaisellgren, May 5, 2007 IP