Highlighted

Using root-relative paths for PHP includes...

Contributor ,
Nov 05, 2018

Copy link to clipboard

Copied

Hi,

I'm an average HTML programmer who knows just enough about PHP to use includes.

I have a simple website layout that uses PHP includes like this :

<?php include ('../_header.php'); ?>

<?php include ('content.php'); ?>
<?php include ('../_footer.php'); ?>

Can't use root-relative paths in includes because, as you pros already know, root-relative on the server means something completely different.

Is there a quick and easy way for me to remedy this, perhaps with an added line in either the header or htaccess file? The convenience of being able to move files outside of DW without having to worry about links like these is worth the initial bother, I find.

Thanks!

Adobe Community Professional
Correct answer by Paul-M | Adobe Community Professional

Try this:

include($_SERVER['DOCUMENT_ROOT'].'somefile.php')

Views

394

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Using root-relative paths for PHP includes...

Contributor ,
Nov 05, 2018

Copy link to clipboard

Copied

Hi,

I'm an average HTML programmer who knows just enough about PHP to use includes.

I have a simple website layout that uses PHP includes like this :

<?php include ('../_header.php'); ?>

<?php include ('content.php'); ?>
<?php include ('../_footer.php'); ?>

Can't use root-relative paths in includes because, as you pros already know, root-relative on the server means something completely different.

Is there a quick and easy way for me to remedy this, perhaps with an added line in either the header or htaccess file? The convenience of being able to move files outside of DW without having to worry about links like these is worth the initial bother, I find.

Thanks!

Adobe Community Professional
Correct answer by Paul-M | Adobe Community Professional

Try this:

include($_SERVER['DOCUMENT_ROOT'].'somefile.php')

Views

395

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 05, 2018 0
Adobe Community Professional ,
Nov 05, 2018

Copy link to clipboard

Copied

Try this:

include($_SERVER['DOCUMENT_ROOT'].'somefile.php')


Paul-M, ACP - www.webspectrum.co.uk

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 05, 2018 0
Contributor ,
Nov 05, 2018

Copy link to clipboard

Copied

Energize  wrote

Try this:

include($_SERVER['DOCUMENT_ROOT'].'somefile.php')

You just changed my life.

So I just repeat this everywhere I use a root-relative path?

Works for me, thanks!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 05, 2018 0
Paul-M LATEST
Adobe Community Professional ,
Nov 07, 2018

Copy link to clipboard

Copied

I don't know what your set up is, but yes is probably the short answer. If you need different output for your local testing server for example you might want to use if / else:

<?php

$dev = array('localhost', '127.0.0.1');

//Check If local testing server before outputting stuff

if(in_array($_SERVER['HTTP_HOST'], $dev))  {

        // DO SOMETHING IF LOCALHOST DETECTED

}else {

        // DO SOMETHING ELSE IF NOT LOCALHOST

    }

?>


Paul-M, ACP - www.webspectrum.co.uk

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 07, 2018 0