# php 101



## Andrew Green (Jul 24, 2005)

Intro to php, breaking pages into pieces to keep all the commone elements in one place, using sessions, etc.

http://www.buffaloit.com/forum/showthread.php?t=364


----------



## Flatlander (Mar 19, 2007)

Andrew Green said:


> Intro to php, breaking pages into pieces to keep all the commone elements in one place, using sessions, etc.
> 
> http://www.buffaloit.com/forum/showthread.php?t=364


 
This link doesn't work...


----------



## Bigshadow (Mar 19, 2007)

PHP never ceases to amaze me....



> *Not Found*
> 
> The requested URL /forum/showthread.php was not found on this server.
> Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.


----------



## Andrew Green (Mar 19, 2007)

Flatlander said:


> This link doesn't work...



hehe, blame Bob, buffaloit.com no longer exists 

Anyways...

Not good code, but simple, and hopefully easy to understand:

===========

A little intro to some basic php ideas and uses.

Seperates common elements of each page into one place, making it easier to redesign the page, add new pages, etc.

Also starts a session and uses it to track # of page views, as well as how to terminate a session.

There is 6 files to it, hopefully all get posted intact


index.php

```
<?php
  // This space would be a good place to add tracking information, such as logging # of
  // page views, which pages got viewed, ip addresses, browser info, where they came from, etc.
  // I may post some code to do this later, but for now: A page view counter!
  // The Logout button is really only there as a reset on the session, just to show how it works.

  session_start(); // Star the session, this gets done everytime you run the script.
    // The session ends when the browser is closed.
  if (isset($_SESSION['views'])){
    $_SESSION['views']++;
  }
  else {
    $_SESSION['views']=1; // Creates a session variable, it is retained as long as the session is active.

  }

  if ($_GET['action']=="logout") { // Check for the logout being pushed
    $_SESSION = array(); // Destroy any session Variables
    session_destroy(); // Destroy the session
    header("Location: index.php"); // Redirect back to self
      // This CANNOT be done after anything else has been sent to the browser.
    exit(); // exit the script
  }
  require_once('head.inc'); // Links in the header info for the page
  // in the head viewer is a added section to show how many pages have been viewed this session.
  
  // This changes the content of the body based on the "action" variable.
  // index.php?action=page1 sets the variable to "page1", this passes the variable through "GET"
  // These files are written in plain html.
  
  if ($_GET['action']=="page1") {
    require_once('page1.inc');
  }
  else if ($_GET['action']=="page2") {
    require_once('page2.inc');
  }
  else { // if nothing is passed, or something that isn't listed above display the main page.
    require_once('page0.inc');
  }

  // Display the footer info and close everything off.
  require_once('foot.inc');
?>
```
head.inc

```
<!DOCTYPE XHTML PUBLIC '-//W3C//DTD XHTML 1.0//EN' '[URL="http://www.w3.org/TR/xhtml1/xhtml1-strict.dtd%27%3E"]http://www.w3.org/TR/xhtml1/xhtml1-strict.dtd'>[/URL]
<html>
  <head>
    <title>Simple PHP Enhancements</title>
  </head>
  <body>
    You have viewed <?php echo $_SESSION['views'] ?> pages!
    <table border ='1' style='width:100%'>
      <tr>
       <th style='font-size:200%;'>Simple PHP Enhancements Demo</th>
      </tr>
      <tr>
        <td style='text-align:center'>
         <a href="index.php">Main</a>
         <a href="index.php?action=page1">Page 1</a>
         <a href="index.php?action=page2">Page 2</a>
         <a href="index.php?action=logout">Log Out</a>
        </td>
      </tr>
      <tr>
        <td>
```
foot.inc

```
</td>
      </tr>      
      <tr>
       <td style='text-align:center'>Footer info</td>
      </tr>
    </table>
  </body>
</html>
```

page0.inc

```
<h3>Main Page</h3>

<p>
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
</p>
```
page1.inc

```
<h3>Page 1</h3>

<p>
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
</p>
```

page2.inc

```
<h3>Page 2</h3>

<p>
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
  Text goes here. Text goes here. Text goes here. Text goes here. Text goes here.
</p>
```


----------



## Bob Hubbard (Mar 19, 2007)

buffaloit.com merged into the rustaz.com forums.

Goto Rustaz.com/forum and look for Technics Guild.


----------

