I am protecting a website with php sign-in form created with Laravel framework. When I type in the URL to get there (192.168.2.140/demosite/index.php) I get a blank page, no error. When I look at the Apache2 error.log it says the following:
[Fri Dec 07 12:23:24.379754 2018] [php7:notice] [pid 992] [client 192.168.2.138:52745] PHP Fatal error: require_once(): Failed opening required '/var/www/html/phplogin/public/demosite/../extra/auth.php' (include_path='.:/usr/share/php') in /var/www/html/phplogin/public/phplogin/index.php on line 4
I don't know how to get rid of the "demosite" embedded in the above DocumentRoot path so that I can get the real URL path as:
/var/www/html/phplogin/extra/auth.php
Details of My set-up:
I'm using Ubuntu LAMP Server 18.04.1 with Laravel Framework 5.7. I created a Laravel Project to the /var/www/html/ directory named phplogin. I can see my php login page just fine from the Laravel public directory from within the phplogin directory. Now I want to protect an already made and ready to go website. I created a directory name demosite inside the Laravel public folder and moved my ready to go website over to it.
To protect the ready made website, I add php code to the html code and change the extension from .html to .php. The PHP code needs to authenticate the user first, so it goes and looks for the Laravel root path which is the phplogin directory.
I disabled the defailt 000-default.conf file and created the laravel.conf file and inserted the following:
<VirtualHost *:80>
ServerName skydonuts.tld
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/phplogin/public
<Directory /var/www/html/phplogin>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The .htaccess in the Laravel public directory has the following contents:
<IfModule mod_rewrite.c>
#
<IfModule mod_negotiation.c>
# Options -MultiViews -Indexes
#</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
The PHP code that I add to protect the website is this:
<?php
// should be equal to: PATH_TO_PHPLOGIN_FOLDER/extra/auth.php
require_once __DIR__ . '/../extra/auth.php';
// reloads the page after authenticating
$currentUrl = (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
// actual authentication process
if (! Auth::check()) {
redirectTo('login?to=' . $currentUrl);
}
?>