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?
Say your config file is in
/system/config.php. in config.php add…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.
Now you don’t have to worry about where you are in your site.