Jump to content

I want to expire a file when the browser leaves


cafetorium

Recommended Posts

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.