<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.tigerheron.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>boot</title>
 <link>http://www.tigerheron.com/category/boot</link>
 <description>The taxonomy view with a depth of 0.</description>
 <language>en-US</language>
<item>
 <title>First steps with PHP - booting a script, Part 2</title>
 <link>http://www.tigerheron.com/article/2008/04/first-steps-php-booting-script-part-2</link>
 <description>&lt;p&gt;
  If you haven&#039;t already, you may want to start with
  &lt;a href=&quot;/article/2008/03/first-steps-php-booting-script-part-1&quot;&gt;Part 1&lt;/a&gt;
  of this topic. Part 2 of my discussion on booting begins with a digression.
&lt;/p&gt;

&lt;h2&gt;Frameworks and libraries&lt;/h2&gt;
&lt;p&gt;
  Some time ago, I read an article by
  &lt;a href=&quot;http://lerdorf.com&quot;&gt;Rasmus Lerdorf&lt;/a&gt; titled
  &lt;a href=&quot;http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html&quot;&gt;&lt;cite&gt;The no-framework PHP MVC framework&lt;/cite&gt;&lt;/a&gt;.
  It got me thinking about my framework, which at the time I was
  converting from PHP4 to PHP5.
&lt;/p&gt;
&lt;p&gt;
  In the good old days, if you wanted to create re-usable code, you
  would create a library of related functions or objects. Now, the
  excitement is all around frameworks. Everyone has one. What is the
  difference between the two?
&lt;/p&gt;
&lt;!--break--&gt;
&lt;p&gt;
  In my mind, a library:
&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Is under the control of the application&lt;/li&gt;
  &lt;li&gt;
    Provides a set of functions or objects for performing a closely
    related set of tasks
  &lt;/li&gt;
  &lt;li&gt;Has few pre-requisites&lt;/li&gt;
  &lt;li&gt;Works independently of other libraries&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
  A framework:
&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Controls the application&lt;/li&gt;
  &lt;li&gt;Tends to enforce a methodology&lt;/li&gt;
  &lt;li&gt;Performs a wide range of tasks&lt;/li&gt;
  &lt;li&gt;
    Contains code that is heavily dependent on the framework
    environment. It would be difficult to extract a single piece of
    functionality without dragging in most of the rest of the
    framework.
  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
  I like the synergy of the framework, but I also like the flexibility
  and lighter weight of a library. Frameworks tend to be biased to
  solving a specific kind of problem. I wanted something more general.
&lt;/p&gt;
&lt;p&gt;
  My framework would:
&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Be object-oriented&lt;/li&gt;
  &lt;li&gt;Be under the application&#039;s control&lt;/li&gt;
  &lt;li&gt;
    Provide an architecture that allowed addressing a wide range of
    tasks
  &lt;/li&gt;
  &lt;li&gt;
    Consist of &quot;libraries&quot; which worked synergistically but which
    would also allow integration of external functionality (through
    base classes and interfaces)
  &lt;/li&gt;
  &lt;li&gt;
    Minimize the amount of core classes needed to use the framework
&lt;/ul&gt;
&lt;p&gt;
  I know that calling my framework a &quot;framework&quot; creates some
  confusion. It is not a framework like CakePHP but it is a framework
  like the Java framework. Until a better term comes along, I&#039;ll stick
  with it.
&lt;/p&gt;

&lt;h2&gt;Back to booting&lt;/h2&gt;
&lt;p&gt;
  Let&#039;s return to our main topic. The digression was necessary because
  it led me to completely separate the boot and framework code. Each
  operates independently from the other.
&lt;/p&gt;
&lt;p&gt;
  Returning to where we left off last week, we know that each Web page
  begins with
&lt;/p&gt;
&lt;div&gt;&lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#039;Courier New&#039;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;require &lt;span style=&quot;color: #006080&quot;&gt;&#039;include.inc&#039;&lt;/span&gt;;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
  where include.inc contains:
