Forum Moderators: open

Message Too Old, No Replies

Proper Way to Code

Using PHP Includes

         

andmunn

2:40 am on Feb 23, 2004 (gmt 0)

10+ Year Member



Here's My Scenario:

Each of my web pages on my site (about 20 ish), has a "header, footer, and side menu" as a php include. For example, my index.php has a header.html, footer.html, and menu.html include.

I'm wondering what would be the proper way to code in the following:

1. Where do I put my link to my style sheets? I.E// in the header file / in every page of my site (i.e// page1.html, page2.html, etc)? Or both?

2. For the includes (i.e.// for the header / footer /menu), should they include a title / head tags? (i.e.// <head><title>?)

3. Where do i put my title tags so that each page is named appropriately? I'm assuming this would be on each page.html file?

4. Is this a good way to go about doing this?

Any input appreciated.

Andrew.

Elijah

3:29 am on Feb 23, 2004 (gmt 0)

10+ Year Member



1. Where do I put my link to my style sheets? I.E// in the header file / in every page of my site (i.e// page1.html, page2.html, etc)? Or both?

Unless you are using a separate style sheet for each of your pages, I would put the link to it in the header file.

2. For the includes (i.e.// for the header / footer /menu), should they include a title / head tags? (i.e.// <head><title>?)
3. Where do i put my title tags so that each page is named appropriately? I'm assuming this would be on each page.html file?

What I do is this:

In page.html


<?php
$page_title="The title of the page";
include 'header.php';
?>
Page content

Then in header.php:

<html>
<head>
<title><?php echo $page_title;?></title>
<link rel="stylesheet" href="style.css" type="text/css" media="all" />
</head>
<body>
....

Hope this helps you a bit. ;)

Post back if you need more help,
Elijah

andmunn

4:35 am on Feb 23, 2004 (gmt 0)

10+ Year Member



Thanks for the help, i'll do exactly that :)

Should my footer.html and menu.html file contain and header/title tags at all?

Thanks again for your help.
Cheers,
Andrew.

sidyadav

5:08 am on Feb 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Should my footer.html and menu.html file contain and header/title tags at all?

No, You wouldn't need head/title tags in those pages. As Elijah stated, you can use the echo function and define the title as a variable to get it included in the page.

Whatever you include in header.php would be conveyed to all the includes, page content etc.

Sid

Hester

3:28 pm on Feb 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try to visualise the page as it appears complete in the browser. If necessary, view the final source (post-PHP). What you want is a page that will appear the same as if PHP wasn't used. So the header will have the LINK and META and TITLE tags as usual, but the footer is really just more content that fits inside the BODY.

PHP includes aren't like documents that go inside frames - they can be anything, from a single unit like a counter number, or a page full of HTML text. So there's no need to add anything extra to the files to 'validate' them.

Remember, they can be any type of document too - eg: "footer.html", "footer.txt" or "footer.php", depending on what's in the included file. Note the last one - you can include more PHP in the file and it will be acted on when the file is included. This can be useful sometimes.

g1smd

9:12 pm on Feb 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Make sure you check the output of your PHP script using [validator.w3.org...] too.

andmunn

5:52 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



Thanks everyone for the great help...the suggestion about "picturing it as one final page" really help out as well.

Always appreciate the quick response and effort.

Andrew.

RammsteinNicCage

4:22 am on Feb 25, 2004 (gmt 0)

10+ Year Member



Now that andmunn's question has been answered, time to hijack this thread. :)

I've seen a lot of threads on here about this, perhaps too many, because I'm now confused more than before. I want to use php includes, but I'd like to keep the .html extenstions. I think the proper terminology is parsing php as html, correct? And I have to add something to the .htaccess file, right? What exactly would that be?

Jennifer

Hester

10:43 am on Feb 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the includes don't have any PHP in them, you can keep the extension as ".html". But if they have PHP in them, you'll need to use an htaccess file to tell the server they should be parsed as PHP. BUT........ that would affect all your HTML files! Do you really want to do that? It could make a huge impact on server speed as files that don't contain PHP are parsed. (Unless there's a way to define the files to a certain folder only?)

sidyadav

11:07 am on Feb 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



