views:

34

answers:

3

Hey everyone... I'm not too familiar with PHP... but I have PHP code that basically "includes" a file if a condition exists. This PHP code is going to be pretty lengthy and I was wondering if it was possible to create a .php file, and just INCLUDE THAT in my shtml files? What the below is doing is placing the correct navigation bar in the shtml file by running a series of if/else statements in PHP. If the REQUEST_URI (the current page) is xxxxx.shtml, then include THIS navigation bar file, etc. This is working perfectly, but as you can imagine for a site that has hundreds of pages, I would have to place each page in this below "conditional server side includes"- and the disadvantage to this is if some menu items get rearranged in the navigation bars, I want to be able to just edit ONE PHP file rather than every single shtml file that includes the below script. I HAVE NO IDEA how to include the below PHP into it's own file. I assume I need to leave out the as that's specific to HTML. But aside from that, is it just as easy as copying and pasting the above into it's own PHP file?

Currently, here's what I have in every one of my shtml files (it WILL get bigger, this is to just start out):

            <!--#if expr="$REQUEST_URI = /1/"-->
        <!--#include virtual="navFiles/featuresCurrent.shtml" -->
        <!--#elif expr="$REQUEST_URI = /2/"-->
        <!--#include virtual="navFiles/videosCurrent.shtml" -->
        <!--#elif expr="$REQUEST_URI = /3/"-->
        <!--#include virtual="navFiles/aboutusCurrent.shtml" -->
        <!--#elif expr="$REQUEST_URI = /4/"-->
        <!--#include virtual="navFiles/subscribeCurrent.shtml" -->
        <!--#elif expr="$REQUEST_URI = /5/"-->
        <!--#include virtual="navFiles/toolsCurrent.shtml" -->
        <!--#elif expr="$REQUEST_URI = /6/"-->
        <!--#include virtual="navFiles/membershipCurrent.shtml" -->
        <!--#endif --> 

I want to be able to create an individual .php file that contains the above code, and instead of putting the ABOVE in my shtml pages, I would just put something like "include xxxx.php". Is this possible?

A: 

Yes you put your code in a file and include that file like this:

<?php include ("path to file");?>

Have a look at include function.

Sarfraz
or maybe `require()`? http://www.php.net/manual/en/function.require.php
Piskvor
@Piskvor: True, if file not found, it will throw fatal error :)
Sarfraz
so on my php file that I will create, will I include the <?-- --> or leave them out? I'm assuming to leave them out, right? How will the syntax be on my php file, exactly how I have it above without the <?-- -->?
Mike Marks
@Sarfraz: which may or may not be the desired behavior; same thing with `include()` raising a warning and continuing.
Piskvor
+1  A: 

you can use

auto_prepend_file = /path/to/file.php

in your php.ini or .htaccess file which will "require_once" file.php before every php script.

kali
A: 

If you want this script to be available to your entire application, you should add its path to the library path in your php.ini by setting include_path = 'path\to\php\file'

Example of how to do that: http://www.cyberciti.biz/faq/how-do-i-set-php-include-path-in-php-ini-file/

js1568