&lt;/p&gt;
&lt;div&gt;&lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#039;Courier New&#039;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;$fileTop = ...;
require &lt;span style=&quot;color: #006080&quot;&gt;&quot;$fileTop/res/boot/boot.inc&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
  The boot.inc performs certain tasks but never directly accesses the
  framework code. The framework, in return, never directly uses any of
  the variables or functions defined in boot.inc.
&lt;/p&gt;

&lt;h2&gt;Directory structure&lt;/h2&gt;
&lt;p&gt;
  This is the directory structure I use:
&lt;/p&gt;
&lt;p&gt;
  &lt;img src=&quot;/files/blogs/developer/node116/boot-dir.png&quot; width=&quot;150&quot; height=&quot;147&quot; alt=&quot;1 res 2 boot 3 boot.inc 1 site 2 php 3 conf.inc 3 setup.inc 1 th&quot; title=&quot;&quot;&gt;
&lt;/p&gt;
&lt;p&gt;Notes:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
    The &quot;res&quot; directory contains all resources except images. This
    includes CSS, Javascript and PHP code.
  &lt;/li&gt;
  &lt;li&gt;
    The &quot;boot&quot; directory contains just one file, boot.inc. I just the
    same boot.inc file to boot many sites.
  &lt;/li&gt;
  &lt;li&gt;
    The &quot;site&quot; directory contains all the site-specific files. The boot
    system requires two files in res/site/php: conf.inc and setup.inc.
  &lt;/li&gt;
  &lt;li&gt;
    The &quot;th&quot; directory contains the Tiger Heron framework which I will
    cover in future articles.
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;The boot sequence&lt;/h2&gt;
&lt;p&gt;
  I can finally describe the boot sequence:
&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The server loads the Web page&lt;/li&gt;
  &lt;li&gt;The Web page loads include.inc&lt;/li&gt;
  &lt;li&gt;In include.inc, define $fileTop and load res/boot/boot.inc&lt;/li&gt;
  &lt;li&gt;In boot.inc, load res/site/php/conf.inc&lt;/li&gt;
  &lt;li&gt;
    Define some variables, set the include path and perform some basic
    initialization
  &lt;/li&gt;
  &lt;li&gt;Finish boot.inc by loading res/site/php/setup.inc&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
  The conf.inc file gives the site a &quot;hook&quot; for any special setup that
  must be performed immediately. It also allows the site to configure
  some site-specific values for boot.inc. Code in conf.inc is limited
  by the fact that the include path has not been set up. It also
  cannot rely on any functionality other than what is in boot.inc and
  what is bundled with PHP.
&lt;/p&gt;
&lt;p&gt;
  While conf.inc is customized for each site, I usually start with a
  template. The template indicates some values I may want to define
  and speeds up construction of new Web sites.
&lt;/p&gt;
&lt;p&gt;
  Boot.inc will:
&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;
    Define $webTop, the relative path to the top of the site. This is
    usually, but not always, the same as $fileTop. I use $fileTop to
    reference files in the file system and $webTop to reference files
    through the Web server (e.g. CSS files, Javascript files, etc.).
  &lt;/li&gt;
  &lt;li&gt;
    I determine whether this site is running on my PC, on a staging
    site or on a production site.
  &lt;/li&gt;
  &lt;li&gt;
    The error reporting is set based on where the code is running. On
    production sites, all error reporting is suppressed, while PC and
    staging sites report all errors.
  &lt;/li&gt;
  &lt;li&gt;
    I set up the include path based on the system type and values
    passed in by conf.inc.
  &lt;/li&gt;
  &lt;li&gt;I turn off user aborts, as these can wreck a database.&lt;/li&gt;
  &lt;li&gt;I remove magic quotes if they have been applied.&lt;/li&gt;
  &lt;li&gt;I allow for Macintosh line endings.&lt;/li&gt;
  &lt;li&gt;
    I set the content type to either text/html or
    application/xhtml+xml depending on whether I am using XHTML (a
    conf.inc option) and whether the browser supports
    application/xhtml+xml.
  &lt;/li&gt;
  &lt;li&gt;I use UTC as the timezone.&lt;/li&gt;
  &lt;li&gt;
    I define some absolute and relative paths to various commonly
    accessed places.
  &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
  Having completed this list of tasks, res/sites/setup.inc is called
  to finish off any site-specific setup. It is the setup.inc file that
  determines whether the framework code will or will not be included.
