Icy Phoenix

     
 


Post new topic  Reply to topic 
Page 1 of 1
 
 
Reply with quote Download Post 
Post SECURITY - How To Prevent Hotlinking With PHP 
 
How To Prevent Hotlinking With PHP

Hotlinking is the act of one site embedding content (such as images, audio files, and videos) hosted on another site. This uses up the ‘bandwidth’ (data transfer allowance) of the site hosting the file, which can be very expensive for webmasters who pay for hosting by the amount of data transferred — as a result, hotlinking is often called ‘leeching’ or ‘bandwidth theft’.

Hotlinking can be prevented using the .htaccess file on an Apache web server, but cheaper web hosting packages often don’t allow webmasters to change this file. The code shown below allows webmasters to prevent hotlinking entirely from within PHP (without .htaccess), which is supported by most web hosting packages. As well as preventing hotlinking the code also allows the site to benefit from direct links (when a site links to content on another site but doesn’t embed it).
 




____________
jack of all trades, master of none
http://www.mieloma.com/ - http://www.casimedicos.com/ - http://www.egalego.com/ - http://www.casimedicos.com.es/ - http://www.medicosmir.com/
 
casimedicosSend private messageVisit poster's website  
Back to topPage bottom
Icy Phoenix is an open source project, you can show your appreciation and support future development by donating to the project.

Support us
 
Reply with quote Download Post 
Post Re: How To Prevent Hotlinking With PHP 
 
How to use the code to prevent hotlinking
Create a directory on your website to contain the anti-hotlinking script and your protected media.
Copy the code in prevent-hotlinking.txt into a file called index.php inside that directory.

Code: [Download] [Hide] [Select]
<?php
$dir='secret-directory-name-here/';
if ((!$file=realpath($dir.$_GET['file']))
    || strpos($file,realpath($dir))!==0 || substr($file,-4)=='.php'){
  header('HTTP/1.0 404 Not Found');
  exit();
}
$ref=$_SERVER['HTTP_REFERER'];
if (strpos($ref,'http://www.example.com/')===0 || strpos($ref,'http')!==0){
  $mime=array(
    'jpg'=>'image/jpeg',
    'png'=>'image/png',
    'mid'=>'audio/x-midi',
    'wav'=>'audio/x-wav'
  );
  $stat=stat($file);
  header('Content-Type: '.$mime[substr($file,-3)]);
  header('Content-Length: '.$stat[7]);
  header('Last-Modified: '.gmdate('D, d M Y H:i:s',$stat[9]).' GMT');
  readfile($file);
  exit();
}
header('Pragma: no-cache');
header('Cache-Control: no-cache, no-store, must-revalidate');
include($file.'.php');
?>


 
Replace the URL http://www.example.com/ on the ninth line of the script with the URL of your website — don’t forget to include the trailing slash.
Replace the phrase ‘secret-directory-name-here’ in the second line of the script with something people are unlikely to guess (for example, a string of thirty random letters) and create a directory with this name inside the directory containing the script. Do not reveal the name of this secret directory to anyone, as doing so would allow people to hotlink to the files it contains. If you do accidentally reveal the directory name, just rename it and alter the script accordingly.
For each piece of content you want to protect, place it in the secret directory (you can also create subdirectories inside that directory).
 




____________
jack of all trades, master of none
http://www.mieloma.com/ - http://www.casimedicos.com/ - http://www.egalego.com/ - http://www.casimedicos.com.es/ - http://www.medicosmir.com/
 
casimedicosSend private messageVisit poster's website  
Back to topPage bottom
Reply with quote Download Post 
Post Re: How To Prevent Hotlinking With PHP 
 
For each piece of content create a file with the same name but with .php appended and place it in the same location as the content. This document should contain HTML (possibly with embedded PHP) to display when a site has either hotlinked or directly linked to yours. The simplest code will redirect the request to the page on your site containing the content:
Code: [Download] [Hide] [Select]
<?php
header('Location: http://www.example.com/page-containing-the-content/');
?>


To include the content in your pages, use a URL of the form http://www.example.com/script-directory/?file=content-file, where ‘script-directory’ is the directory containing the anti-hotlinking script, and ‘content-file’ is the name of the content file (if the content file is in a subdirectory of the secret directory, include the subdirectory in the path — for example ?file=subdirectory/contentfile). These URLs can be included in the pages mentioned in the previous step in order to create a custom page displaying the content instead of redirecting to an existing page on your site.
Now when someone tries to hotlink to content on your site, people visiting that page will only see the content if they have already been to your site. Direct links to content on your site will return an HTML file containing the content so you can ensure visitors see the file in its original context.

The script currently includes MIME types for JPEG, PNG, WAV, and MIDI files. If you need to extend it to handle other file types, look up the MIME type name in the official list of MIME types and then add code of the following form above the line containing 'jpg'=>'image/jpeg',:
    'extenstion'=>'mimetype',

‘extension’ is the file extension (for example, ‘mpg’) and ‘mimetype’ is the official MIME type (for example, ‘video/mpeg’). Some formats do not have registered MIME types, in which case the x prefix should be used (for example, ‘image/x-myimageformat’).
http://www.iana.org/assignments/media-types/
 




____________
jack of all trades, master of none
http://www.mieloma.com/ - http://www.casimedicos.com/ - http://www.egalego.com/ - http://www.casimedicos.com.es/ - http://www.medicosmir.com/
 
casimedicosSend private messageVisit poster's website  
Back to topPage bottom
Reply with quote Download Post 
Post Re: How To Prevent Hotlinking With PHP 
 
