Web Analytics Made Easy -
StatCounter Download Counter with PHP and htaccess - CodingForum

Announcement

Collapse
No announcement yet.

Download Counter with PHP and htaccess

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Download Counter with PHP and htaccess

    Hi folks,

    I want to install some sort of a download security system including a download counter. Whenever someone requests a file from the downloads directory I want it to redirected via htaccess to a php script. This script is supposed to redirect directly to the requested file and increment the download counter. I also want to check the referrer if it comes from my domain or if someone is hotlinking the files.

    How can I do something like that? Or would that result in an infinite loop?

    The download counting is no problem I just need help with the htaccess redirect.

    Thanks!
    Last edited by CalypsoClub; Mar 3, 2004, 07:44 AM.

  • #2
    If you don't redirect to the file with the script, it won't result in an infinite loop.

    I just pasted a script here a week or two ago to do something like this:

    PHP Code:
    <?php

        
    // place here your code to count the downloads, for example:
        
    $conn = @mysql_connect("localhost""user""pw");
        @
    mysql_select_db("database"$conn);
        @
    mysql_query("UPDATE downloadConterTable SET downloads = downloads + 1");

        
    // Setting MIME-type, for example a zip
        
    header('Content-type: application/zip');
        
    // header('Content-type: audio/mpegurl');
        // header('Content-type: audio/x-mpegurl');
        // header('Content-type: m3u/text');
        // Setting Name of download
        // header('Content-Disposition: attachment; filename="playlist.m3u"');
        
    header('Content-Disposition: attachment; filename="test.zip"');

        
    // ouput file content
        
    readfile('original_file.zip');
    ?>
    A list of some Mimetypes can be found here:


    Saludo
    piz
    www.united-scripts.com
    www.codebattles.org

    Comment


    • #3
      Thanks for your answer! This will help me.

      But I still have a problem...

      I need help with the .htacces file itself, so that it redirects to the php file.

      I want to have something like this (pseudo code)

      rewrite /downloads/{filename} /downloads/download.php?file={filename}

      I would be nice if someone could provide me with something like that or help writing one.

      Cheers!

      Comment


      • #4
        mod_rewrite has to be installed on your apache and running.

        .htaccess:
        ======================
        RewriteEngine On
        RewriteRule /downloads/([^/\?]*) /downloads/download.php?file=$1
        ======================

        should work - not tested.
        www.united-scripts.com
        www.codebattles.org

        Comment


        • #5
          Thanks again, but still another thing...

          Can I do the same also with mod_alias?
          And how would that look like?

          Thanks!

          Comment


          • #6
            Sure, but I never used mod_alias...
            It should be something like this:

            AliasMatch /downloads/(.*) /downloads/download.php?file=$1
            www.united-scripts.com
            www.codebattles.org

            Comment


            • #7
              Thanks for all your replies, but there'sstill a problem. I always get a Forbidden 403 message, when I try t access the directory.

              phpinfo() tells me mod_rewrite is compiled, so i guess it should work, right? I added "RewriteEngine on" (without quotes) to the .htaccess file, but it still doesn't work.

              Is it a common problem? Can I fix it, or do I have to speak with my hoster?

              Thanks in advance!

              Cheers

              Comment


              • #8
                Is it a common problem?
                Yes, it is.
                But don't ask me where the problem is...

                Can I fix it, or do I have to speak with my hoster?
                yes, you've to contact your hoster.

                Greetz
                piz
                www.united-scripts.com
                www.codebattles.org

                Comment

                Working...
                X