&lt;/p&gt;
&lt;p&gt;
  I&#039;ve left off one important step. The Web page, before including any
  code, can set define some values of its own. Let&#039;s say, for example,
  that you wanted to enable caching for some files and not others. The
  conf.inc file could include the default choice and each Web page
  could override that choice.
&lt;/p&gt;
&lt;p&gt;
  To make this clear, the Web page would start with:
&lt;/p&gt;
&lt;div&gt;&lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#039;Courier New&#039;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;$useCache = &lt;span style=&quot;color: #0000ff&quot;&gt;false&lt;/span&gt;;
require &lt;span style=&quot;color: #006080&quot;&gt;&#039;include.inc&#039;&lt;/span&gt;;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
  And the conf.inc file would contain:
&lt;/p&gt;
&lt;div&gt;&lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#039;Courier New&#039;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (!isset($useCache)) $useCache = &lt;span style=&quot;color: #0000ff&quot;&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
  Finally, the setup.inc file would set up the caching based on the
  value of $useCache.
&lt;/p&gt;
&lt;p&gt;
  The caching example points out that boot.inc doesn&#039;t include all the
  tasks you may need for some Web sites. For instance, there is
  nothing in boot.inc for handling caching and sessions. These are
  more complicated tasks and are supported by the framework. Since the
  boot sequence can&#039;t depend on the framework, these additional tasks
  are left to setup.inc. For rapid site building, I have a template
  for setup.inc, but I am free to change it as appropriate for the
  site.
&lt;/p&gt;

&lt;h2&gt;The .htaccess file&lt;/h2&gt;
&lt;p&gt;
  One of my goals was to be able to copy the entire site from my PC to
  the staging or production servers without requiring any code
  changes. Sometimes, code in the .htaccess file cannot be written to
  be system independent.
&lt;/p&gt;
&lt;p&gt;
  I used to have to maintain two copies of the .htaccess file, one for
  my PC and one for the hosting site. It occurred to me one day that I
  could start up my PC&#039;s Apache server with a -D DEVELOPMENT_ENV
  command-line option. This allows me to separate PC and hosting
  options using
&lt;/p&gt;
&lt;div&gt;&lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#039;Courier New&#039;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #800000&quot;&gt;IfDefine&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;DEVELOPMENT_ENV&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;gt;&lt;/span&gt;
  ...
&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #800000&quot;&gt;IfDefine&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
  and
&lt;/p&gt;
&lt;div&gt;&lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#039;Courier New&#039;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #800000&quot;&gt;IfDefine&lt;/span&gt; !&lt;span style=&quot;color: #ff0000&quot;&gt;DEVELOPMENT_ENV&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;gt;&lt;/span&gt;
  ...
&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #800000&quot;&gt;IfDefine&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Mission accomplished&lt;/h2&gt;
&lt;p&gt;
  If you go back to &lt;a
  href=&quot;/article/2008/03/first-steps-php-booting-script-part-1&quot;&gt;Part
  1&lt;/a&gt; of this topic, you&#039;ll see I have met all the goals I set for
  myself without violating any of the restrictions I listed.
&lt;/p&gt;
&lt;p&gt;
  The boot sequence is fast, performing only what I need to support my
  PC/staging/production development approach. It also initializes the
  PHP environment, since I can&#039;t count on being able to set php.ini or
  using php_flag and php_value in my .htaccess file.
&lt;/p&gt;
&lt;p&gt;
  The boot sequence provides two hooks for site customization. I can
  also choose to use it with or without my framework. This gives me a
  lot of flexibility.
