Dennis
Dennis

Reputation: 616

Base Location for PHP

I recently decided to incorporate PHP into my website, but I've never worked with it before so I'm not familiar with the functions...

My problem is that I am using the include() method for my header and footer on each PHP page like this:

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

This navigates from /public_html directory to the /php directory in the root of my website. To account for the different sub-directory levels that files may be located, at the top of each PHP page I add:

<?php
/* Path prefix so relative links work correctly */ 
$path = "../";
?>

and ../ could be replaced with ../../ for a file another sub-directory down and so on, and in the header and footer files, links are preceded like this:

<a href="<?php echo($path); ?>index.php" title="Home">Home</a>

and everything works fine like this...until I try to use it with wordpress which is located in a sub-directory on my website. Wordpress uses dynamic pages and so sometimes pages are located in a sub-directory lower than other pages and the links will fail to work with this method. In my old site design, I used the tag to set all link references to the root/public_html directory of my site. However, PHP is not affected by this tag. What I need is a method to set the the link references from the /public_html directory for my include() methods on the blog.

Is there such a method or is there a better way to do what I am doing?

Upvotes: 2

Views: 607

Answers (4)

Galen
Galen

Reputation: 30170

Say your config file is in /system/config.php. in config.php add...

define( 'DIR_BASE', dirname( dirname( __FILE__ ) ) );
define( 'DIR_INCLUDES', DIR_BASE . 'includes/' );

Now as long as your config.php file doesn't move, your base directory will always be 2 directories below it. You can use these in all of your files that include config.php.

require DIR_INCLUDES . 'header.php';

Now you don't have to worry about where you are in your site.

Upvotes: 5

TWLATL
TWLATL

Reputation: 2879

Your best bet is to use either $_SERVER['SERVER_NAME'] or $_SERVER['DOCUMENT_ROOT'], depending upon which one better suits your directory traversing needs (document root will allow you to go deeper than the root directory, which would be helpful if you keep your includes a directory below your web root).

Not clear though on how this would mess with Wordpress, and WP has its own settings to determine the root (set in the wp_options table).

Upvotes: 0

mmundiff
mmundiff

Reputation: 3951

Can you get to the Wordpress Admin section?

You do not need to include any files into Wordpress really. It kind of stands by itself. Or maybe I am misunderstanding the question.

Like the above person said you can specify $_SERVER['DOCUMENT_ROOT'] as your root and then build url's off of that.

EG:

<?php

echo '<a href="'.$_SERVER['DOCUMENT_ROOT'].'/subfolder/subfolder/file.php">Link To page</a>';

?>

Does this help or do you still have questions?

Upvotes: 0

allanmc
allanmc

Reputation: 179

You could take a look at $_SERVER['DOCUMENT_ROOT']. It will return the document root directory, as defined in the server's configuration file.

Upvotes: 1

Related Questions