Can One PHP Includes Change, Change All Pages

Sitepoint Members,
I have a 3 column website. Isn’t there a way to use php includes so I can use one solitary includes file (not copies) to create the left column for all the pages and another includes file to create the right column for all the pages? This way one change in an includes file would make the change for all of the pages of the website.

Thanks for the help,

Chris

Sure you can.

stick <?php include_once(‘left.php’); ?> in where the left column should be (and then create a ‘left.php’, obviously)

But what you might want to consider doing is taking everything before your main content, put it into a ‘header’ file, take everything after your main content, and put it into a footer file.

Now, your main pages would look like:


<?php include_once('header.php'); ?>
<p>This is the main content page. Stuff goes here.</p>
<?php include_once('footer.php'); ?>

StarLion,
So if I create a file collage.html (where everything is assembled to make the page), I can write the peripheral files of includes as left.php and right.php and in collage.html I would call for these includes php files with

<?php include_once(‘left.php’); ?>
and
<?php include_once(‘right.php’); ?>

Is “once” for both files right?

Thanks,

Chris

Yes - you only want to include each of the two files once in each page.

Also you don’t need the () around the filename as include_once is not a function call.

Felgall,
So like this
<?php include_once ‘left.php’ ?>
and
<?php include_once ‘right.php’ ?>

What if I put the ‘right’ and ‘left’ php files in the web root of my webhost account? Would the above calls look like

<?php include_once ‘/left.php’ ?>
and
<?php include_once ‘/right.php’ ?>

Thanks,

Chris

Almost.

In order for the PHP code to be read, unless your server is very oddly configured, you should make it college.php instead of college.html. HTML files dont get fed through the PHP parser.

StarLion,
So have inside index.php file (formerly collage.html) these two lines to call for the left and right columns

<?php include_once(‘left.php’); ?>
and
<?php include_once(‘right.php’); ?>

Is that right?

Chris

((that would be /‘left.php’ and /'right.php))

Include IS a structure call, btw, but the ()'s are indeed optional (as they are with echo(), etc). It is equivalent to if(). A structural element.

As far as the / or not /, the simple explanation is that without a /, the system will look in the file’s current directory for the include file. If you put the leading /, it will go to the siteroot, and then navigate to the file.

EX:
If i had a file located at /example/test.php…


include_once('foo.php'); //Looks for http://www.yoursite.com/example/foo.php
include_once('/bar.php'); //Looks for http://www.yoursite.com/bar.php
include_once('/example/cow.php'); //Looks for http://www.yoursite.com/example/cow.php

(it actually looks in the filestructure rather than http:, but the example holds)

StarLion,
What is the difference in function between
include_once(‘foo.php’)
and
<?php get_header() ?>

Thanks,

Chris

There are multiple ways to template your PHP website. The include(“somefile.php”) is one way of doing this. You create multiple files, each consisting of a “block” of html that fits into your content page.

<!-- header.php -->
<h1>Header</h1>
<!-- sidebar.php -->
<div>Sidebar</div>

and so on.

get_something() is another way of doing the same. I’ve seen similar pattern used in Joomla. You can create one file containing all the blocks that you need to use on your content pages. Then you wrap the blocks inside functions, something like:

<!-- template.php -->
<?php function get_header() { ?>
<h1>Header</h1>
<?php } ?>
<?php function get_sidebar() { ?>
<div>Sidebar</div>
<?php } ?>
<?php function get_footer() { ?>
<p>Footer</p>
<?php } ?>

The second approach is more easier to manage but that is just my opinion.

Just be careful - you’re getting very close to calling it [FPHP]get_headers[/FPHP], which is an entirely different thing.

WebdesignHouston, StarLion,
I think one file (not folder) containing all the blocks would would be fastest for a browser, instead of includes where I create several files.

So in my index.php file I have a bunch of calls that are used get the blocks to build the collage (the collage being the assembly of the different parts used to create the page.)

So we have
File: index.html
Inside index.php would be
<?php function get_leftcolumn() { ?>
<?php function get_rightcolumn() { ?>
.
.
.
}}
</body>
</html>

Is this right? What do I put for “function”? Are those close brackets at the bottom placed correctly? If these close brackets are actually placed outside this file, I probably won’t do it this way.

What is the structure of the one file containing blocks look like"

File: thegetfile.php
Inside thegetfile.php would be

<?php
code for left column
code for right column
.
.
.
?>

How do you separate these blocks?

Thanks,

Chris

Forget that. I better ask this question first.

I’m working on getting rid of the CMS running my site. Everything can be changed to html and a few php files, but how can I keep my page address ending with “/”, as they do now, instead of “.html”? I’d like to avoid a long list of 302 redirects in my htaccess file.

Thanks,

Chris

An easy way to do it is like this:

If you want to see /somename/ instead of somename.html, rename somename.html to index.html and put it in a folder called /somename/. Whenever you point a browser to a folder, it looks for a file called index.html or index.php etc. If it finds it, you just see the folder name. It’s just like when you point a browser to the index page in the root folder. You see mysite.com, rather than mysite.com/index.html.

There are other ways to do the same thing, such as via a .htaccess file, but I prefer the above.

Ralph,
I was hoping to dump my CMS yet keep my ending slash. It looks like if I have 50 web pages I’m going to need 50 folders. Oh I see, the folder gets you /…/ and the index.html will gets you not showing index.html. I’ll have to think about it. I wonder if there’s a php way. Maybe I’m making a wrong assumption - will search engines look at /abc/ as the same as abc.html? I’m assuming not.

Thanks,

Chris

Not sure, but I don’t think so. I guess if you have that many pages, you might be better off taking the option to rewrite your URLs via Apache (if that’s what your site is hosted on). In a .htaccess file, you can formulate a rule that removes the .html extension, leaving just the file name. I guess you could add a slash at the end too. You’d need to consult the Apache forum for more info on doing that.

Seems like everyone always recommend.htaccess rules but you can accomplish the same thing without easily.


<?php
$arrRequestArgs = isset($_SERVER['PATH_INFO'])?explode('/',trim($_SERVER['PATH_INFO'],'/')):array();
echo '<pre>',print_r($arrRequestArgs),'</pre>';
?>

Place that in a file at your site root. After navigate to a URL like:

http://local.site/pathinfo.php/1/2/3/4

The catch is make sure you don’t use / within an argument since its the delimiter for separate arguments.

Once you do that you can use a single htaccess rule to rewrite all requests to a single entry point to accomplish URLs like:

/name-of-article => /pathinfo.php/name-of-article

That is way simpler than using rules in my opinion.

Oddz,
What does “argument” mean in
don’t use / within an argument
?

Also if I use that pHp in the root, does the file have to have a certain file name?

Thanks,

Chris

Felgall,
You were saying
“HTML files dont get fed through the PHP parser.”

Would that include when I have php in an html file and in the htaccess file I have

Use PHP5 Single php.ini as default

AddHandler application/x-httpd-php5s .php

Thanks,

Chris