cafetorium Posted March 3, 2008 Share Posted March 3, 2008 Hi, I'm a newbie to PHP, but I'm loving it more and more every day. I apologise if this question has been answered before, but this forum is huge and it's difficult to find some things. Here's my question: When a person visits my page, it creates a number of simple text files. They're actually page redirectors which help to cloak some affiliate links from ads on my page. My problem is that the files remain after the browser has left. I had a script that cleaned up every so often (via Cron), but that meant that if a browser was still looking at my site when the files were deleted, those links would fail. Then I heard about PHP sessions. I'm really not sure how to use them, but they sound as if they could help. Is it possible for the files to be created within a Session, and then when the Session closes (ie the browser goes away from my site), the files get deleted? If so, how do I do this? I can't work out how to trigger when a Session finishes. Thanks in advance, Phil Quote Link to comment Share on other sites More sharing options...
priti Posted March 3, 2008 Share Posted March 3, 2008 yes you can use session to identify when you want to delete the files i.e when user has finally done with his work .It is something like your Logout facility .... so on logout you can find that user is pretending to leave the page so you can destroy session my unset($_SESSION) or session_destroy(). before this you can clean up those text file too. Quote Link to comment Share on other sites More sharing options...
aschk Posted March 3, 2008 Share Posted March 3, 2008 Ok, what you've done already sounds rather similar to sessions but without the technical processes behind instantiation and deletion. A session is just a persistent data storage area. Most PHP installations have a default of 20 minutes on a session. Which means so long as the user continues to use your website within that 20 minute limit period since they last used it, the session will continue. To use a session you just need to do the following <?php session_start(); $_SESSION['theNameOfMyVariableHere'] = "something i want to put inside the session"; echo $_SESSION['theNameOfMyVariableHere']; ?> 1 thing to note is that "session_start()" MUST be called on each page that you want to use the session information. Also see: http://www.php.net/session Quote Link to comment Share on other sites More sharing options...
cafetorium Posted March 3, 2008 Author Share Posted March 3, 2008 Thanks for your help, but I'm still not sure about this. There's no concept of logging in and out of my site. It's for any visitor viewing my pages. The files get created when the page is first displayed, and need to get deleted when the visitor stops viewing my site. If the Session expires before the visitor has left, then the redirector files will have disappeared and the links will fail. I don't want the redirectors to be permanent because my script rotates different affiliate links into the page every time it's viewed. Perhaps Sessions aren't quite what I need here. I guess what I need is some means of detecting when the visitor navigates away from my site, which will then trigger the deletion. However, it would all fail again if the visitor then used Back in their browser and it displayed a cached version of the page. Perhaps I need to rethink the entire concept. Added later: I just read elsewhere that a session expires when the visitor closes their browser, so it sounds like there's no problem with the session expiring prematurely. But is there some trigger event that tells my system that the session has expired? Phil Quote Link to comment Share on other sites More sharing options...
priti Posted March 3, 2008 Share Posted March 3, 2008 before leaving a page you can do a javascript pop up "Are you sure you want to switch to next page" or so this way you can identify the event and then you can delete the needed files. does it help?? Quote Link to comment Share on other sites More sharing options...
aschk Posted March 3, 2008 Share Posted March 3, 2008 I honestly do think sessions are what you need. Try them out, i think you'll find they work better than writing to files (especially as they can deal with multiple-users at once). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.