I have an issue where is_user_logged_in()
appears to alternate between being true and false.
Additionally, get_current_user_id()
gives the correct user ID if is_user_logged_in()
is true.
Here's my code:
.htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*)$ file-access.php?file=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
file-access.php:
require_once('wp-load.php');
if ( is_user_logged_in() ) {
// User is logged in, proceed.
} else {
// User is a guest, block.
}
I've verified the alternating status and user id using some error logging in the file-access.php
conditionals.
Do I need to call something other than wp-load.php perhaps? Just seems strange to me that it alternates between true and false, rather than always being false...
Edit to add: I also had an issue where one of my files I was testing with wasn't uploaded as the https version which caused issues.
www.example.com
, then you won't necessarily be logged in atexample.com
- depending on how the cookies were set.