tags:

views:

157

answers:

5

Hello.

I want to make my homepage, without frames, should i just split up my design on index.php so it is header.php/footer.php, and then just include them on every page?

A: 

You may want to set a session for that. A session variable exists as long as a visitor is on your website:

<?php
    session_start(); // Remember that session_start(); must be the first line of your PHP and HTML-code

    if($add_a_message){
        $_SESSION['message'] = 'Message';
    }

    if($destroy_message){
        $_SESSION['message'] = '';
    }

    // echo this message
    if(isset($_SESSION['message']) && strlen($_SESSION['message']) > 0){
        echo '<strong>' . $_SESSION['message'] . '</strong>';
    }
?>
Harmen
I think this does not address the question.
Felix Kling
That's what I thought, but the question has been edited in the meantime.
middus
`Remember that session_start(); must be the first line of your PHP and HTML-code`That's not true.
Crozin
+5  A: 

Yes, you can split your index.php into header.php/footer.php and then just include them on every page. Note that your pages can be not static HTML but php scripts, to show multiple pages with one script.
I'd suggest also to have not a commonplace structure like

include 'header.php';
//do some stuff
include 'footer.php';

but another structure, much more useful:

//do some stuff, retrieve all data.
include 'header.php';
include 'page.php'; //include page template
include 'footer.php';
Col. Shrapnel
Downside of this approach: "then just include them on every page". That brings along some problems and violates the good practice of DRY: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself
middus
yes you can call it with your front controller. But not the way you're doing it in your example. There must be 2 calls for the particular page - first to the script itself and then to it's template.
Col. Shrapnel
A: 

Another idea would be to have just one single point of entry which is called with a GET parameter, e.g ?site=about. Your index.php could look like this:

<?php
// whitelist of allowed includes
$allowedIncludes = array('home', 'about', 'error404'); // etc.
// what to include if ?site is not set at all / set to an illegal include
$defaultInclude = 'home';
$errorInclude = 'error404';

// if site is not set, include default
$site = (empty($_GET['site'])) ? $defaultInclude : $_GET['site'];
// if site is illegal, include error page
$include = (in_array($site, $allowedIncludes)) ? $site : $errorInclude;

// actual includes
include 'header.php';
include $include.'.php';
include 'footer.php';

Thus you only have to include header.php and footer.php once and have full control about what is allowed and what is not (the included files could be in a directory that only php has access to). While your index.php handles the request, home.php, about.php do not have to know about header.php and footer.php (you could easily replace them at a later point in time).

If you do not like http://www.example.com/?site=about, you can look into mod_rewrite and friends.

middus
That's wrong approach, As I satted before. Just try to implement a simple custom <title> for the each page. Your scheme would work only if you move template calls into included page. Also, having all page names in array is just silly.
Col. Shrapnel
No, it is not "silly", it is a rather secure but still simple way to ensure that only files you explicity allow to be included can actually be included. Regarding the title: it was just an example, you can easily alter my answer to a structure like the one you are proposing. Turns out: you only have to do it in one place `index.php` instead of in all the include files. (See also Niels' answer: http://stackoverflow.com/questions/2437982/making-a-site-without-frames/2438308#2438308)
middus
there are tons of no less secure but more automatic methods. writing it by hand is silly.And No, your example cannot be altered to implement dynamic title or HTTP header.
Col. Shrapnel
Like I said: it is an example. Instead of defining the whitelist array yourself you could hand this off to some clever function that does the busywork for you. Of course it can be altered: you could `include` before `include 'header.php'` and use some kind of template. Thus, you can send custom headers, set a title variable that is used in `header.php`, etc. Did you actually look into Niels' answer? It lists some of the benefits of this approach.
middus
I know benefits of every approach in the world. But the most commonplace one you demonstrated here - to use front condtroller as a template engine. Most not smart one. BUT, Actually, front controller has nothing to do with templates. It can be used and can be not used whilst template would be ok in either case.
Col. Shrapnel
Hm, I got the feeling that this is going nowhere. Let's leave it at that.
middus
I agree with middus. This approach is very DRY, which is good!And using a front controller as a template engine? In proper MVC, iirc, the front controller accepts commands and passes them off to other parts of the system. The templating part is work for the View part of MVC.Anyhoozles: reinventing the wheel is fun and all, but most people are better of just using an existing framework. But understanding the principles and the reasons for design decisions is good.
Niels Bom
+1  A: 

The problem with the suggested solution of including stuff in every page of your site is that you have to update all the pages of your site if you want to include another thing, say a sidebar.

A better idea is not to have a script--page connection at all. So you don't write a php file per page you want to show. Instead, use one front controller file, most use index.php in the root of the website. And then use Apache mod_rewrite or other server techniques to have flexibility in the URL's of your site. Then let index.php map different URL requests to serve different pages, you can then put all the pages of your site into a database or somewhere else.

This way there's only one point in your site that includes the templates for the header and footer, so that's easily changeable, and you can use the root of the site to serve AJAX requests, in which you won't want to output HTML but JSON for instance.

Afaik this is a good way of going about it.

Niels Bom
See my answer for an example of how to implement this: http://stackoverflow.com/questions/2437982/making-a-site-without-frames/2438228#2438228
middus
what's wrong with adding a sidebar to a header.php?
Col. Shrapnel
I did not say include another thing in the header.php. That's for 2 reasons: a sidebar should not be in header.php for semantic reasons, secondly: this way of adding stuff to every page is highly inflexible. Suppose you want to add sidebar X to certain pages and sidebar Y to others but not everyone of them. You'd have to put all that logic in header.php, which is completely the wrong place for it imho.
Niels Bom
+2  A: 

I suggest you use a framework. Most frameworks (if not all) have simple template systems, so you don't have to repeat code.

AntonioCS
Yup plus 1 for the framework. I would advise to quickly watch Codeigniters screencast how to create a blog in 20 minutes =>http://codeigniter.com/tutorials/
Alfred
I think he wants to learn some basic stuff before diving into a a framework.
middus
@middus using a framework doesn't prevent you from learning basic stuff it just gives you a sane structure for your web app/page :)
AntonioCS