I use WordPress for a private site where users upload files.
I use the “Private WordPress” to prevent access in to the site if the user is not logged in.
I would like to do the same to the files uploaded in the uploads folder.
So if a user its not logged in they wont be able to access to :
https://xxxxxxx.com/wp-content/uploads/2011/12/xxxxxxx.pdf
if they try to access but they are not logged then they should be redirected to login page for example.
I found a plugin called private files but last time updated was in 2009 and it does not seems to work on my WordPress.
Anyone know any method?
Hotlinking method will be enough to protect this?
I also found this method :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^.*uploads/private/.*
RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in.*$ [NC]
RewriteRule . /index.php [R,L]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
But then any user that replicate the cookie could pass this right?
Regards
Only checking if the cookie exists, is not much of a strict protection.
To get a stronger protection, you can pass or “proxy” all requests to the uploaded folder (exemplary
uploads
in the following example) through a php script:All requests to uploaded files (which includes images in posts) would go to
dl-file.php
which then can do verify if the user is logged in or not.If the user is not logged in, your sites login-form will be shown. After the user logged in, she will get redirected back to the file and can download it now.
Exemplary
dl-file.php
.Something similar can be found in
wp-includesms-files.php
in your wordpress installation, but that one is for multisite and w/o the login check and redirects.Depending on how much traffic you have, it could be wise to better integrate this with your server, e.g.
X-Accel-Redirect
orX-Sendfile
headers.Two ways, simple in 2. with the help of an apache rule or in 1. with the help of custom code in a plugin.
1. Plugin
You can write a plugin using the
init
hook and the get-value$_GET[ 'file' ];
. If the user has this get-value, jump in a function to check the rights for access on the files: For example, with a checkbox inside a Meta Box.the function
fb_get_file()
You can also add a custom URL for files via the hook
generate_rewrite_rules
2. Apache check for the Cookie
Leave a new .htaccess file inside of the
/wp-content/uploads/
directory. Or an other defined directory for the uploads.How it works
Inside of the
<IfModule>
containers, there are three rules that do the following:wordpress_logged_in_
The trick here is step 2, then check for the absence of a cookie that begins with
wordpress_logged_in_
. When the user is logged in, WordPress adds a cookie to your browser that looks like:Example rule with a check for file type
If you would like a plugin-based approach to solving this problem, here is a reasonably good solution that I have (finally) found:
https://wordpress.org/plugins/download-monitor/
website here: https://www.download-monitor.com/kb/adding-downloads/.
Take note of the ‘Download’ shortcode provided for you (eg. save to
Notepad). Note that the file gets saved in
/wp-content/uploads/dlm_uploads/
This means that anyone not logged in cannot either download the file or see the real URL to the file. If in the event that someone unauthorised figures out the URL to the file, the plugin also stops users browsing to the real file URL by blocking access to the
/wp-content/uploads/dlm_uploads/
folder.Bonus: if you a doing this for a site where you need users to be able to login as ‘Members’ only (but have no WordPress permissions like page editing or being an Admin), install the ‘Members’ plugin https://wordpress.org/plugins/members/, create a new user role called ‘Member’, and give it the single capability of ‘read’, create a new User in WordPress, and make sure to give them a role of ‘Member’.
If you want protect the content of pages, the ‘Members’ plugin provides some options, or there are other plugins out there. If you want to theme the login page for Members to look better than the WordPress default login form, use something like ‘Theme My Login’: https://wordpress.org/plugins/theme-my-login/
What about a plugin-based approach to solving this problem, I’ve found a WP Plugin created to approach this:
Prevent files / folders access: https://wordpress.org/plugins/prevent-file-access/