Jump to content

Include files in php, problem with Root


Go to solution Solved by YndiaPaleAle,

Recommended Posts

Hi

I have a lack of understanding how root folder works, together with the option "include" files in different folders.

This is my simple example (I taylored the problem):

I have index.php in the root folder.

<!DOCTYPE html>
<html lang="de">
	<?php include "./phpadds/head.php"; ?>
    <?php include "./phpadds/header.php"; ?>
	<body>
		<h1>Hello</h1>
	</body>
</html>

Here both include order (head.php and header.php works fine.

header.php

</header>
	<div>
		<a href="./index.php"><span class='Ub2'>Home</span></a>
		<img src='./src/img/favicon/new.jpg' alt='Neu' width= '35'>
	</div>
</header>

head.php

<head>
	<link rel="stylesheet" type="text/css" href="./src/css/styles.css">
</head>

The styles.css is defined in head.php and works too. 

h1 {
  text-align: center;
  color: red;
}

Now i create a new folder "google", there in copy the index.php from my root (root(google/index.php).

Now both includes are not working, because they are not pointing to the right place.

<?php include "./phpadds/head.php"; ?>
<?php include "./phpadds/header.php"; ?>

I can change them to: 

<?php include "../phpadds/head.php"; ?>
<?php include "../phpadds/header.php"; ?>

Now, header.php is visible, but then still styles.css and all other links inside head.php and header.php are not working, because the point to the false folder (./)

What do I have for possibilities to manage this behaviour, so that links and includes are working ?

Root on my local server and on my hosting server is not the same.

Yves  

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/316102-include-files-in-php-problem-with-root/
Share on other sites

There are two paths you have to think about: the path to files as they exist on your server, and the URL that you see in the address bar of your browser.

For files on your server, it's best to always use absolute paths all the time for everything. That way there's no confusion about "well what if I put this in a different place, then I have to change all the paths".
To get an absolute path you can use the DOCUMENT_ROOT in $_SERVER. That will tell you what directory is the root of your website, then you can continue building a path from there.

include $_SERVER["DOCUMENT_ROOT"] . "/phpadds/head.php";

For URLs, it's common to use absolute paths all the time for everything. Again, so there's no confusion.
This time you start everything with a leading slash for the root of your website, then build from there.

<a href="/index.php">This one always goes home</a>
<a href="/google/index.php">This one always goes to the Google thing you set up</a>

 

Thanks for your answer. I understood, but still have problems (I am not a programmer, I just do this for fun for my website): 

https://www.swissbeerglasses.com/ 

I have this configuration:

Root on server
string(33) "/home/bierglae/www/Webseite.ch"

Root local
string(17) "/var/services/web"

How can I solve the different  $_SERVER["DOCUMENT_ROOT"] folder ?

Now I have to include /Webseite.ch/ in the code (local not working without /Webseite.ch/:

</header>
	<div>
		<a href="/index.php"><span class='Ub2'>Home</span></a>
		<img src='/Webseite.ch/src/img/favicon/new.jpg' alt='Neu' width= '35'>
	</div>
</header>

  

<head>
	<link rel="stylesheet" type="text/css" href="/Webseite.ch/src/css/styles.css">
</head>

On the server not working this configuration too:

<head>
	<link rel="stylesheet" type="text/css" href="/src/css/styles.css">
</head>

Include needs this configuration, which just works local on my Synology NAS webserver.

<?php include $_SERVER["DOCUMENT_ROOT"] . "/Webseite.ch/phpadds/head.php"; ?>
<?php include $_SERVER["DOCUMENT_ROOT"] .  "/Webseite.ch/phpadds/header.php"; ?>

 

Sorry for this confusion, I have to find the "fist step" 

20 hours ago, YndiaPaleAle said:

I have this configuration:

Root on server
string(33) "/home/bierglae/www/Webseite.ch"

Root local
string(17) "/var/services/web"

That does not make sense.

What is the value of the DOCUMENT_ROOT? I would expect it to be /home/bierglae/www/Webseite.ch. I don't know what the /var/services/web path is supposed to represent.

  • Solution

Oh yeah, I see. iIt seems, that I have to change the Document_root on the Synology WebStation. I tried this,  configuration looks good, but still not working. I will investigate more in this WebStation topic and close this one.  Thanks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.