Creating the Home Page


The home page for the site, called index.php, will be a model for the other pages on the public side. It will require the configuration file (for error management) and the header and footer files to create the HTML design. This page will also welcome the user by name, assuming the user is logged in.

To write index.php

1.

Create a new document in your text editor (Script 13.5).

 <?php # Script 13.5 - index.php 

Script 13.5. The script for the site's home page, which will greet a logged in user by name.


2.

Include the configuration file, set the page title, and include the HTML header.

 require_once ('./includes/config.  inc.php); $page_title = 'PHP and MySQL for  Dynamic Web Sites: Visual  QuickStart Guide (2nd Edition)'; include ('./includes/header.html'); 

The script includes the configuration file first so that everything that happens afterward will be handled using the error-management processes established therein. Then the header.html file is included, which will start output buffering, begin the session, and create the initial part of the HTML layout.

3.

Greet the user and complete the PHP code.

 echo '<h1>Welcome'; if (isset($_SESSION['first_name'])) {   echo ", {$_SESSION['first_name']     }!"; } echo '</h1>'; ?> 

The Welcome message will be printed to all users. If a $_SESSION['first_name'] variable is set, the user's first name will also be printed. So the end result will be either just Welcome (Figure 13.6) or Welcome, Name! (Figure 13.7).

Figure 13.6. If the user is not logged in, this is the home page they will see.


Figure 13.7. If the user is logged in, the index page will greet the user by name.


4.

Create the content for the page.

 <p>Spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam.</p> <p>Spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam spam.</p> 

5.

Include the HTML footer.

 <?php include ('./includes/footer.html'); ?> 

The footer file will complete the HTML layout (primarily the menu bar on the right side of the page) and conclude the output buffering.

6.

Save the file as index.php, upload to the Web server, and test in a Web browser.

Tip

  • If you compare the list of links in Figures 13.6 and 13.7, you'll see how the conditional in footer.html changes the list of options.




    PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide
    PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    ISBN: 0321336577
    EAN: 2147483647
    Year: 2005
    Pages: 166
    Authors: Larry Ullman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net