Solved

Bandwidth & Hotlinking

Posted on 2004-10-15
4
240 Views
Last Modified: 2012-06-27
I offer a free file upload and storage server. I'm thinking of coding the script in Coldfusion, but need to know how to actually calculate the amount of bandwidth used for each individual image. This information will be available in the administrator page.

Also, I want to allow hotlinking, but I don't want people to link directly to the .jpg file. Instead, I want them to link to a CFM file and still be able to use that link in their <img> tags. Is there a way to achieve this?

Any advice would be appreciated, thanks.
0
Comment
Question by:lifegauge
4 Comments
 

Accepted Solution

by:
enune earned 63 total points
ID: 12326187
Hi lifegauge,

Theoretically, you should be able to calculate the amount of bandwidth used by simply retrieving the size of the file/image via PHP's filesize() function, and multiplying it by the amount of times the file has been downloaded.

As for your second question, it is quite simple to achieve what you want. I use the following code to read files and send them to users from my file-archive:

        header("Pragma: ");
        header("Cache-Control: ");
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: no-store, no-cache, must-revalidate");  //HTTP/1.1
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Content-length: {$fd[file_size]}" );
        header("Content-type: {$fd[file_type]}" );
        header("Content-disposition: attachment; filename={$fd[file_filename]}");
        readfile($fd['file_dir'].$fd['file_filename']);

where '$fd' is an array containing
a) The size of the file
b) the MIME type (file_type)
c) the file name (file_filename)
d) the directory and real filename on my server (file_dir and file_filename)

You can then point the user to a link such as:
http://www.yourserver.com/get_file.php?id=123
and, using the id number, retrieve the file record from your database that they request, and using readfile(), send them the data.

Hope this helps.

Enune
0
 
LVL 20

Assisted Solution

by:virmaior
virmaior earned 62 total points
ID: 12338219
if you have total control of the webserver and its apache, then you can let them hotlink with .jpg extension by using a combination of php and .htaccess

.htaccess:

ReWriteEngine On
ReWriteRule . /imageserver.php

imageserver.php:

<?php
session_start();
if ($_SESSION['usergroups'][0] == 'y') //check if you login to download the file
{

        $file = $_SERVER["DOCUMENT_ROOT"] . $_SERVER["REQUEST_URI"];
        if (file_exists($file))
        {
              //header("Content-type: application/force-download");
              header("Content-Transfer-Encoding: Binary");
              header("Content-length: ".filesize($file));
              header("Content-disposition: attachment; filename=".basename($file). "");
              readfile("$file");
        }
        else
        {
                  include "error.php";
       }
}
else
{
       include "error.php";
}

?>


by combining these two files with a DB backend that checks bandwidth usage, you can let them hotlink to the ".jpg" (which means they end up calling the php script that checks and then sends the real .jpg if they can get it)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question