yep, I use .html files with PHP includes. You could do .htaccess, but instead of getitng those 500's in miss-coding, I just change the MIME-TYPE of .HTML(.htm etc) extensions to application/php-httpd (I think) in cPanel. Its so easy.

Works great.

Sticky me if you need any help - I can provide you the exact code.

Sid

jetboy_70

11:35 am on Feb 25, 2004 (gmt 0)

10+ Year Member



AddType application/x-httpd-php .htm .html .php

added to either an .htaccess file or Apache's httpd.conf file (assuming you're using Apache).

This will enable PHP parsing on .htm, .html and .php extensions.

RammsteinNicCage

1:21 pm on Feb 25, 2004 (gmt 0)

10+ Year Member



Each page will include at least a little php right now (see Elijah's post), but I would like to add more once I learn it. ;)

Are there any other advantages/disadvantages over using the mime-type way instead of the htaccess?

Jennifer

twist

8:21 pm on Feb 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My 1 cent,

I'll call this file my_widget_page.php

<?
include( 'includes/page_setup.php' );
include( 'includes/content/widget/setup.php' );
include( 'includes/page_header.php' );
include( 'includes/content/widget/page.php' );
include( 'includes/page_footer.php' );
?>

Now I create a folder called includes in the same directory. I apply security to the folder by denying access to outside visitors. This stops anyone from viewing the files that will be inside the includes folder.

I then create a file inside of the includes folder called page_setup.php

  • This page sets up variables and info that is shared throughout all my pages. i.e. database setup, functions, and what not.

    Next I create a folder called widget and inside that folder a file called setup.php.

  • This file will contain info that is used specifically on my widget page. i.e. page title, variables, and so on.

    And finally I create the last three files, page_header.php, widget/page.php, and page_footer.php.

  • These three files are just plain html that is echoed by php. I just plug in the variables I created in the setup pages.

    The final step is making my files easy to remember on the web by using .htaccess.

    RewriteEngine on
    RewriteRule ^widget$ /my_widget_page.php [L]

    Now my page address looks like this http://example.com/widget

    The great thing about using this method is that I could switch to asp, lets say, and my visitors would never know the difference. I would just change my .htaccess file to look like this,

    RewriteEngine on
    RewriteRule ^widget$ /my_widget_page.asp [L]

    The url is still http://example.com/widget

    Good luck :)

  • andmunn

    11:57 pm on Feb 25, 2004 (gmt 0)

    10+ Year Member



    You Hijacked my thread! Hehe. Jk.

    I have more question, is it possible to do something simliar with Meta Tags? I.E//, put the meta tags in the "page1 / page2.html file", and then have it so that the "header" calls the meta tag from the file? (does that make sense)

    i.e// in page.html, you have

    <?php
    $page_title="The title of the page";
    include 'header.php'; $page_meta="whatever keywords";
    ?>

    And then in the header have:

    <html>
    <head>
    <title><?php echo $page_title;?></title>
    <link rel="stylesheet" href="style.css" type="text/css" media="all" />
    <meta $page_meta>
    </head>

    Anyways, is this possible? does that make sense?

    twist

    9:33 am on Feb 26, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    <?php
    $page_title="The title of the page";
    include 'header.php'; $page_meta="whatever keywords";
    ?>

    And then in the header have:

    <html>
    <head>
    <title><?php echo $page_title;?></title>
    <link rel="stylesheet" href="style.css" type="text/css" media="all" />
    <meta $page_meta>
    </head>

    The parser reads the script the same way you read a book. Left to right and top to bottom.

    Anything you define before the include statement will be availabe in the included file.

    Since your $page_meta variable is defined after your include it wont be available in your included file.

    This will work,


    $page_title="The title of the page";
    $page_meta="whatever keywords";
    include 'header.php';

    <?
    echo'
    <title>'. $page_title .'</title>
    <link rel="stylesheet" href="style.css" type="text/css" media="all" />
    <meta name="Keywords" content="'. $page_meta .'" />
    </head>
    ';
    ?>

    Hester

    9:55 am on Feb 26, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    And finally I create the last three files, page_header.php, widget/page.php, and page_footer.php.

    # These three files are just plain html that is echoed by php. I just plug in the variables I created in the setup pages.

    Shouldn't these files have ".html" extensions? Otherwise each is calling the PHP parser for no reason.

    twist

    12:10 pm on Feb 26, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    These three files are just plain html that is echoed by php

    I meant that these pages are the structure of my page. I don't define variables are add coding to setup the page on this page. This is the page where I echo my html tags to the browser.

    I just plug in the variables I created

    Something like this,

    <?
    // Page header.php
    echo'
    <html>
    <title>'. $page_title .'</title>
    <meta description="'. $page_description .'" />
    <body><h1>'. $page_heading .'</h1>
    ';
    ?>

    Turning the whole page into php allows me to add comments to the code that won't be sent to the users browser and I personally think it's easier to edit the page without having <? echo $var1;?>some html code<? echo $var2;?> all throughout the page.

    I am not a expert or anything but I don't understand why people that use php don't just make all their pages pure php. It looks cleaner and is much easier to edit in my opinion. (but this is the opinion of someone who is up at four in the morning)

    Hester

    12:21 pm on Feb 26, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    After learning PHP I find more and more of my pages benefit from it. Now nearly all my new pages are PHP. The main reason is that it makes it easy to add one line that spits out the last-modified date of the page. Before I was forever editing the footers on pages, remembering to get the date right. Now PHP handles this for me. (It can even be used to give the modified date of any included files, incase you change those too.)

    RammsteinNicCage

    8:37 pm on Feb 26, 2004 (gmt 0)

    10+ Year Member



    twist, what happens if you have it set how you do, but the person types in something like /widget.html when it's really a php page? Will that still automatically go to /widget?

    Jennifer

    twist

    11:39 pm on Feb 26, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    If your concerned about users trying to add extensions then you can add more rewrite rules like so,

    RewriteEngine on
    RewriteRule ^widget$ /my_widget_page.php [L]
    RewriteRule ^widget.html$ /my_widget_page.php [L]
    RewriteRule ^widget.htm$ /my_widget_page.php [L]
    RewriteRule ^widget.php$ /my_widget_page.php [L]
    RewriteRule ^widget.asp$ /my_widget_page.php [L]

    Now a person could type in and of the following and would still get my_widget_page.php

    http://example.com/widget
    http://example.com/widget.html
    http://example.com/widget.htm
    http://example.com/widget.php
    http://example.com/widget.asp

    and in case your wondering, if you want to make your homepage my_home_page.php open when a person types in http://example.com then,

    RewriteRule ^$ /my_home_page.php [L]

    There are other ways to accomplish this, try the Apache Web Server [webmasterworld.com] forum here at webmasterworld.

    andmunn

    3:39 am on Feb 27, 2004 (gmt 0)

    10+ Year Member



    Maybe someone can help me...i've worked it out somewhat, but now i get a little problem.

    My header header.html file looks like this:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE><?php echo $page_title;?></TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
    <META NAME="Author" CONTENT="WEBSITE">
    <META NAME="Keywords" CONTENT="PENDING KEYWORDS">
    <LINK REL=STYLESHEET TYPE="text/css" HREF="style/style.css">
    </HEAD>

    And my page.html file looks like this:

    <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=754>
    <TR VALIGN=TOP ALIGN=LEFT>
    <TD HEIGHT=218 WIDTH=754><?php $page_title="The title of the page"; include("header.html");?>
    </TD>
    </TR>
    </TABLE>

    BUT..when i do a view source of the page in a webbrowser (with the include and all) it messed up the code and looks likes this:

    <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=754>
    <TR VALIGN=TOP ALIGN=LEFT>
    <TD HEIGHT=218 WIDTH=754><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE>The title of the page</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
    <META NAME="Author" CONTENT="WEBSITE">
    <META NAME="Keywords" CONTENT="PENDING KEYWORDS">
    <LINK REL=STYLESHEET TYPE="text/css" HREF="style/style.css">
    </HEAD>

    ANy ideas why?

    RammsteinNicCage

    4:51 am on Feb 27, 2004 (gmt 0)

    10+ Year Member



    You're putting the include in the table cell. Try putting it above the table tag, by itself.

    <?php $page_title="The title of the page"; include("header.html");?>
    <body>
    <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=754>
    <TR VALIGN=TOP ALIGN=LEFT>
    <TD HEIGHT=218 WIDTH=754>site content</TD>
    </TR>
    </TABLE>
    </body>
    </html>

    And of course, don't forget the body tag. :)

    Jennifer