tags:

views:

111

answers:

4

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?

+1  A: 

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

allanmc
A: 

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?

mmundiff
+5  A: 

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.

Galen
+1, very clean and flexible solution
Richard Nguyen
I understand the concept of this, but I don't know how the config file you refer to is run so that the variables can be difined. Can you please explain how the config.php works?
Dennis Hodapp
You include your config file (which contain variables for your directories) in every page
Galen
A: 

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).

SerpicoLugNut