homepage Welcome to WebmasterWorld Guest from 54.242.231.109
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / HTML
Forum Library, Charter, Moderators: incrediBILL

HTML Forum

    
Getting browser session id in php to append to file name.
nelsonm




msg:4266858
 4:07 pm on Feb 14, 2011 (gmt 0)

Hi all,

I have a html.php page that creates a server side file containing user specific html data that is either printed or emailed when the user clicks on a html button to print or email.

The problem is that there is nothing to stop another user from overwriting the file before the previous user gets a chance to print or email the html file data.

I need to attach some sort of unique session id to the file name so users around the world don't overwrite other users html data files.

I understand that browsers create a unique session id for each user. I have searched for info on the subject but i'm still a little confused on what to use and where to get it.

1. How do i access that id in php so i can attach it to the file name?

2. Maybe a better question is... what kind of ID (IP address, browser session ID,...) is better to use as a session ID?

 

incrediBILL




msg:4266928
 5:54 pm on Feb 14, 2011 (gmt 0)

What you need to do is start a PHP session and use the session ID to identify your file.

Here's the docs on PHP session: [php.net...]

Alternatively, you could just randomly name the file using tempnam() [php.net] and that also solves the problem as each time you create the file the name is completely unique.

Either way you go, you will need to create some cron task to clean up all these files once a day because there will be failures which result in a backlog of old files clogging up the system.

nelsonm




msg:4267141
 2:43 am on Feb 15, 2011 (gmt 0)

thanks...

I decided to generate the file name using the php random number generator because the tempnam() function kept giving me the windows server system temp folder "C:\windows\temp\" as a directory even though I set the dir string in tempnam() to "/tmp" so the file would be created in the /tmp folder under the site root.

I have a feeling that the tempnam() dir parameter does not like relative paths.

I even tried using the full URL path "www.domain.com/tmp" as a dir in the tempnam() function but it still would not work.

incrediBILL




msg:4267176
 5:12 am on Feb 15, 2011 (gmt 0)

Random # isn't 100% unique - you could end up overwriting the same file depending on the volume of requests and how it's seeded.


I use the tempnam() in Linux and crank about about 50K temp names a day, it works really well.

Perhaps the tempnam() problem is your mixing of "/" and "\" or perhaps try ".\temp"

nelsonm




msg:4267197
 6:42 am on Feb 15, 2011 (gmt 0)

My development server is a windows 7 x64 machine.

I tried your suggestions, but i still get "C:\Windows\Temp\". According to the php doc's, if the specified directory does not exist, tempnam() may generate a file in the system's temporary directory, and return the name of that.

the "/tmp" folder i'm trying to write a tmp file into exists in the development sites root directory "client". The complete system path on the development web server is "D:\Webserver\Apache\development\client\tmp". Maybe i'm just not specifying the tempnam dir parameter right.

I guess i could try and just use tempnam() with some string functions to extract the unique id and then append the file prefix and type to it.

incrediBILL




msg:4267202
 7:16 am on Feb 15, 2011 (gmt 0)

Try specifying the full path if all else fails, sounds like a rights issue on the /tmp path possibly

nelsonm




msg:4267223
 8:37 am on Feb 15, 2011 (gmt 0)

No...

The issue is me! Apparently i tried everything - but - "./tmp" in tempnam(). It now works and finds the correct development site root path.

Sorry about that...

Unfortunately, since tempnam() creates the file by default with the ".tmp" file extension, I now have to rename the file to change the file type to ".html".

Kind of messy, but problem solved - thanks again!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / HTML
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved