Little more on sessions?

Discussion in 'PHP' started by ktsirig, Mar 2, 2006.

  1. #1
    Hi,
    I am trying a couple of days now to learn some things on SESSIONS,but I think I am not getting it very clearly.
    I have a PHP page tha contains a form.
    Users enter data in the form, which are then written in a file (the file is created at the time a user enters some data)
    The file is then given as an input to an external program
    The external program runs and gives some results which are then stored in another file (that is created when the external program finishes)
    What I want to do is to somehow create these files and, when the user exits from the browser, then both files that refer to this user, will be deleted.
    Most guys in the forums suggested I use sessions, and go like this :
    1) User "nick" enters data in the form
    2) Temporary file $nick_session_id.FILE is created and passed on to the external program as input
    3) The external program saves the results in another temporary file, say $nick_session_id.RESULTS
    4) When nick sees anything he wants to see in his browser, concerning the results etc and exits, then
    both $nick_session_id.FILE & $nick_session_id.RESULTS will be deleted automatically (so the administrator doesn't have to erase files from the hard disk
    all the time).

    Pls note that I am not describing any kind of user authentication system, ie there is no log in/out in my page.
    Also, the users, as described above,cannot write anything to the input file once they write their data in the form and push "Submit".
    Of course, they cannot write anything to the results file as well, as this file contains the results given by the external program.

    i apologize for the length of my message but I wanted to make it as clear as possible...
    Many thanks!
     
    ktsirig, Mar 2, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Because PHP is server side and requires server requests you can't delete stuff 'when they leave'. Best would be to store the filename with a timestamp of when it was last used. Then run a cron job every couple of hours to delete all files that have not been used for X amount of time.
     
    T0PS3O, Mar 2, 2006 IP
  3. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #3
    I agree to TOPS30.

    Here is a another method that I used in one of my sites.

    Insert this code in the top of your php script.

    ini_set("session.gc_divisor", "6");
    ini_set("session.gc_maxlifetime", "900");
    ini_set("session.gc_probability", "1" );
    
    Code (markup):
    This code will delete the junk session files that were not accessed/modified in previous 900 seconds. Your session files will be cleared out automatically on every 6th page load. You needn't go for cronjob to run on particular interval if this works for you.

    Hope this helps.
     
    vishwaa, Mar 3, 2006 IP