How the code to prevent hotlinking works
Code: [Download] [Hide] [Select]
1  <?php
 2  $dir='secret-directory-name-here/';
 3  if ((!$file=realpath($dir.$_GET['file']))
 4      || strpos($file,realpath($dir))!==0 || substr($file,-4)=='.php'){
 5    header('HTTP/1.0 404 Not Found');
 6    exit();
 7  }
 8  $ref=$_SERVER['HTTP_REFERER'];
 9  if (strpos($ref,'http://www.example.com/')===0 || strpos($ref,'http')!==0){
10    $mime=array(
11      'jpg'=>'image/jpeg',
12      'png'=>'image/png',
13      'mid'=>'audio/x-midi',
14      'wav'=>'audio/x-wav'
15    );
16    $stat=stat($file);
17    header('Content-Type: '.$mime[substr($file,-3)]);
18    header('Content-Length: '.$stat[7]);
19    header('Last-Modified: '.gmdate('D, d M Y H:i:s',$stat[9]).' GMT');
20    readfile($file);
21    exit();
22  }
23  header('Pragma: no-cache');
24  header('Cache-Control: no-cache, no-store, must-revalidate');
25  include($file.'.php');
26  ?>

Line 2
Line 2 stores the name of secret directory so that the script can easily be modified.
Lines 3 to 7
On line 3, the first condition of the if-test checks that the file exists and resolves any relative path components (for exmaple ../). On line 4, the second condition checks the the path specified is in the secret directory (so that people can’t use relative path components to see the source code of scripts on your site), and the third condition checks that the path does not refer to one of the PHP files associated with the content. If the file specified should not be accessed (or doesn’t exist) a 404 (file not found) status code is returned and the script exits.
Lines 8 and 9
The if-test in line 9 could have been written in various forms, but using the ‘or’ operator with this order of parameters is fastest. The test allows the file to be displayed if either the referrer header does not refer to a website (for example, a blank refer or one obscured by security software), or if it refers to the correct wesbite. The slash at the end of the website name is necessary to prevent exploitation by websites with names of the form http://www.yoursite.com.theirsite.com/), and the use of ‘http’ in the second condition catches both regular (http://) and secure (https://) referrers.
Lines 19 to 21
These lines output the content file with the appropriate headers for content type, content length, and date of last modification, and then exit the script.
Lines 23 to 25
These lines output headers to prevent caching and then perform the action specified in the PHP file associated with the content file (for example, redirecting to the page containing the content). The anti-caching headers are needed so that if users later try to view the content as you intended they will receive the content file and not the HTML file. Note that the content file itself will be cached, which means that if a site hotlinks to the file and a user already has the file in their cache they will still see it. If they don’t already have the file in their cache, their browser will download an HTML file, which it will realise isn’t a media file and won’t display (unless the link was a direct link, rather than a hotlink).

For more ready-to-use examples of this kind, try O’Reilly & Associates’ excellent PHP Cookbook:
PHP Cookbook at Amazon.com
PHP Cookbook at Amazon.co.uk (for British readers)

original
http://www.safalra.com/programming/php/prevent-hotlinking/
 




____________
jack of all trades, master of none
http://www.mieloma.com/ - http://www.casimedicos.com/ - http://www.egalego.com/ - http://www.casimedicos.com.es/ - http://www.medicosmir.com/
 
casimedicosSend private messageVisit poster's website  
Back to topPage bottom
Reply with quote Download Post 
Post Re: How To Prevent Hotlinking With PHP 
 
Really good... I'll add it to the Docs Section.

Can you add something about preventing HOTLINK using HTACCESS?

Thanks.
 




____________
Luca
SEARCH is the quickest way to get support.
Icy Phoenix ColorizeIt - CustomIcy - HON
 
Mighty GorgonSend private messageSend e-mail to userVisit poster's website  
Back to topPage bottom
Reply with quote Download Post 
Post hotlink 
 
ok
i have some post on this forum about this
i'll try to find it and rewrite
 



 
casimedicosSend private messageVisit poster's website  
Back to topPage bottom
Reply with quote Download Post 
Post Re: SECURITY - How To Prevent Hotlinking With PHP 
 
Hotlinking can also be protected by assigning a session variable. When viewer comes to the download page that page will assign a session variable which enable php attachment download. But if viewer directly enter a download link, i.e. if session variable is not set that link will be redirected to the download page.
Download page:
Code: [Download] [Hide] [Select]
<?php
session_start();
$_SESSION['download']='ok';
?>

Attachment download page:
Code: [Download] [Hide] [Select]
<?php
session_start();
if($_SESSION['download']!=='ok'){
$redirect='index.html'; //URL of the page where you want to redirect.
header("Location: $redirect");
exit;}
//Attachment download code.
?>

For more information: Hotlink protected PHP download script
 



 
archanSend private message  
Back to topPage bottom
Reply with quote Download Post 
Post Re: SECURITY - How To Prevent Hotlinking With PHP 
 
thumps up for your article, great post...
 



 
topman26Send private message  
Back to topPage bottom
Post new topic  Reply to topic  Page 1 of 1
 


Display posts from previous:    

HideWas this topic useful?

Link this topic
URL
BBCode
HTML




 
Permissions List
You cannot post new topics
You cannot reply to topics
You cannot edit your posts
You cannot delete your posts
You cannot vote in polls
You cannot attach files
You can download files
You cannot post calendar events


  

 

  cron