Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Bandwidth & Hotlinking

Posted on 2004-10-15
4
Medium Priority
?
271 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 252 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 248 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

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 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