Making includes easy.... Am I a 'tard?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello all,

  
Here's the situation. I have a site where each php page contains the
"header" (top bar), "navigator" (left-side bar), the "footer" (bottom
bar), and the css file. Each of those four files are in a folder
called includes in the site root folder (sorry if the terminology is
incorrect).

So, on a webserver hosting numerous virtual sites, I have something
like this:


Apache
  htdocs
    site1
      includes
        (those four files)
    site2
      includes
        (those four files)
    etc...

Apache is set up so that when you hit http://site1 it brings up the
index page in the site1 folder, and for http://site2 you get the index
page in the site2 folder.

So for the purpose of this question, I'm going to call each of those
the site root folder for that particular site.

Now, those four files are different for site1 and site2 in terms of
different buttons used etc., so they aren't the same in terms of being
able to use the exact same four files for both sites.

The problem I have is that when including these files, I don't want to
have to mess with actually putting the correct number of "../" before
"includes/top.php" as well as the other three files I want to include
on each page. It's not really a hassle, so to speak, until you do
something like move an entire subfolder of pages (which might contain
more subfolders of pages) into another location. Then you have to go
back and fix the number of "../" for each page that was affected.

The include function either wants the relative path "../" or it wants
the URL. It's just a personal hangup of mine to not hardcode things in
like that (yes I use a config.php file for stuff like that). If that's
not good enough, the other reason is that there is a production server
and development server for these two sites which have different URLs,
so you'd have to fix all the URLs to work on the production server
once you were done.

I read through php.net on things like dirname(), realpath(),
pathinfo(). include(), require(), etc... But I couldn't find a
"dynamic" way of doing it.

So, I decided to write my own which I'll show here, but my question is
whether or not there is an easier way. As it stands now, I have to put
that function on every page, which is ok because I only have to do it
once, but it seems extremely redundant.

Well, here it is.


****************

function dynRoot()
{
  $levels = substr_count($_SERVER['PHP_SELF'],"/");

  for ($i=0; $i < $levels - 1; $i++)
  {
    $relativeDir .= "../";
  }
  
  return $relativeDir;
}

****************


Which then allows me to do the below:


****************

<HEAD>
  <link rel="stylesheet" type="text/css" href="<? echo dynRoot();?>includes/site.css"/>
</HEAD>


<?php include(dynRoot() . 'includes/footer.php');?>

****************


Yes, I know I don't have to do that for the css up there, but I was
playing around with it and just wanted to show it worked there too.


So far, I haven't run into any issues regardless of how deep the page
is buried in subfolders.


The question is.... Is there a better way.


Thanks for any help.



Tagline of the day:
The trouble with being punctual
is that nobody's there to appreciate it.




-- 
Leif (TB lists moderator and fellow end user).

Using The Bat! 3.0.2.3 Rush under Windows XP 5.1
Build 2600 Service Pack 2 on a Pentium 4 2GHz with 512MB

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux