Hotlinking: when users find your image in Google Images they will be redirect to your gallery, to the page that contains that image. They can not save it directly from google. That brings a lot more traffic (visitors to your gallery) and they will proabibly remember your gallery.
If users keep saving the image directly from google, you will loose a lot of visitors.
Attention: will consume more server resources (CPU time and MySql time) because redirect.php needs to search the database for the image name and find the id of the page for that image.
A smaller database may help. Small database ( make you database smaller) for less server resource: http://forum.coppermine-gallery.net/index.php/topic,77935.0.html (http://forum.coppermine-gallery.net/index.php/topic,77935.0.html)
found in the forum tutorial do redirect to a image or a link but not to the page that contains the image
I did this:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?allvip.us [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ allvip.us/gallery/displayimage.php [NC,R,L]
what should I write on line 4 of the code to go to displayimage.php of the actual image?
As displayimage.php always require a pid parameter, you need to query the database. Please post a link to the tutorial, maybe there's some information that helps us.
I choosed to use a .htaccess rule from http://www.hongkiat.com/blog/smarter-way-to-prevent-image-hotlinking-with-htaccess/ (http://www.hongkiat.com/blog/smarter-way-to-prevent-image-hotlinking-with-htaccess/)
now I have this code in my htacees that redirects all goggle images to: http://www.allvip.us/no_hotlinking.html
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?allvip.us [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ http://www.allvip.us/no_hotlinking.html [NC,R,L]
I would like the to redirect to the page that contains the image.
example when people click View image and not visit page:
http://www.google.com/imgres?um=1&sa=N&biw=1024&bih=580&hl=en&tbm=isch&tbnid=Q6MCfgnUUesY4M:&imgrefurl=http://allvip.us/gallery/displayimage.php%3Falbum%3D57%26pid%3D10287&docid=uMldMOIWdHBynM&imgurl=http://allvip.us/gallery/albums/userpics/10001/angelina_jolie_000009.jpg&w=682&h=1024&ei=QdBGUoyYI4SPswb7yIF4&zoom=1&ved=1t:3588,r:7,s:0,i:102&iact=rc&page=1&tbnh=222&tbnw=148&start=0&ndsp=11&tx=87&ty=115 (http://www.google.com/imgres?um=1&sa=N&biw=1024&bih=580&hl=en&tbm=isch&tbnid=Q6MCfgnUUesY4M:&imgrefurl=http://allvip.us/gallery/displayimage.php%3Falbum%3D57%26pid%3D10287&docid=uMldMOIWdHBynM&imgurl=http://allvip.us/gallery/albums/userpics/10001/angelina_jolie_000009.jpg&w=682&h=1024&ei=QdBGUoyYI4SPswb7yIF4&zoom=1&ved=1t:3588,r:7,s:0,i:102&iact=rc&page=1&tbnh=222&tbnw=148&start=0&ndsp=11&tx=87&ty=115)
all the images to redirect to the page that has the image
to redirect to http://www.allvip.us/gallery/displayimage.php?album=57&pid=10287 (http://www.allvip.us/gallery/displayimage.php?album=57&pid=10287)
do not know what to look for in the database
the database has exact links of every pictures or albums but I can not write a rule in .htaccess for every image.I have to many pictures.
www.fansshare.com has this kind of hotlinking:
http://www.google.com/imgres?um=1&sa=N&biw=1024&bih=580&hl=en&tbm=isch&tbnid=dpRuXcqIAk7RDM:&imgrefurl=http://www.fansshare.com/news/eddie-redmayne-thanks-his-big-lips-for-getting-cast-as-angelina-jolie-s-son-in-good-shepherd/&docid=1-fkxIPsfjXP-M&imgurl=http://www.fansshare.com/media/content1/550x298_eddie-redmayne-thanks-his-big-lips-for-getting-cast-as-angelina-jolie-s-son-in--the-good-shepherd--3805.jpg&w=550&h=298&ei=rdZGUu3iAoittAatsYGAAQ&zoom=1&ved=1t:3588,r:2,s:0,i:87&iact=rc&page=1&tbnh=165&tbnw=283&start=0&ndsp=12&tx=165&ty=74 (http://www.google.com/imgres?um=1&sa=N&biw=1024&bih=580&hl=en&tbm=isch&tbnid=dpRuXcqIAk7RDM:&imgrefurl=http://www.fansshare.com/news/eddie-redmayne-thanks-his-big-lips-for-getting-cast-as-angelina-jolie-s-son-in-good-shepherd/&docid=1-fkxIPsfjXP-M&imgurl=http://www.fansshare.com/media/content1/550x298_eddie-redmayne-thanks-his-big-lips-for-getting-cast-as-angelina-jolie-s-son-in--the-good-shepherd--3805.jpg&w=550&h=298&ei=rdZGUu3iAoittAatsYGAAQ&zoom=1&ved=1t:3588,r:2,s:0,i:87&iact=rc&page=1&tbnh=165&tbnw=283&start=0&ndsp=12&tx=165&ty=74)
click view image
and wordpress imaguard plugin http://wordpress.org/plugins/imaguard/ (http://wordpress.org/plugins/imaguard/)
is a small plugin with a few functions and .htaccess rules but no not understand much
I hadn't looked at the Wordpress plugin, but here's how my idea should work. Somebody tries to directly view your example picture, which is located at
Quotehttp://www.allvip.us/gallery/albums/userpics/10001/angelina_jolie_000009.jpg
Now, you need to create an
.htaccess rule which passes that information as parameter to a new script instead of your static HTML page, let's call it
redirect.php. That script needs some simple code which queries the database to get the picture ID (pid) of the accessed image. It's easy, as Coppermine stores the picture's file path and name in the database (which you just passed to the script). In our example, the file's path would be
userpics/10001/
and the file's name
angelina_jolie_000009.jpg
which can be extracted with a regular expression or a combination of substr/strpos.
Now, a query like
SELECT pid FROM {$CONFIG['TABLE_PICTURES']} WHERE filepath = '{$filepath}' AND filename = '{$filename}'
should return the
pid and you can redirect the user to the intermediate-sized view. As I've currently no testbed where I can test this, I can just provide you this generic information. If you need code, please let me know and I'll create it as soon as possible.
I did paste your code in phpMyAdmin in the SQL and I got:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '['TABLE_PICTURES']} WHERE filepath = '{$filepath}' AND filename = '{$filename}' ' at line 1
yes I do need code
thanks
Of course this code won't work, as the variables need to filled with values. I'll add a proper .htaccess rule to my testbed and create working code now.
Adjust the last line of your .htaccess file to
RewriteRule ^(.*\.(jpg|jpeg|png|gif))$ http://www.allvip.us/redirect.php?$1 [NC,R,L]
and use this code for redirect.php
<?php
define('IN_COPPERMINE', true);
require('include/init.inc.php');
$file = $superCage->server->getEscaped('QUERY_STRING');
if (strpos($file, $CONFIG['fullpath']) === FALSE) {
header("Location: no_hotlinking.html");
} else {
$file = str_replace($CONFIG['fullpath'], '', $file);
$pos = strrpos($file, '/') + 1;
$filepath = substr($file, 0, $pos);
$filename = substr($file, $pos);
$pid = mysql_result(cpg_db_query("SELECT pid FROM {$CONFIG['TABLE_PICTURES']} WHERE filepath = '{$filepath}' AND filename = '{$filename}' LIMIT 1"), 0);
if ($pid) {
header("Location: displayimage.php?pid=".$pid);
} else {
header("Location: no_hotlinking.html");
}
}
Hello André,
I have a question,
Would not it be necessary to consider also $CONFIG['url']?
Regards.
That code worked in my testbed, as the query string just returned "albums/.../...".
Hello André,
I replaced "no_hotlinking.html" with an image file and always redirects to the image, never to the intermediate page.
I have verified that everything is correct as you wrote the code.
Regards.
Please post an example link to an intermediate-sized image in your gallery.
Hello André,
I am now using another hotlinking until I can use yours. I think your hotlinking will be very good.
An example here (http://wonmyo.com/albums/uploads/Logotipos/normal_acdc_logo.png), and I'm using this (http://forum.coppermine-gallery.net/index.php/topic,76414.msg368589.html#msg368589).
Regards.
http://wonmyo.com/albums/uploads/Logotipos/thumb_acdc_logo.png shows the image
http://wonmyo.com/albums/uploads/Logotipos/normal_acdc_logo.png shows the image
http://wonmyo.com/albums/uploads/Logotipos/acdc_logo.png redirects to http://wonmyo.com/direct-linking-not-permitted.jpg
Keep in mind that your browser may load images from cache, which will skip the
.htaccess rule.
I currently don't know what exactly
Quote from: Niecher on October 04, 2013, 09:51:22 PM
I replaced "no_hotlinking.html" with an image file and always redirects to the image, never to the intermediate page.
means. Do you get redirected to your "no_hotlinking.html" image file replacement or do you get the result as above?
Hello André,
In your code I replaced:
header ("Location: direct-linking-not-permitted.jpg");
instead of
header ("Location: no_hotlinking.html");
Always redirects to the image and I hope to be redirected to the intermediate page:
header("Location: displayimage.php?pid=".$pid);
Regards.
Please try again with this code for redirect.php and post the result:
<?php
define('IN_COPPERMINE', true);
require('include/init.inc.php');
echo $superCage->server->getEscaped('QUERY_STRING');
This new has the same behavior as above, redirects to hotlink image but not to the intermediate image page.
I tried this one too, and nothing:
echo $superCage->server->getEscaped('QUERY_STRING', '/^[a-zA-Z0-9&=_\/.-]+$/');
Regards.
It's not possible that the script redirects you, as it just prints the query string, nothing more. Please post the content of your .htaccess file.
Hello André,
I did above, is this (http://forum.coppermine-gallery.net/index.php/topic,76414.msg368589.html#msg368589). Anyway I also used htacces content posted above by allvip and with the new rule rewritte said by you to this file redirect.php.
I made sure to try following his instructions.
Regards.
I still don't know if you adjusted your .htaccess file properly. That's why I asked to post the current content.
Ahh Ok
For the plugin sef_url
I'm going to uninstall, I'll try without plugin, that I did not try.
Hello André,
I tested clean, without htaccess and without sef_url plugin.
It seems that does not work on my server. I see no need to disturb more.
With hotlink image is enough for me.
Thank you so much, always.
Of course it won't work without the .htaccess file.
Thank you very much Master,
There was a conflict with the plugin sef_url (removed).
Working perfectly 100%, first script you created, here (http://forum.coppermine-gallery.net/index.php/topic,76683.msg370326.html#msg370326)
This htaccess file is ready for redirect.php script, as follows:
<Files ".htaccess">
Order Allow,Deny
Deny from All
</Files>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.([Bb][Mm][Pp]|[Jj][Pp][Ee]?[Gg]|[Gg][Ii][Ff]|[Pp][Nn][Gg])$ [NC]
<FilesMatch "^(normal_(.*)|thumb_(.*)).([Bb][Mm][Pp]|[Jj][Pp][Ee]?[Gg]|[Gg][Ii][Ff]|[Pp][Nn][Gg])$">
RewriteCond %{HTTP_REFERER} !^$
</FilesMatch>
RewriteCond %{HTTP_REFERER} !^http(s)?://(.*)?example.com [NC]
RewriteCond %{REQUEST_URI} !^/example\.jpg$ [NC]
RewriteRule ^(.*\.([Bb][Mm][Pp]|[Jj][Pp][Ee]?[Gg]|[Gg][Ii][Ff]|[Pp][Nn][Gg]))$ http://example.com/redirect.php?$1 [R,NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.(7z|aac|arj|as(f|x)|avi|bz2|divx|docx?|f4(a|v)|flv|gtar|gz|lzh|m4(a|v)|midi?|mkv|mov(ie)|mp(2|3|4|e?g)|og(g|m)|pdf|qt|ra(m|r)|sw(c|f)|tar|tgz|wm(a|v)|zip)$ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(.*)?example.com [NC]
RewriteRule ^(.*\.(7z|aac|arj|as(f|x)|avi|bz2|divx|docx?|f4(a|v)|flv|gtar|gz|lzh|m4(a|v)|midi?|mkv|mov(ie)|mp(2|3|4|e?g)|og(g|m)|pdf|qt|ra(m|r)|sw(c|f)|tar|tgz|wm(a|v)|zip))$ http://example.com/redirect.php?$1 [R,NC,L]
Note: To be used in the root folder. Do not forget to replace example.com with your domain and example.jpg your image to redirect.
This other (http://forum.coppermine-gallery.net/index.php/topic,76414.msg368589.html#msg368589) htaccess file does not work with redirect.php, is a different file (free choice, what each one likes).
Regards.
Quote from: Αndré on October 04, 2013, 10:52:10 AM
Adjust the last line of your .htaccess file to
RewriteRule ^(.*\.(jpg|jpeg|png|gif))$ http://www.allvip.us/redirect.php?$1 [NC,R,L]
and use this code for redirect.php
<?php
define('IN_COPPERMINE', true);
require('include/init.inc.php');
$file = $superCage->server->getEscaped('QUERY_STRING');
if (strpos($file, $CONFIG['fullpath']) === FALSE) {
header("Location: no_hotlinking.html");
} else {
$file = str_replace($CONFIG['fullpath'], '', $file);
$pos = strrpos($file, '/') + 1;
$filepath = substr($file, 0, $pos);
$filename = substr($file, $pos);
$pid = mysql_result(cpg_db_query("SELECT pid FROM {$CONFIG['TABLE_PICTURES']} WHERE filepath = '{$filepath}' AND filename = '{$filename}'"), 0);
if ($pid) {
header("Location: displayimage.php?pid=".$pid);
} else {
header("Location: no_hotlinking.html");
}
}
thanks a lot.it worked.
I only edited a little the code for the .htaccess:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?allvip.us [NC]
RewriteRule ^(.*\.(jpg|jpeg|png|gif))$ http://www.allvip.us/gallery/redirect.php?$1 [NC,R,L]
because my gallery is not at allvip.us is in the folder gallery - allvip.us/gallery
I placed the .htaccess , redirect.php and no_hotlinking.html in the gallery folder.
In created a new .htaccess in the gallery folder only with your code and my original .htaccess with other website rules is still in the public_html
Hello allvip,
Thank you very much for your idea and request, this is a good hotlinking.
Regards.
even more thanks to andre that took the time to make it
thanks andre for the code is VERY, VERY important to a website.
If anyone wants all the gallery pages to redirect from example.com to www.example.com (useful for CloudFlare.CloudFlare has no point if users access the site without www.
CoudFlare runs only for www.)
code by Andre Reply #14:
http://forum.coppermine-gallery.net/index.php/topic,77210.0.html (http://forum.coppermine-gallery.net/index.php/topic,77210.0.html)
Redirect.php is making the CPU time and MySQL time very hight.
Greg told me:
This will result in a full scan of the cpg pictures table for every request because:
There is no index available that MySQL can use to find the result based on filepath and filename.
There is no information given to MySQL to allow it to stop after the first (and ONLY) match is found - it will complete the scan...
For the second issue - just adding 'LIMIT 1' to the SQL statement will tell MySQL to stop looking after the first match... We know there can only be one match (the same filepath/filename can only exist once - but MySQL doesn't know that...)
For the first issue - we need to add some kind of index. The perhaps obvious choice would be a 2 column unique index on filepath and filename. Unfortunatly we hit a MySQL limitation of 1000 byte key... Both filepath and filename at defined as varchar(255) - or 510 characters - but when saved in UTF8 - can take up to 3 bytes per character - which exceeds the limit.
We can reduce the amount of data in the index - or take an approach with 1 column index(es).
I would suggest adding the following index (via phpMyAdmin) using your correct cpg table prefix - expecting that in most cases your actual fieldlengths are smaller than 255...:
ALTER TABLE `cpg_pictures` ADD UNIQUE `filepath_filename` (`filepath` (200), `filename` (100))
and changing the SQL statement to:
SELECT pid FROM {$CONFIG['TABLE_PICTURES']} WHERE filepath = '{$filepath}' AND filename = '{$filename}' LIMIT 1
In phpMyAdmin I clicked the coppermine database - click SQL button on top - click clear - paste ALTER TABLE ... - click Go.
Error
SQL query:
ALTER TABLE `cpg15x_pictures` ADD UNIQUE `filepath_filename` (`filepath` (200), `filename` (100))
MySQL said: Documentation
#1061 - Duplicate key name 'filepath_filename'
with:
ALTER TABLE `cpg15x_pictures` ADD INDEX `filename` (`filename`)
Error
SQL query:
ALTER TABLE `cpg15x_pictures` ADD INDEX `filename` (`filename`)
MySQL said: Documentation
#1061 - Duplicate key name 'filename'
I can not even tell if the database is optimize.
Is proabibly not a coppermine dev problem,but please help.
How to optimize the database or just the cpg pictures table?
How to make redirect.php take less resources?
I can give my host cpanel password to Greg,Andre or Jeff Bailey.
Quote from: allvip on July 22, 2014, 06:37:41 PM
ALTER TABLE `cpg15x_pictures` ADD UNIQUE `filepath_filename` (`filepath` (200), `filename` (100))
MySQL said: Documentation
#1061 - Duplicate key name 'filepath_filename'
I assume you try to add the index more than once? If so, that error message is obvious. I just applied both queries on my Coppermine database successfully.
How can I tell if the pictures table is indexed now?
Quote from: Αndré on July 23, 2014, 10:20:39 AM
I assume you try to add the index more than once?
Yes.Twice.
The first time the error was:
Error
SQL query:
ALTER TABLE `cpg15x_pictures` ADD UNIQUE `filepath_filename` (`filepath` (200), `filename` (100))
MySQL said: Documentation
#1062 - Duplicate entry 'userpics/10001/-barbara_palvin_8.jpg' for key 'filepath_filename'
I deleted -barbara_palvin_8.jpg and tryed again.
Quote from: allvip on July 23, 2014, 11:04:14 AM
How can I tell if the pictures table is indexed now?
SHOW INDEX FROM cpg15x_pictures
Is that ok?
As you can see, the index exists.
Can you please,please help me index all the tables.
Is important for the database to use less server resources.
Is the default coppermine database.Nothing changed.
I belive all the copermine databases look the same.
Please start a new thread for that topic. Don't forget to add some information what exactly needs to be indexed in your opinion.
Can you please update reply#7 (redirect.php code) with LIMIT 1.
Is a good think for redirect.php to use less resources.
Use less server resouces is always better even if is a small gallery.
Done.
I forgot.
Greg also told me:
Comparison from 'EXPLAIN':
"EXPLAIN SELECT pid FROM `kmn_cpg_pictures` WHERE filepath = 'albums/' AND filename = 'pic.jpg' LIMIT 1"
(the values for filepath and filename don't matter for the EXPLAIN..)
No index:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE cpg_pictures ALL NULL NULL NULL NULL 973 Using where
Index:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE cpg_pictures ref filename filename 767 const 1 Using where
Notice the use of a key field (index) and reduction in number of rows estimated to be examined ... (973 is the number of pics in my test gallery).
I typed:
EXPLAIN SELECT pid FROM `cpg15x_pictures` WHERE filepath = 'albums/' AND filename = 'pic.jpg' LIMIT 1
and all I got:
see attachment
Please try again with valid data.
I think is a disaster.A lot of filename.
Maybe because afer ALTER...`filename` (100)) I used even:
I told Greg I got error with the first one so he told me:
OK... I expected the combination of filepath and filename to be unique - but since I had to use partial columns, they might not be in your case..
A simpler approach - add a non-unique index on filename - will greatly limit the set of rows that filepath has to be checked for...
Use this SQL to add (non-unique) index:
ALTER TABLE `cpg15x_pictures` ADD INDEX `filename` (`filename`)
From your screenshot it seems to work now - fine.
Regarding what Greg said:
Quote from: allvip on July 22, 2014, 06:33:59 PM
we hit a MySQL limitation of 1000 byte key... Both filepath and filename at defined as varchar(255) - or 510 characters - but when saved in UTF8 - can take up to 3 bytes per character - which exceeds the limit.
this probably doesn't apply to your gallery if you don't use single byte characters (e.g. all latin characters, digits). Even if you use multi characters in your file and folder names, it's unlikely (but theoretically possible) that you exceed the limit. As long as you don't use very long file and folder names which contain a lot of (or just) multi byte characters, you could also use the 2 column index. Additionally, as far as I know Coppermine transliterates all non-latin characters in file names during upload, so the only way multi byte characters could occur is in the folder names, which can just be created by the admin. The last sentence apply for cpg1.5.x, so if you upgraded your gallery from cpg1.4.x or earlier, file names may contain multi byte characters.
Yes but now I have possible keys filepath_filename,filename not just filepath_filename because I used the second one too that ADD INDEX `filename`.
Now I have added 2 index: filepath_filename with the first ALTER... and filename with the second.
How do I remove (undo) the second ALTER TABLE:
ALTER TABLE `cpg15x_pictures` ADD INDEX `filename` (`filename`)
I should have just filepath_filename if I use the first ALTER... or just filename if I use the second ALTER...
I made the mistake to use them both.
Made it.
ALTER TABLE cpg15x_pictures DROP INDEX `filename`
Thanks a lot for your help and all your time.I sure asked a lot of questions.
Is not copermine team problem to teach me the index stuff.