Good work mate, please post here if you have the full code for hotlink protection that redirects to the post of the image. We remain grateful to you.
To redirect to your image page I am using this: RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mydomain.com[NC] RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$ [NC] RewriteRule (.*) /redirect.php?p=$1 [L] Now, you have to find your host/landing page in redirect.php. It just so happens that my structure of the images and pages is like 1_image.jpg -> 1_image.html so I just redirect in redirect.php with header() function. The $_GET["p"] in redirect.php lets me know which image is being hotlinked. If you have more complicated structure, I guess you wold have to sql query your img database and find your landing page.
Basic hotlink protection The jpeg pictures needing protection are in the /images folder. If they insert the picture into a different site, so the HTTP Referer isn't our own site or if it's empty, then the /donothotlink.jpg is shown instead of the original picture. .htaccess RewriteEngine on RewriteBase / RewriteCond %{HTTP_REFERER} !^http://www\.example\.com [NC] RewriteCond %{HTTP_REFERER} !^$ RewriteRule ^images/.*\.jpg /donothotlink.jpg [L] Code (markup): Problems with the above solution Several browsers don't send the Referer header included in the RFC 2616 standard. There are sites from which most browsers don't transmit a referer. (Can you guess which ones?) A simple picture warning about hotlinking won't necessarily convince people to visit your site. A more though-out, better solution The solution below yields slightly better results compared to the one above, because the visitor sees the watermarked and slightly worse quality, but identical sized version of the original image, based on which they can already decide if they want to visit the site or not. The solution requires GD-supported PHP running option beside access to .htaccess. .htaccess RewriteEngine on RewriteBase / RewriteRule ^.*\.html - [L,CO=shield:yes:www.example.com:15:/] [NC] RewriteRule ^$ - [L,CO=shield:yes:www.example.com:15:/] RewriteCond %{HTTP_USER_AGENT} !(bot|crawler|spider|slurp|pinterest|facebook|feedfetcher) [NC] RewriteCond %{HTTP_REFERER} !^http://www\.example\.com [NC] RewriteCond %{HTTP_COOKIE} !shield=yes [NC] RewriteRule ^images/.*\.jpg /protect.php [L] Code (markup): In the example above, the .htaccess file sets a cookie called shield (yes) for a 15 minute period for the main page (http://www.example.com/) and for all the requests that include a .html extension. Afterwards, it checks the contents of the shield cookie for every download of an image from the /images library, if it doesn't find it and the referer doesn't point to our own site, then the protect.php file receives the request instead of the original picture. It doesn't affect the requests coming from robots. protect.php <?php // config $c_protectdir = "images"; // relative (!) path to protected directory $c_watermark = "watermark.png"; // transparent PNG watermark $c_watermark_size = "80"; // percent of original width $c_protected_image_quality = "50"; // jpeg quality (percent) // main if (preg_match("/^\/(".preg_quote($c_protectdir,"/")."\/.*\.jpg)/",$_SERVER['REQUEST_URI'],$matches)) { $picture = $matches[1]; if (!file_exists($picture)) { header("HTTP/1.1 404 Not Found"); exit; } else if (file_exists($picture.".protected")) { $jpgdata = file_get_contents($picture.".protected"); } else { $source = imagecreatefromjpeg($picture);$s_width = imagesx($source);$s_height = imagesy($source); $watermark = imagecreatefrompng($c_watermark);$w_width = imagesx($watermark);$w_height = imagesy($watermark); $d_width = round($s_width * $c_watermark_size / 100);$d_height = $d_width * $w_height / $w_width; imagecopyresampled($source, $watermark, round(($s_width - $d_width) / 2), $s_height - $d_height, 0, 0, $d_width, $d_height, $w_width, $w_height); imagejpeg($source,$picture.".protected"); $jpgdata = file_get_contents($picture.".protected"); } } else { header("HTTP/1.1 403 Forbidden"); exit; } header("HTTP/1.1 200 OK"); header("Content-type: image/jpeg"); header("Cache-Control: no-cache, no-store, must-revalidate"); header("Pragma: no-cache"); header("Last-Modified: ".date('r')); header("Expires: ".date('r',time()-3600)); header("Accept-Ranges: none"); header("Content-Length: ".strlen($jpgdata)); print $jpgdata; ?> PHP: The code above takes a partially-transparent .png image (watermark.png), then copies its contents over onto the original picture, shrinking or enlarging it as necessary. Such a partially-transparent image can easily be made with GIMP or any other image-modifying program. The example above resizes the partially-transparent png image in a way that it covers 80% of the original picture's width, this appears in the bottom corner of the original picture. So that it needn't be recreated with each download, the modified image is saved beside the original image with a .protected extension added onto the original filename. If a new request comes and the .protected file already exists, it just gives that over to the browser. It's useful to add a short text onto the partially-transparent image, informing the visitor that they can access the original picture on your site. We don't allow the cacheing of that image so that they can immediately find the original picture when they visit the site. There's also a small javascript that can be inserted into the head section of html pages, it helps in case your page is opened into an iframe, because then the javascript deletes the cookie that gives permission for accessing the images. index.html <html> <head> <script type="text/javascript"><!-- // if (top != self) document.cookie = 'shield=no; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/'; // --></script> </head> <body> <div> <img src="/images/protected.jpg" alt="Protected image"/> </div> </body> </html> HTML: The code above can and should be improved, for example with logging and other tricks. Share it and keep improving it!
very sad situation for all webmasters .. for the entire internet ... but i'm looking at fansshare.com's solution ... So - when someone actually clicks on an image - first it displays for 1 2 seconds the normal image (from Google's cache), and when Google tries to pull the full size image, some script redirects to an low-resolution image with watermark. So - if this can be detected - if we can detect if Google tries to pull an image, then i'm sure that we can create some php script that will redirect to the normal page! not to the image. so when somebody click on an image, for one moment the image will be displayed on Google's page, and then when Google tries to pull the full size, the browser will be redirected to the page! I'm sure that this can be done ! and i'm willing to pay if someone actually can do this!
Don't worry google will always send traffic to your website They are not going to destroy web, it's where they earn money from
)))))))))))))))) really ??? do you understand that they make money with our images ???? if you have an website with adsense on it - they pay you max 70% and 30% remains to google . But !!! if they use your images and display your images .. then they don't pay nothing !
As a matter of fact, fansshare.com's solution works only for part of their pictures, not all of them: http://www.google.com/search?q=fans...hoGQCA&sqi=2&ved=0CAcQ_AUoAA&biw=1584&bih=658
Finally I got it working do image search for my website " site:www . a1-diamond . com " and click on view original website it will take u to the webpage which host the image. ---------------------Method---------------------- step1: make a database of "images" and in which "webpage url" they are in with 2 fileds :- url, image, url = full webpage url/ path in which image hosted (e.g http:// www. abc .com /page.html image= full path of image (e.g http:// www. abc . com /images /image.jpg) step 2: then make a file " image.php" add these lines <?php $inurl=$_GET['p']; $inurlfull= "http://" . $_SERVER['HTTP_HOST'] ."/".$inurl; $host="192.168.1.1"; // Host name $username="username"; // Mysql username $password="password"; // Mysql password $db_name="database"; // Database name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Retrieve data from database $result = mysql_query('select url, image from table where image = "'.$inurlfull.'"'); $row = mysql_fetch_assoc($result); mysql_free_result($result); $redirect= $row['url']; header( "Location:$redirect" ) ; ?> ------------------------------------- Step 3 add these lines in .htaccess RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?abc.com [NC] RewriteCond %{HTTP_USER_AGENT} !Googlebot [NC] RewriteCond %{HTTP_USER_AGENT} !Googlebot-Image [NC] RewriteCond %{HTTP_USER_AGENT} !Mediapartners-Google [NC] RewriteCond %{HTTP_USER_AGENT} !Mediapartners [NC] RewriteCond %{HTTP_USER_AGENT} !Googlebot-Mobile [NC] RewriteCond %{HTTP_USER_AGENT} !Googlebot-Video [NC] RewriteCond %{HTTP_USER_AGENT} !Googlebot-News [NC] RewriteCond %{HTTP_USER_AGENT} !msnbot [NC] RewriteCond %{HTTP_USER_AGENT} !slurp [NC] RewriteCond %{HTTP_USER_AGENT} !Bingbot [NC] RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$ [NC] RewriteRule (.*) /image.php?p=$1 [L] --------------------------------------------- replace abc.com with your domain all done let me know if not working, or if u need help to get it done on your website I will do it for you contact me via pm or at bhavatmaj @ gmail .com
yes - it works but i'm looking for something different - when somebody clicks on the image to redirect to the main page! in addition this is ok for small websites like yours - but how about if you have over 100.000 images ? Thx
It looks very cool. Thank you! I have a static html site. My images are stored in different folders. Could you please explain how to create such database of images?
______________________________________________________________________________ Hello this script is good but can help us with wordpress site?
this is not something dynamic .. you have to build your image database manually ... and if you have 100.000 images ... what you do ?
Yes, I don't know what to do. I have a lot of images, not 100, but about 50K. Do you think is it imposible to build my image database?
Not sure ... we have to wait to see if somebody else finds another solution .... like i said -- i am willing to pay for this.