Good day, I am making a website. I'd like this site to have an account-system. Following multiple tutorials and teaching myself, I believe I've made a decent system using Apache and MySQL.
However, when linking the PHP files from the "assets/private" folder, it doesn't appear to work within subfolders. For example, this is my autoload.php
file which runs the database functions;
<?php
session_start();
ini_set("display_errors",0);
require "../private/database.php";
require "../private/functions.php";
?>
And here is the code on the top of every HTML file to prevent those that are not logged in from viewing its pages;
<!php
require '../private/autoload.php';
$user_data = check_login($connection);
$username = '';
if(isset($_SESSION['username']))
{
$username = $_SESSION['username'];
}
?>
All of that works perfectly! But as you can see, it works within one "folder path", with ../private/autoload.php
. I need it to run within multiple folders, potentially folders of folders. Rather than have it relative, which appears to not be working, is there a way to create an absolute link?
require '/websitename/private/autoload.php';
$user_data = check_login($connection);
I have tried that example, which works for stylesheets, JavaScript files, images and videos, yet it does not work with PHP files for some reason.
These paths work;
- /websitename/home/index.php - Home Page
- /websitename/home/changelog.php - Changelog page
And the Library paths;
- /websitename/library/index.php - Library page
- /websitename/library/list.php - List page
But subfolders do no work;
- /websitename/library/article/index.php - Library > Article page
- /websitename/library/countries/index.php - Library > Country page
- /websitename/library/countries/country/index.php - Library > Country > CountryName page
How would I apply an absolute path to fix the subfolder issue?
Or is there another solution? I have looked this up on multiple sites and nothing appears to give an answer. I have seen one vague answer that apparently the .htaccess
file may be require an option setting to tell each file where it's getting it's information from, since by "default", it's within one folder-path yet didn't explain which settings that would be.
Hopefully this all makes sense! First post so I hope I met the requirements <3
CodePudding user response:
its hard to tell how your project handling your routes. I suggest to check in project files for router file or a main controller that may implode the url and create a response when it finds the controller. you can create a final path variable at the entry of your project and use global in to require file from everywhere you need for example:
[index.php]
define('BASE_DIR', str_replace("\\", "/", dirname(__DIR__)));
[file.php]
$path = BASE_DIR.'/public/img/project/'.$file_name;
you can also check composer project it has autoload that you can check on the web. I hope this answer find useful
CodePudding user response:
How would I apply an absolute path to fix the subfolder issue?
Unlike .html, .css, .js, images & video files, that have their paths within the main site folder, paths from php code within .php files are taken from the server's source. Which, for me using Apache results in;
require "C:/xampp/htdocs/website/database/autoload.php";
Thank you for your answers!