&lt;/p&gt;
&lt;p&gt;
  If you want a closer look at this, I have packaged up the &lt;a
  href=&quot;/files/blogs/developer/node116/boot-sample.zip&quot;&gt;boot code&lt;/a&gt; for a
  site at www.sample.com. The package includes two Web pages,
  index.php and subdir/index.php with the supporting boot files. It
  does not include any framework code.
&lt;/p&gt;
</description>
 <comments>http://www.tigerheron.com/article/2008/04/first-steps-php-booting-script-part-2#comments</comments>
 <category domain="http://www.tigerheron.com/category/boot">boot</category>
 <category domain="http://www.tigerheron.com/category/php">PHP</category>
 <pubDate>Fri, 25 Apr 2008 22:22:13 +0000</pubDate>
 <dc:creator>developer</dc:creator>
 <guid isPermaLink="false">116 at http://www.tigerheron.com</guid>
</item>
<item>
 <title>First steps with PHP - booting a script, Part 1</title>
 <link>http://www.tigerheron.com/article/2008/03/first-steps-php-booting-script-part-1</link>
 <description>&lt;p&gt;
  Having covered &lt;a
  href=&quot;/article/2007/11/tale-two-patterns&quot;&gt;downloading and
  installing PHP&lt;/a&gt; and &lt;a
  href=&quot;/article/2008/01/eclipse-and-php&quot;&gt;setting up a development
  environment&lt;/a&gt;, I&#039;m now actually going to talk about
  &lt;em&gt;writing&lt;/em&gt; PHP code! Specifically, I will describe how I boot
  my PHP scripts and why I boot them the way I do. Part 1 shows how my
  requirements for code portability and maintainability influence the
  boot process. Part 2 will go into more detail about the specific
  steps executed by the boot code.
&lt;/p&gt;
&lt;!--break--&gt;
  &lt;h2&gt;My environment&lt;/h2&gt;
  &lt;p&gt;
    Here&#039;s some background: I develop sites for multiple clients.
    Initial coding occurs on my Windows XP system. When the code is
    running well, I upload it to the client&#039;s hosting system, which is
    typically Linux based.
  &lt;/p&gt;
  &lt;p&gt;
    I start by creating a new directory for my client&#039;s Web files (on
    my PC) and then creating an alias in my Apache configuration file
    to point to this location. For this article, let&#039;s assume I reach
    the Web site using http://localhost/sample.
  &lt;/p&gt;

  &lt;h2&gt;A sample task&lt;/h2&gt;
  &lt;p&gt;
    For our first PHP class, let&#039;s tackle something easy&amp;mdash;let&#039;s
    use PHP code to generate our sample site&#039;s navigation menu. Here&#039;s
    a quick sketch of the navigation class:
  &lt;/p&gt;
&lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#039;Courier New&#039;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;&amp;lt;?php
&lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Navigation
{
    &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; function printMainNav()
    {
        echo &lt;span style=&quot;color: #006080&quot;&gt;&#039;&amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;&amp;lt;a href=&quot;index.php&quot;&amp;gt;Home&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;....&#039;&lt;/span&gt;;
    }
}
?&amp;gt;
&lt;/pre&gt;
  &lt;p&gt;
    and here is the index.php page using the class:
  &lt;/p&gt;
&lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#039;Courier New&#039;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
&amp;lt;?php &lt;span style=&quot;color: #0000ff&quot;&gt;require&lt;/span&gt; &lt;span style=&quot;color: #006080&quot;&gt;&#039;Navigation.inc&#039;&lt;/span&gt;; ?&amp;gt;
&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #800000&quot;&gt;html&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #800000&quot;&gt;head&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;gt;&lt;/span&gt;
    ...
  &lt;span style=&quot;color: #0000ff&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #800000&quot;&gt;head&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #800000&quot;&gt;body&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;gt;&lt;/span&gt;
    &amp;lt;?php $navigation = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Navigation(); $navigation-&amp;gt;printMainNav(); ?&amp;gt;
      ...
  &lt;span style=&quot;color: #0000ff&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #800000&quot;&gt;body&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #800000&quot;&gt;html&lt;/span&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
  &lt;p&gt;
    At this point, the directory we created contains two files:
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;index.php&lt;/li&gt;
    &lt;li&gt;Navigation.inc&lt;/li&gt;
  &lt;/ul&gt;
  &lt;p&gt;
    The code works, but we&#039;re already seeing some potential pitfalls.
    First, we don&#039;t want to place the PHP classes in the same directory as
    the Web pages. Secondly, our sample system will eventually grow to
    where we will use sub-directories to organize our Web pages. The
    problems we see are:
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      Each Web page will need to include the Navigation.inc file based
      on the Web file&#039;s location. If we ever change our mind about
      where to place Navigation.inc, a lot of files will need changes.
    &lt;/li&gt;
    &lt;li&gt;
      The link to the home page (in the navigation), as written, only
      works for Web pages at the top of the directory tree.
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;p&gt;
    It may seem that there are some easy work-arounds for these two
    problems. For instance, setting the path to the Navigation.inc file in
    the php.ini file solves the first problem. And linking to
    &quot;/sample/index.php&quot;, solves the second. The reason these solutions
    aren&#039;t ideal lies with the environment in which I work.
  &lt;/p&gt;

  &lt;h2&gt;Goals and requirements&lt;/h2&gt;
  &lt;p&gt;
    These are my goals:
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;I want to use a consistent approach for all my clients.&lt;/li&gt;
    &lt;li&gt;I want to allow for sub-directories in the Web site.&lt;/li&gt;
    &lt;li&gt;
      I want all Web files to have a consistent way of including PHP
      code, regardless of their location on the site. This lets me use
      the same template for all files and to move a file from one
      place to another without having to edit it.
    &lt;/li&gt;
    &lt;li&gt;
      I don&#039;t want the Web pages to use any complicated calculations
      in order to include PHP code. Complicated code should be stored
      in one place, not scattered in every Web page.
    &lt;/li&gt;
    &lt;li&gt;
      I want to be able to upload the code from my Windows system to
      the hosting system without &lt;em&gt;any&lt;/em&gt; changes and have it work
      in both environments.
    &lt;/li&gt;
    &lt;li&gt;
      Occasionally, I also want the code to work without change on a
      testing site on the hosting system. This lets the client preview
      site changes without affecting the site the customers sees.
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;p&gt;
    These are the restrictions I have:
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      I can&#039;t alter the php.ini file since shared hosting services
      usually don&#039;t provide access to this file.
    &lt;/li&gt;
    &lt;li&gt;
      I can&#039;t place php_flag or php_value statements in the .htaccess
      file. These only work if PHP is run as an Apache module and some
      hosting companies use Fast CGI. &lt;em&gt;(Note: apparently, this
      limitation will disappear in PHP 5.3.)&lt;/em&gt;
    &lt;/li&gt;
    &lt;li&gt;
      I can&#039;t assume I can reference a Web page at the top of the site
      by starting the path with &quot;/&quot;. This might work for the final
      site, but not for the site on the PC, where the top is at
      /sample.
    &lt;/li&gt;
  &lt;/ul&gt;

  &lt;h2&gt;My current solution&lt;/h2&gt;
  &lt;p&gt;
    I have not found the perfect solution. What I use is pretty close:
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt; Each Web page begins with &amp;lt;?php require &#039;include.inc&#039;; ?&amp;gt;.&lt;/li&gt;
    &lt;li&gt;
      Each Web site directory contains an include.inc file with the
      following contents:
&lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#039;Courier New&#039;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none&quot;&gt;
&amp;lt;?php
  $fileTop = &lt;span style=&quot;color: #006080&quot;&gt;&#039;.&#039;&lt;/span&gt;;
  &lt;span style=&quot;color: #0000ff&quot;&gt;require&lt;/span&gt; &lt;span style=&quot;color: #006080&quot;&gt;&quot;$fileTop/res/boot/boot.inc&quot;&lt;/span&gt;;
