Solved

Bandwidth & Hotlinking

Posted on 2004-10-15
4
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to dynamically set the form action using jQuery.
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.

696 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