SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast globalspy's Avatar
    Join Date
    Feb 2004
    Location
    Sydney, Australia
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Set cookie when user downloads file

    Hi,

    I want to set a cookie when a visitor dowload an *.exe file from my web server. Is this possible using Apache/PHP?

    Example, if a user downloads /myfile.exe from the referrer download.com.com, I want to put an ID cookie in their browser and then write the referrer into a database so I can track if that user later comes back using the same browser.

    I know it's not going to be easy/elegant to mix PHP with Apache's static file serving. What are some of the best ways to achieve this?

  2. #2
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    create a download.php page and handle all your downloads through it.

    download.php?myexe.exe

    And in this page, create a cookie and then redirect the page to myexe.exe which will be downloaded by user.

  3. #3
    SitePoint Enthusiast globalspy's Avatar
    Join Date
    Feb 2004
    Location
    Sydney, Australia
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What if I already have dozens of software sites linking to /myfile.exe directly?

  4. #4
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That can be a little tricky but then Apache comes in help
    Have you ever used apache Redirect?
    You can pattern search in url and direct to a url
    So if file is pointing to *.exe then redirect to download.php first with name of exe file and then in download.php send it to user after setting cookie.

  5. #5
    SitePoint Enthusiast globalspy's Avatar
    Join Date
    Feb 2004
    Location
    Sydney, Australia
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I am familiar with Redirect and RewriteRule.

    If I redirect myfile.exe to download.php (to set the cookie), and download.php redirects back to myfile.exe, that will cause an infinite loop?

    So I need download.php to redirect back to a different filename (myfile2.exe). Then users might start linking to myfile2.exe directly? Problem.

    Is there a way to do this without letting the user see that they are being redirected to download.php or myfile2.exe? I basically want this to be as transparent as possible, and as few redirects as possible so that it does not affect user agents negatively.

  6. #6
    SitePoint Enthusiast globalspy's Avatar
    Join Date
    Feb 2004
    Location
    Sydney, Australia
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP has a way of outputing a file to the page output. I could use a PHP file called myfile.exe (although it has an EXE extension, it can still be made to run as PHP) which can read in the real-myfile.exe and then output it to the buffer.

    Myfile.exe is about 1 MB in size. How ineffecient is it to do it this way?

  7. #7
    SitePoint Member
    Join Date
    Feb 2004
    Location
    London, UK
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course you could always set yourfile.exe to be parsed as a php page on your server and then:

    1) In that PHP page set the cookie
    2) Then redirect to a new php page
    3) IN the new page stream the file with code something like below

    PHP Code:
    $filepath 'foobar.exe; // The file 
    // the data 

    $filename = basename($filepath); 
    if (strstr($_SERVER['
    HTTP_USER_AGENT'], 'compatibleMSIE ') !== false && 
    strstr($_SERVER['
    HTTP_USER_AGENT'], 'Opera') === false) { 
    // IE doesn'
    t properly download attachmentsThis should work 
    // pretty well for IE 5.5 SP 1 
    header('Content-Disposition: inline; filename="'.$filename.'"'); 
    header('Content-Type: application/octetstream; name="'.$filename.'"'); 
    header('Content-length: '.filesize($filepath)); 
    } else { 
    // Use standard headers for Netscape, Opera, etc. 
    header('Content-Disposition: attachment; filename="'.$filename.'"'); 
    header('Content-Type: application/octetstream; name="'.$filename.'"'); 
    header('Content-length: '.(filesize($filepath)); 


    readfile($filepath); 
    .
    Justin

    ----------------------------------------------------------------
    http://php.jvmultimedia.com - PHP Freeware and Tutorials

  8. #8
    SitePoint Enthusiast globalspy's Avatar
    Join Date
    Feb 2004
    Location
    Sydney, Australia
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jv2222, I could just set the cookie in the same file as the page that streams the file? Is it OK (read: effecient) to use readfile() for large ~1-2 MB files?

    Why did you use a Content-Disposition header if this is not usually needed for file downloads? Example of a file response with no Content-Disposition header.

    Code:
    HTTP/1.1 200 OK
    Date: Mon, 16 Feb 2004 14:32:28 GMT
    Server: Apache/1.3.29 (Unix)
    Last-Modified: Thu, 12 Feb 2004 14:22:47 GMT
    ETag: "22816d-14c58a-402b8c37"
    Accept-Ranges: bytes
    Content-Length: 1361290
    Content-Type: application/octet-stream
    Connection: Keep-Alive
    I wouldn't be able to generate the Last-Modified and ETag headers so I guess that would make the file uncacheable!

    Is this the most elegant way to do it?

    Edit: I wouldn't be able to handle Accept-Ranges either which would make it un-resumable (file download can't be resumed). So many disadvantages to consider.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •