PHP - Hello World 2001

(Windows 98; Linux; Apache; PHP; MySQL)


 

  • Environment:

This guide assumes the following:

  • LINUX = RedHat 7.0 (Kernel 2.2.16-22)
  • APACHE_DOC_PATH = /var/www/html
NOTE: The mysql (-3.23.22-6.i386.rpm) application that comes with RedHat 7.0 has a bug that prevents PHP from using it. It is recommended that the sample codes should be tested with the Apache, PHP and MySQL packages that come with RedHat 7.1.

Download the Demo sample, extract it and test it.

 

  • Apache:

To run the Apache Web Server:

/etc/rc.d/init.d/httpd restart

 

  • PHP:

This is the engine of sample. It has 3 main modules:


-----------------------        -----------------------        -----------------------
|                     |        |                     |        |                     |
|      SERVLET        |        |       ACTION        |        |      DATABASE       |
|                     |--------|                     |--------|                     |
-----------------------        -----------------------        -----------------------
|                     |        |                     |        |                     |
| - HTTP interface    |        | - Interface-        |        | - Common database   |
|                     |        |   independent       |        |   querying APIs     |
| - Sets sessions     |        |   process-like      |        |   e.g. connect,     |
|                     |        |   objects.          |        |   query, etc.       |
| - Serves pages      |        |                     |        |                     |
|   e.g. HTML.        |        | - Reads XML for     |        | - Puts results in a |
|                     |        |   data.             |        |   2 dimensional     |
|                     |        |                     |        |   array.            |
|                     |        | - Logs transactions |        |                     |
|                     |        |   to the database.  |        |                     |
|                     |        |                     |        |                     |
-----------------------        -----------------------        -----------------------

  • Create "/var/www/html/index.php":
    <?php
    #$servlet
    
    include_once('Servlet.inc');
    
    $servlet = new Servlet();
    $servlet->start();
    
    ?>
    
  • Create "/var/www/html/Servlet.inc":
    <?php
    
    session_start();
    
    include_once('Engine.inc');
    
    class Servlet extends Engine
    {
      var $methodVars;
      var $xml;
      var $action;
      var $page;
    
      function Servlet
      (
      )
      {
        $this->Engine();
    
        $this->methodVars = array();
        $this->xml        = '';
    
        return;
      }
    
      function get
      (
      )
      {
        $this->setMethodVars();
        $this->setXML();
        $this->setAction();
    
        return;
      }
    
      function set
      (
      )
      {
        $this->setSession();
        $this->setPage();
    
        return;
      }
    
      function setMethodVars
      (
      )
      {
        global $HTTP_GET_VARS;
        global $HTTP_POST_VARS;
    
        $this->methodVars = $HTTP_GET_VARS;
    
        if (getenv('REQUEST_METHOD') == 'POST')
        {
          $this->methodVars = $HTTP_POST_VARS;
        }
    
        return;
      }
    
      function setXML
      (
      )
      {
    #$key
    #$value
        $this->xml =
          sprintf("<%s>", $this->common->form);
    
        reset($this->methodVars);
        while (list($key, $value) = each($this->methodVars))
        {
          $this->xml .=
            sprintf
            (
              "<%s>%s</%s>",
              $key,
              $value,
              $key
            );
        }
    
        $this->xml .=
          sprintf("</%s>", $this->common->form);
    
        return;
      }
    
      function setAction
      (
      )
      {
        include_once('Login.inc');
    
        $this->action = new Login($this->xml);
    
        if ($this->methodVars[$this->common->action] ==
            $this->common->menu)
        {
          if ($this->methodVars[$this->common->menu] ==
              $this->common->listing)
          {
            include_once('Listing.inc');
    
            $this->action = new Listing($this->xml);
          }
    
          if ($this->methodVars[$this->common->menu] ==
              $this->common->logout)
          {
            include_once('Logout.inc');
    
            $this->action = new Logout($this->xml);
          }
        }
    
        $this->action->start();
    
        return;
      }
    
      function setPage
      (
      )
      {
        include_once('LoginPage.inc');
    
        $this->page = new LoginPage($this->action->xmlHandler->vars);
    
        if ($this->action->xmlHandler->vars[$this->common->action] ==
            $this->common->login)
        {
          include_once('MenuPage.inc');
    
          $this->page = new MenuPage($this->action->xmlHandler->vars);
        }
    
        if ($this->action->xmlHandler->vars[$this->common->action] ==
            $this->common->menu)
        {
          include_once('MenuPage.inc');
    
          $this->page = new MenuPage($this->action->xmlHandler->vars);
    
          if ($this->action->xmlHandler->vars[$this->common->menu] ==
              $this->common->listing)
          {
            include_once('ListingPage.inc');
    
            $this->page = new ListingPage($this->action->xmlHandler->vars);
          }
    
          if ($this->action->xmlHandler->vars[$this->common->menu] ==
              $this->common->logout)
          {
            include_once('LoginPage.inc');
    
            $this->page = new LoginPage($this->action->xmlHandler->vars);
          }
        }
    
        $this->page->set();
    
        return;
      }
    
      function setSession
      (
      )
      {
        global $HTTP_SESSION_VARS;
    
        if ($this->action->xmlHandler->vars[$this->common->action] ==
            $this->common->login)
        {
          session_register($this->common->sessionID);
          session_register($this->common->sessionIP);
          session_register($this->common->sessionDomain);
    
          $HTTP_SESSION_VARS[$this->common->sessionID]     = session_id();
          $HTTP_SESSION_VARS[$this->common->sessionIP]     = getenv('REMOTE_ADDR');
          $HTTP_SESSION_VARS[$this->common->sessionDomain] =
            'http:\/\/'.getenv('SERVER_NAME').'\/';
        }
    
        if ($this->action->xmlHandler->vars[$this->common->action] ==
            $this->common->menu)
        {
          if ($this->action->xmlHandler->vars[$this->common->menu] ==
              $this->common->logout)
          {
            if (sizeof($HTTP_SESSION_VARS) > 0)
            {
              reset($HTTP_SESSION_VARS);
              while(list($key, $value) = each($HTTP_SESSION_VARS))
              {
                session_unregister($key);
              }
            }
    
            session_unset();
            session_destroy();
    
            return;
          }
        }
    
        reset($this->action->xmlHandler->vars);
        while(list($key, $value) = each($this->action->xmlHandler->vars))
        {
          session_register($key);
    
          $HTTP_SESSION_VARS[$key] = $value;
        }
    
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/Common.inc":
    <?php
    
    class Common
    {
      var $form;
      var $action;
      var $login;
    
      var $username;
      var $password;
    
      var $menu;
      var $listing;
      var $logout;
    
      var $sessionID;
      var $sessionIP;
      var $sessionDomain;
    
      var $database;
      var $mySQL;
    
      function Common
      (
      )
      {
        $this->form   = 'form';
        $this->action = 'action';
        $this->login  = 'login';
    
        $this->username = 'username';
        $this->password = 'password';
    
        $this->menu    = 'menu';
        $this->listing = 'listing';
        $this->logout  = 'logout';
    
        $this->sessionID     = 'session_id';
        $this->sessionIP     = 'session_ip';
        $this->sessionDomain = 'session_domain';
    
        $this->mySQL    = 'mysql';
        $this->database = $this->mySQL;
    
        return;
      }
    
      function set1DArrayDebug
      (
        $arrayValue
      )
      {
    #$key
    #$value
        reset($arrayValue);
        while(list($key, $value) = each($arrayValue))
        {
          printf("\n<br>%s = %s", $key, $value);
        }
    
        return;
      }
    
      function set2DArrayDebug
      (
        $arrayValue
      )
      {
    #$key
    #$value
        reset($arrayValue);
        while(list($key, $value) = each($arrayValue))
        {
          printf("\n<br>");
          $this->set1DArrayDebug($value);
        }
    
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/Engine.inc":
    <?php
    
    class Engine
    {
      var $common;
    
      function Engine
      (
      )
      {
        include_once('Common.inc');
    
        $this->common     = new Common();
    
        return;
      }
    
      function start
      (
      )
      {
        $this->get();
    
        if ($this->check() != true)
        {
          return;
        }
    
        $this->set();
    
        return;
      }
    
      function get
      (
      )
      {
        return;
      }
    
      function check
      (
      )
      {
    #$flag
        $flag = true;
    
        return $flag;
      }
    
      function set
      (
      )
      {
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/Action.inc":
    <?php
    
    include_once('Engine.inc');
    
    class Action extends Engine
    {
      var $xml;
      var $xmlHandler;
      var $database;
    
      function Action
      (
        $xmlValue
      )
      {
        include_once('XMLHandler.inc');
    
        $this->Engine();
    
        $this->xmlHandler = new XMLHandler();
        $this->xml        = $xmlValue;
    
        return;
      }
    
      function get
      (
      )
      {
        $this->setXMLHandler();
    
        return;
      }
    
      function set
      (
      )
      {
        $this->setDatabase();
    
        return;
      }
    
      function setXMLHandler
      (
      )
      {
        include_once('XMLHandler.inc');
    
        $this->xmlHandler = new XMLHandler();
        $this->xmlHandler->parse($this->xml);
        $this->xmlHandler->finalise();
    
        return;
      }
    
      function setDatabase
      (
      )
      {
        include_once('MySQL.inc');
    
        $this->database = new MySQL();
    
        $this->database->connect('root', '', 'localhost');
        $this->database->query
        (
          "SELECT log.* FROM helloworld.log;",
          "helloworld"
        );
        $this->database->disconnect();
    
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/XMLHandler.inc":
    <?php
    
    class XMLHandler
    {
      var $parser;
      var $vars;
      var $name;
      var $value;
    
      function XMLHandler
      (
      )
      {
        $this->vars  = array();
        $this->name  = '';
        $this->value = '';
    
        return;
      }
    
      function startElement
      (
        $parserValue,
        $nameValue,
        $attributesValue
      )
      {
        $this->name  = $nameValue;
        $this->value = '';
    
        return;
      }
    
      function endElement
      (
        $parserValue,
        $nameValue
      )
      {
        $this->vars =
          array_merge($this->vars, array($this->name => $this->value));
    
        return;
      }
    
      function characterData
      (
        $parserValue,
        $dataValue
      )
      {
        $this->value = $dataValue;
    
        return;
      }
    
      function parse
      (
        $xmlValue
      )
      {
        $this->vars = array();
    
        $this->parser = xml_parser_create();
        xml_set_object($this->parser, & $this);
        xml_parser_set_option
        (
          $this->parser, XML_OPTION_CASE_FOLDING, false
        );
        xml_set_element_handler
        (
          $this->parser, "startElement", "endElement"
        );
        xml_set_character_data_handler
        (
          $this->parser, "characterData"
        );
        if (xml_parse($this->parser, $xmlValue) != true)
        {
          return;
        }
    
        return;
      }
    
      function finalise
      (
      )
      {
        xml_parser_free($this->parser);
    
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/Database.inc":
    <?php
    
    class Database
    {
      var $username;
      var $password;
      var $host;
    
      var $server;
      var $dbName;
      var $queryString;
      var $results;
    
      function Database
      (
      )
      {
        $this->username = '';
        $this->password = '';
        $this->host     = '';
    
        $this->dbName      = '';
        $this->queryString = '';
        $this->results     = array();
    
        return;
      }
    
      function connect
      (
        $usernameValue,
        $passwordValue,
        $hostValue
      )
      {
        $this->username = $usernameValue;
        $this->password = $passwordValue;
        $this->host     = $hostValue;
    
        return;
      }
    
      function query
      (
        $queryValue,
        $dbNameValue
      )
      {
        $this->queryString = $queryValue;
        $this->dbName      = $dbNameValue;
        $this->results     = array();
    
        return;
      }
    
      function disconnect
      (
      )
      {
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/MySQL.inc":
    <?php
    
    include_once('Database.inc');
    
    class MySQL extends Database
    {
      function MySQL
      (
      )
      {
        $this->Database();
    
        return;
      }
    
      function connect
      (
        $usernameValue,
        $passwordValue,
        $hostValue
      )
      {
        $this->username = $usernameValue;
        $this->password = $passwordValue;
        $this->host     = $hostValue;
    
        $this->server =
          @mysql_connect
          (
            $this->host,
            $this->username,
            $this->password
          )
          or
          die(mysql_error());
    
        return;
      }
    
      function query
      (
        $queryValue,
        $dbNameValue
      )
      {
    #$result
    #$row
        $this->queryString = $queryValue;
        $this->dbName      = $dbNameValue;
        $this->results     = array();
    
        if (@mysql_select_db($this->dbName, $this->server) == false)
        {
          die(mysql_error());
        }
    
        $result =
          mysql_query($this->queryString, $this->server);
    
        while ($row = @mysql_fetch_array($result, $this->server))
        {
          array_push($this->results, $row);
        }
    
        if (sizeof($result) > 0)
        {
          @mysql_free_result($result);
        }
    
        return;
      }
    
      function disconnect
      (
      )
      {
        @mysql_close
        (
          $this->server
        )
        or
        die(mysql_error());
    
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/Login.inc":
    <?php
    
    include_once('Action.inc');
    
    class Login extends Action
    {
      function Login
      (
        $xmlValue
      )
      {
        $this->Action($xmlValue);
    
        return;
      }
    
      function setDatabase
      (
      )
      {
        include_once('MySQL.inc');
    
        $this->database = new MySQL();
    
        $this->database->connect('root', '', 'localhost');
        $this->database->query
        (
          "INSERT INTO helloworld.log VALUES".
          sprintf
          (
            "(NULL, '%s', '%s', '%s', '%s', '%s');",
            date("Y-m-d H:i:s", time()),
            'localhost', 'helloworld', 'log', 'login'
          ),
          "helloworld"
        );
        $this->database->disconnect();
    
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/Listing.inc":
    <?php
    
    include_once('Action.inc');
    
    class Listing extends Action
    {
      function Listing
      (
        $xmlValue
      )
      {
        $this->Action($xmlValue);
    
        return;
      }
    
      function setDatabase
      (
      )
      {
        include_once('MySQL.inc');
    
        $this->database = new MySQL();
    
        $this->database->connect('root', '', 'localhost');
        $this->database->query
        (
          "INSERT INTO helloworld.log VALUES".
          sprintf
          (
            "(NULL, '%s', '%s', '%s', '%s', '%s');",
            date("Y-m-d H:i:s", time()),
            'localhost', 'helloworld', 'log', 'listing'
          ),
          "helloworld"
        );
        $this->database->disconnect();
    
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/Logout.inc":
    <?php
    
    include_once('Action.inc');
    
    class Logout extends Action
    {
      function Logout
      (
        $xmlValue
      )
      {
        $this->Action($xmlValue);
    
        return;
      }
    
      function setDatabase
      (
      )
      {
        include_once('MySQL.inc');
    
        $this->database = new MySQL();
    
        $this->database->connect('root', '', 'localhost');
        $this->database->query
        (
          "INSERT INTO helloworld.log VALUES".
          sprintf
          (
            "(NULL, '%s', '%s', '%s', '%s', '%s');",
            date("Y-m-d H:i:s", time()),
            'localhost', 'helloworld', 'log', 'logout'
          ),
          "helloworld"
        );
        $this->database->disconnect();
    
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/Page.inc":
    <?php
    
    class Page
    {
      var $vars;
      var $string;
      var $common;
    
      function Page
      (
        $varsValue
      )
      {
        include_once('Common.inc');
    
        $this->vars   = $varsValue;
        $this->string = '';
        $this->common = new Common();
    
        return;
      }
    
      function set
      (
      )
      {
        $this->string  = '';
        $this->string .= sprintf("\n<html>");
        $this->string .= sprintf("\n<head>");
        $this->string .= sprintf("\n</head>");
        $this->setBody();
        $this->string .= sprintf("\n</html>");
    
        printf("%s", $this->string);
    
        return;
      }
    
      function setBody
      (
      )
      {
        $this->string .= sprintf("\n<body>");
        $this->string .= sprintf("\n</body>");
    
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/LoginPage.inc":
    <?php
    
    include_once('Page.inc');
    
    class LoginPage extends Page
    {
      function LoginPage
      (
        $varsValue
      )
      {
        $this->Page($varsValue);
    
        return;
      }
    
      function setBody
      (
      )
      {
        $this->string .= sprintf("\n<body>");
        $this->string .= sprintf("\n<form method=\"post\" action=\"index.php\">");
        $this->string .= sprintf("\n<input type=\"hidden\" name=\"%s\" value=\"%s\">",
                                 $this->common->action,
                                 $this->common->login);
        $this->string .= sprintf("\n<input type=\"text\" name=\"%s\" value=\"hello\">",
                                 $this->common->username);
        $this->string .= sprintf("\n<input type=\"password\" name=\"%s\" value=\"world\">",
                                 $this->common->password);
        $this->string .= sprintf("\n<input type=\"submit\" value=\"Ok\">");
        $this->string .= sprintf("\n</form>");
        $this->string .= sprintf("\n</body>");
    
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/MenuPage.inc":
    <?php
    
    include_once('Page.inc');
    
    class MenuPage extends Page
    {
      function MenuPage
      (
        $varsValue
      )
      {
        $this->Page($varsValue);
    
        return;
      }
    
      function setBody
      (
      )
      {
        $this->string .= sprintf("\n<body>");
        $this->string .= sprintf("\n<form method=\"post\" action=\"index.php\">");
        $this->string .= sprintf("\n<input type=\"hidden\" name=\"%s\" value=\"%s\">",
                                 $this->common->action,
                                 $this->common->menu);
        $this->string .= sprintf("\n<select name=\"%s\">",
                                 $this->common->menu);
        $this->string .= sprintf("\n<option value=\"%s\" selected>%s</option>",
                                 $this->common->listing,
                                 $this->common->listing);
        $this->string .= sprintf("\n<option value=\"%s\">%s</option>",
                                 $this->common->logout,
                                 $this->common->logout);
        $this->string .= sprintf("\n</select>");
        $this->string .= sprintf("\n<input type=\"submit\" value=\"Ok\">");
        $this->string .= sprintf("\n</form>");
        $this->string .= sprintf("\n</body>");
    
        return;
      }
    }
    
    ?>
    
  • Create "/var/www/html/ListingPage.inc":
    <?php
    
    include_once('Page.inc');
    
    class ListingPage extends Page
    {
      var $database;
    
      function ListingPage
      (
        $varsValue
      )
      {
        $this->Page($varsValue);
    
        return;
      }
    
      function set
      (
      )
      {
        $this->setDatabase();
    
        $this->string  = '';
        $this->string .= sprintf("\n<html>");
        $this->string .= sprintf("\n<head>");
        $this->string .= sprintf("\n</head>");
        $this->setBody();
        $this->string .= sprintf("\n</html>");
    
        printf("%s", $this->string);
    
        return;
      }
    
      function setBody
      (
      )
      {
        $this->string .= sprintf("\n<body>");
        $this->common->set2DArrayDebug($this->database->results);
        $this->string .= sprintf("\n</body>");
    
        return;
      }
    
      function setDatabase
      (
      )
      {
        include_once('MySQL.inc');
    
        $this->database = new MySQL();
    
        $this->database->connect('root', '', 'localhost');
        $this->database->query
        (
          'SELECT log.* FROM helloworld.log;',
          'helloworld'
        );
        $this->database->disconnect();
    
        return;
      }
    }
    
    ?>
    

 

  • MySQL:

  • Create "/var/www/html/helloworld.sql":
    DROP DATABASE IF EXISTS helloworld;
    CREATE DATABASE helloworld;
    CONNECT helloworld;
    
    DROP TABLE IF EXISTS helloworld.log;
    CREATE TABLE helloworld.log
    (
      id          INT         NOT NULL AUTO_INCREMENT,
      timestamp   DATETIME    NOT NULL,
      host        VARCHAR(64) NOT NULL,
      application VARCHAR(64) NOT NULL,
      process     VARCHAR(64) NOT NULL,
      message     VARCHAR(64) NOT NULL,
      PRIMARY KEY(id)
    );
    
    DELETE FROM helloworld.log;
    
  • Start the database server:
    /sbin/chkconfig --level 2345 mysqld on
    /sbin/service mysqld restart
    
  • To run the mysql script:
    /usr/bin/mysql -h localhost -u root -p
    (Press Enter for password)
    source /var/www/html/helloworld.sql
    

 

  • Testing the Sample Code:

  • Open up the web browser and enter the URL:
    http://localhost/index.php
    


 

Primac Systems Limited

Copyright 2001

 

Hosted by www.Geocities.ws

1