Solved

Bandwidth & Hotlinking

Posted on 2004-10-15
4
228 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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 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

10 Experts available now in Live!

Get 1:1 Help Now