Solved

Bandwidth & Hotlinking

Posted on 2004-10-15
4
234 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a Select Query and Populate a Table 3 43
simplest php form 3 57
.php tree directory? 5 54
How to set a countdown by not using the PC time in PHP 36 43
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now