473,779 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hotlinking protection using php

12 New Member
php version - 4.4.4
mysql version - 4.1.22

Previously I tried to prevent hotlinking of images on a clients site using .htaccess and mod_rewrite. It however didn't work for some reason or other and I decided to do hotlinking protection using php. I'm really new to php. I've seen code like the following
Expand|Select|Wrap|Line Numbers
  1. <img src="display.php?id=1898" border="0" />
and have been trying to do this for the site based on this post <Link removed>. I'm having trouble specifically with getting the php script to return an image when queried as above. I've used the following code (edited because of database connection details).

Expand|Select|Wrap|Line Numbers
  1. <?
  2. //display.php
  3. $dbh=mysql_connect("<CONNECTION DETAILS...>") or die ('I cannot connect to the database because: ' . mysql_error());
  4. mysql_select_db("<DATABASE>",$dbh);
  5. $query="SELECT filename FROM thumbnails WHERE idn=".$_GET["wli"];
  6. $rawdb=mysql_query($query);
  7. $array=mysql_fetch_array($rawdb);
  8.   if (!empty($array["filename"])){
  9.         // Output the header
  10.     header("Content-Type: image/jpeg");
  11.        // Output the image
  12.         echo "photos/gallery_".$array['filename'].".jpg";
  13.   }else{
  14.     echo "photos/imagenotretrievable.jpg";
  15.     }
  16. @mysql_close($dbh);
  17. ?>
  18.  
With the corresponding code on an html page used to display the image...

Expand|Select|Wrap|Line Numbers
  1. <a href="www.somesite.com" ><img src="display.php?wli=1998" alt="" border="0" /></a>
  2.  
It doesn't display an image, just the image title.
Oct 24 '07 #1
3 2449
Atli
5,058 Recognized Expert Expert
Hi. Welcome to TSDN!

I've removed the link from your post, as links to forums in competition with TSDN, commercial webs and any other links that may violate the Posting Guidelines are forbidden in the technical forums.

As to your problem.
Try changing line 12 of your code, so instead of just echoing the name of your image, echo the contents.

The 'src' attribute of the <img> tag expects a link to the image itself, where it can find the contents of the image. The page containing your code is in reality posing as an image, so it needs to return the contents of the image it represents, rather than its location.

Expand|Select|Wrap|Line Numbers
  1. # Change
  2. echo "photos/gallery_".$array['filename'].".jpg";
  3.  
  4. # Into
  5. echo file_get_contents("photos/gallery_".$array['filename'].".jpg");
  6.  
P.S. I highly recommend upgrading to PHP5. And even MySQL 5.
Oct 24 '07 #2
blindlemon1983
12 New Member
Thanks a stack that fixed it! Many thanks,
Sam
Oct 24 '07 #3
blindlemon1983
12 New Member
Just an update - the code above doesn't prevent hotlinking. I had to use the code in combination with a .htaccess mod rewrite. This is only possible if your server is Apache. You should also have a basic understanding of regular expressions although not absolutely necessary.

Put display.php (with the right code - see above posts) in its own folder in your root folder (usually public_html). Say we've called this folder "hotlinking " then create or add to your .htaccess file (just a text file) the following code replacing yourdomain with your domain.

Expand|Select|Wrap|Line Numbers
  1. Options +FollowSymLinks
  2. RewriteEngine On
  3.  
  4. ##### HOTLINKING PROTECTION ###############
  5. RewriteCond %{HTTP_REFERER} ^(http://(www\.)?yourdomain\.com(/.*)?)?$ [NC]
  6. RewriteRule ^hotlinking/([0-9]+)$ /hotlinking/display.php?wli=$1 [L]
  7. ####################################
  8.  
Now each image you want protected have its src attribute as follows
Expand|Select|Wrap|Line Numbers
  1. <img src="http://www.yourdomain.com/hotlinking/[idn]" alt="etc" border="0" /> 
where [idn] is a key for the database that corresponds to that images filename - i.e. have a mysql database with each record having a key (idn) and a corresponding filename (filename). Mod rewrite if working properly will rewrite the image src attribute above as http://www.yourdomain. com/hotlinking/display.php?wli =[idn] which will return an image file. Mod rewrite will only do this if the refferer is yourdomain.com.

Think that's all. Its not fool proof but it should discourage many from hotlinking your images.
Oct 27 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
2367
by: Howard | last post by:
I am currently looking at the various packages that are available for software protection. I have a particular question that I'd welcome your feedback on. Background info: I work for a company that develops software, and typically releases software SDKs. Development is in C++ using MS Visual C++. We need to apply software encryption/protection to achieve: 1) Time-limited versions (e.g software expires after X days)
0
2029
by: SoftComplete Development | last post by:
SoftComplete Development Updates EXECryptor to v. 2.1.20 Software piracy! Cracked serial numbers! Thousands of commercial products are posted on the warez sites and become available to all who want have your software product and pay no money for your development work! Shareware developpers look for good protection for their products and create some protection themselves or find ready-made tools. But there's no many programs really...
17
3336
by: andre.gunther | last post by:
Hello, unfortunately I am a customer of Yahoo Webhosting. They don't offer scripting or any meaningful server controls. I have a photo website and I have serious problems with leachers now. Those guys at xanga.com hotlink to my free desktop wallpapers, thus generating a lot of traffic. I need to block this somehow, but I have no idea how to do it on the yahoo webhosting (I am not even sure if they are running apache or IIS). Does...
2
5787
by: Rune Nergard | last post by:
I have tried to use the System.Security.Cryptography.Xml.SignedXml class to sign an Xml message with Xml-DSIG and using an Enveloped signature type and the sha1RSA algorithm. Everything works fine with soft certificates and some smartcard based certificates. I'm using X509Certificate2 to hold the certificates. My problem is that our customers is forced (by law!) to use a smart-card based personal certificate with strong protection. That is...
20
1718
by: Paul Bromley | last post by:
Not sure if I can ask the question in this forum - please let me know if it is better elsewhere. Does anyone know of a reasonably priced good copy protection program that will protect software on either a machine only or network range basis?? I have just found Shareguard that looks reasonably priced. My applications are for vertical market and are not likely to be cracked, BUT users are likely to pass working versions around if not...
0
1592
by: KhoaNguyen | last post by:
Hi, When i compiled these two source files, it gives me an error saying: Inaccesssible Due to its protection level. ------------Base Class----------------- using System; using System.Collections.Generic; using System.Text; using System.Configuration; using System.Data.SqlClient;
0
1874
by: dba | last post by:
Hi folks, I would just like to share with you SQL CodeSecure, a newly released database protection and auditing tool from SqlLabs: SQL CodeSecure provides ultimate protection from unauthorized creation, modification and deletion of database objects. It provides flexibility through various protection types and granularity at different protection levels. CodeSecure comes with a full-featured auditing tool that keeps track of all changes...
13
2027
by: Adhal | last post by:
Hi, How can I stop hotlinking to a specific file, and I want it to redirect it to a PHP link so I can monitor the number of downloads. Here is my site with the download page: http://www.adhal.org/software/downloads.htm And here is softpedia directly connecting to my file and bypassing the counter php. http://www.softpedia.com/get/File-managers/HashNET.shtml
0
1954
by: RobertTheProgrammer | last post by:
Hi folks, I need to combine these two functions and I'm not sure how to go about it. I know how to use .htaccess to prevent anyone from hotlinking the images on my site. What I basically want to do is to (1) prevent anyone, anywhere from hotlinking my images and (2) prevent anyone from MySpace from hotlinking to my site at all (whether images or otherwise). Now I currently have my .htaccess like this: RewriteEngine on
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.