?&amp;gt;
&lt;/pre&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;p&gt;
    The advantages are:
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      We can move Web files from one directory to any other without
      editing.
    &lt;/li&gt;
    &lt;li&gt;
      The variable $fileTop identifies the file path to the top of the
      root of the Web site.
    &lt;/li&gt;
    &lt;li&gt;
      $fileTop is a relative path, so the Web site tree can be located
      anywhere in the site&#039;s document root.
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;p&gt;
    It&#039;s not a perfect solution because I still have to create one
    include.inc file per directory. Of course, that&#039;s not very many
    files and the contents are not complicated. Any heavy lifting can
    be relegated to the boot.inc file.
  &lt;/p&gt;
  &lt;p&gt;
    It&#039;s also not perfect because it requires an extra file load for
    the include.inc file.
  &lt;/p&gt;
  &lt;p&gt;
    It assumes that &#039;.&#039; is in the php.ini search path. So far, this has
    proven to be a safe assumption.
  &lt;/p&gt;

  &lt;h2&gt;Alternatives&lt;/h2&gt;
  &lt;p&gt;
    Drupal and a number of other systems use a common dispatcher page
    for all Web site accesses. Every page loads index.php, which then
    includes the desired content. $fileTop is always &#039;.&#039;.
  &lt;/p&gt;
  &lt;p&gt;
    Using Apache&#039;s mod_rewrite module, the process could work like this:
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;The user enters http://www.sample.com/somepage.&lt;/li&gt;
    &lt;li&gt;Mod_rewrite converts this to http://www.sample.com/index.php?q=somepage.&lt;/li&gt;
    &lt;li&gt;The index.php file is loaded.&lt;/li&gt;
    &lt;li&gt;
      The index.php can directly contain the boot.inc code. This saves
      one include/require.
    &lt;/li&gt;
    &lt;li&gt;It uses the &#039;q&#039; parameter to determine which page the user really wanted.&lt;/li&gt;
    &lt;li&gt;That Web page somepage.inc is included.&lt;/li&gt;
  &lt;/ul&gt;
  &lt;p&gt;
    This eliminates two file includes. There is some hidden cost in
    that the mod_rewrite script probably has to do a file check to see
    if the requested Web file actually exists&amp;mdash;this allows access
    to CSS, Javascript and image files that you may not want handled
    by PHP.
  &lt;/p&gt;
  &lt;p&gt;
    One problem with this approach is that my Web pages can set some
    options &lt;em&gt;before&lt;/em&gt; loading the include.inc file and these
    options control aspects of the boot process (e.g. whether to use
    caching, whether to use sessions, etc.). I will discuss this more
    in Part 2 of this article; we might lose this flexibility in the
    single dispatcher approach or we might have to work around it in a
    way that voids the performance gain. Not having tried this
    alternative, I&#039;m not sure of the answer.
  &lt;/p&gt;
  &lt;p&gt;
    Do you have an even better approach? Please let me know.
  &lt;/p&gt;

  &lt;h2&gt;More to come&lt;/h2&gt;
  &lt;p&gt;
    In Part 2, I will discuss what happens when the boot.inc file is
    executed. Stay tuned!
  &lt;/p&gt;
</description>
 <comments>http://www.tigerheron.com/article/2008/03/first-steps-php-booting-script-part-1#comments</comments>
 <category domain="http://www.tigerheron.com/category/boot">boot</category>
 <category domain="http://www.tigerheron.com/category/php">PHP</category>
 <pubDate>Tue, 18 Mar 2008 23:57:56 +0000</pubDate>
 <dc:creator>developer</dc:creator>
 <guid isPermaLink="false">114 at http://www.tigerheron.com</guid>
</item>
</channel>
</rss>
