Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Super easy session question!

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
I'm just hitting a roadblock for what should be something crazy easy.

I've rewritten some ridiculous java code into PHP, and when it's all said in done, in my java code I say
temp = session.getId()
filename = "blah"+temp;

And I have a unique filename for each user based on the session... I want to do the same on PHP... I don't really care what I append to the file, as long as it's guaranteed to be unique at that given time, it can be repeated in the future, just can't have two users writing to the same file for all the obvious reasons... so in short question format, what's the easiest way to grab some session related unique variable?

Follow up question...
for maintenance purposes...
can I have php automatically delete all files in a directory that are more than an hour old? I don't need to automate it, but just a chunk of code that can do that?

Thanks,
Rob
 
Well... you might wanna check out the uniqid function... -gerrygerry

Standard response to one of my posts:
"No where in your entire rantings did you come anywhere close to what can be considered a rational answer. We are all now dumber from having heard that. I award you no points and may God have mercy on your soul."
 
That certainlly works for creating my unique file names... thank you. Now then, anyone have some input to the wiping out of files more than an hour old? More than a day old? I'm not so picky, I just don't want to clutter the whole place up.

-Rob
 
Something like this should work:
Code:
$dir = &quot;<YOUR DIR>&quot;; // The directory without trailing slash
$limit = 60 * 60; // The number of seconds after which a file is deleted
$dirh = opendir($dir);
while ($file = readdir($dirh))
{
    if (($mtime = filemtime($dir . &quot;/&quot; . $file)) === FALSE)
        die(&quot;Unable to stat $dir/$file&quot;);
    if ((time() - $mtime) > $limit)
    {
        if (unlink($dir . &quot;/&quot; . $file) === FALSE)
            die(&quot;Unable to delete $dir/$file&quot;);
    }
}
clearstatcache();
closedir($dirh);
//Daniel
 
Or you could just give them all the same file extension (like .tmp) then run a script (as a service) to delete all .tmp files each night at 3 am (or some other unpopular time)... thats usually the sort of thing I use... it reduces processing time of the main script by removing the garbage collection element. -gerrygerry

Standard response to one of my posts:
&quot;No where in your entire rantings did you come anywhere close to what can be considered a rational answer. We are all now dumber from having heard that. I award you no points and may God have mercy on your soul.&quot;
 
That'd be a nice idea, but I'm writing a program which someone else is going to be implementing... and I don't trust scripts and such to actually get setup properly... I'll add in the readme that they can speed it up by doing that, see if they bother ;).

Otherwise, Danielhozac's answer worked with the exception that I needed to make sure I wasn't unlinking

.. or .

Thanks all.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top