Submit Your Article Forum Rules

Results 1 to 7 of 7

Thread: OOP in PHP and MySQL tips: Start here.

  1. #1
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    OOP in PHP and MySQL tips: Start here.

    Why reinvent the wheel?

    1. Resources for the thread.
      [list:74611b87b2]
    2. Harry Fuecks (may 2005 edition): "The PHP Anthology: Object Oriented PHP Solutions Volume I and II".
    3. The code library that follows with the book, SPLIB.
    4. The low hanging fruit of OO PHP
    [*] First configuration file.

    config.php

    <?php
    ini_set('include_path',ini_get('include_path') . '../SPLIB:' . '../pear:');
    ?>

    location: ../config
    [*] Connection to the MySQL database.

    connection.php

    <?php
    require_once('../config/config.php'); // Configuration file
    // Include MySQL class
    require_once('Database/MySQL.php'); // In SPLIB

    $host='db.name.com'; // Hostname of MySQL server
    $dbUser='Your user name'; // Username for MySQL
    $dbPass='your password'; // Password for user
    $dbName='Name of SQL DB'; // Database name

    // Instantiate MySQL connection
    $db=& new MySQL($host,$dbUser,$dbPass,$dbName);
    ?>

    location: ../connection Only for testing. Hide it in your applications.
    [*] SQL text files imported to MySQL via phpMyAdmin.

    menu.sql

    # phpMyAdmin MySQL-Dump
    # version 2.3.0-rc3
    # http://phpwizard.net/phpMyAdmin/
    # http://www.phpmyadmin.net/ (download page)
    #
    # Host: localhost
    # Generation Time: Sep 21, 2003 at 11:43 AM
    # Server version: 4.00.00
    # PHP Version: 4.3.2
    # Database : `sitepoint`
    # --------------------------------------------------------

    #
    # Table structure for table `menu`
    #

    CREATE TABLE menu (
    menu_id int(11) NOT NULL auto_increment,
    parent_id int(11) NOT NULL default '0',
    name varchar(255) NOT NULL default '',
    description text NOT NULL,
    location varchar(255) NOT NULL default '',
    PRIMARY KEY (menu_id),
    UNIQUE KEY location (location)
    ) TYPE=MyISAM;

    #
    # Dumping data for table `menu`
    #

    INSERT INTO menu VALUES (1, 0, 'Home', 'Home', '/');
    INSERT INTO menu VALUES (2, 1, 'News', 'Site news', '/news/');
    INSERT INTO menu VALUES (3, 1, 'About', 'About us', '/about/');
    INSERT INTO menu VALUES (4, 1, 'Contact', 'Contact Us', '/contact/');
    INSERT INTO menu VALUES (5, 0, 'Products', 'Product Catalog', '/products/');
    INSERT INTO menu VALUES (6, 5, 'Pets', 'The Petstore', '/products/pets/');
    INSERT INTO menu VALUES (7, 6, 'Birds', 'The Aviary', '/products/pets/birds/');
    INSERT INTO menu VALUES (8, 6, 'Cats', 'The Lions Den', '/products/pets/cats/');
    INSERT INTO menu VALUES (9, 5, 'Books', 'The Bookstore', '/products/books/');
    INSERT INTO menu VALUES (10, 9, 'Fiction', 'Fiction', '/products/books/fiction/');
    INSERT INTO menu VALUES (11, 9, 'Biography', 'Biographies', '/products/books/biography/');
    INSERT INTO menu VALUES (12, 3, 'Folio', 'Sites', '/about/folio/');
    [*] Building a simple menu loaded from the Database.

    File 12.php chapter 9 Vol I Fuecks modified.

    <?php
    require_once('../config/config.php'); // Modified
    require_once('../connection/connection.php');


    // Include MySQL class
    require_once('Database/MySQL.php'); // SPLIB

    // Include Menu class
    require_once('UI/Menu.php'); // SPLIB

    // Include BreadCrumb class
    require_once('UI/BreadCrumb.php'); // SPLIB


    // Set the base location for this page relative to web root - MODIFY THIS!!!
    // Something curious here.
    // http://www.kjellbleivik.com/WebPageElements/12.php/ OK
    // http://www.kjellbleivik.com/WebPageElements/12.php Not OK

    $baseUrl='/WebPageElements/12.php'; // Important modification from Fuecks code.

    // Fetch the location frameelement to match against menu table
    $location = str_replace ($baseUrl,'',$_SERVER['PHP_SELF']);

    // Instantiate new BreadCrumb menu passing the MySQL connection and location
    $crumbs=& new BreadCrumb($db,$location);
    ?>
    <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title> BreadCrumb Menu Example </title>
    <meta http-equiv="Content-type" content="text/html"
    charset="iso-8859-1" />
    <style type="text/css">
    body, a, li
    {
    font-family: verdana;
    font-size: 11px;
    }
    h1
    {
    font-family: verdana;
    font-size: 15px;
    color: navy
    }
    .breadCrumbs
    {
    margin-bottom: 10px;
    border-style: dashed;
    border-width: 2px;
    padding: 4px;
    width: 400px;
    }
    </style>
    </head>
    <body>
    <h1>Bread Crumbs Menu</h1>
    <div class="breadCrumbs">
    <?php
    // Display the breadcrumbs
    while ( $item = $crumbs->fetch() ) {
    if ( $item->isRoot() ) {
    echo ( "<a href=\"".$baseUrl.$item->location()."\">"
    .$item->name()."</a>" );
    } else {
    echo ( " > <a href=\"".$baseUrl.$item->location()."\">"
    .$item->name()."</a>" );
    }
    }
    ?>
    </div>
    Sample Urls:

    Contact

    Folio

    Products

    Fiction

    </body>
    </html>
    [*] Online Result
    [*] Have I forgotten something e.g. a .sql file? I have not checked that well enough.
    [/list:o:74611b87b2]

  2. #2
    Senior Member
    Join Date
    Sep 2005
    Posts
    254
    Why reinvent the wheel?
    While I agree there are some great php libraries already available I think it's imporant to build your own classes (framework) for some of the core day to day tasks. I think an application developer (even online applications) should know what every line of their core application does, with external libraries restricted to bolt-on functionality.

    Sorry to distract from the initial purpose of this thread, some great resources posted so far kgun

  3. #3
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999
    No problem. Discussion is very important. My remarks. Imagine 5 levels from basic to advanced.

    1. I agree with you when you start on OOP. You must know what is going on.
    2. When finished with that level, you start using third party libraries, like PEAR and SPLIB.
    3. At the next level you are able to read a class API like you read a manual. You learn a form of language where the different classes become elements ("words") in your library. You look at the class / API and know how to use it and extend it. You extend it by going from general to more specific tasks. That is about (multiple) inheritance.
    4. At this level, you can use different OOP languages. See OopSchool in my signature for resources.
    5. At this level you can write the most advanced hosted applications in C++ and distributed objects in e.g. BETA.


    Most webmasters should be able to manage the first three levels. The learning curve for OOP may seem steep, but once you grasp it, you will love it.

    <off topic>
    Favourite link?
    Borland Developer Network.

    When I studied Borland C++ Builder and C++ in the mid 90's, every serious developer knew that the Borland compiler was lightyears ahead of Microsoft's C++ compiler. You could even use the MS C++ class library in the Borland Compiler. That library naturally had its strenghts on MS operating system(s) classes.

    I think that Borland's C++ builder is still one of the best developement tools, but Borland is not that clever on marketing as for example Microsoft and Google. You do not need that for developing Web Applications.

    OOP in PHP becomes easy when you know C++. So if you are a young web developer, my advice is, take a course in C++ and learn to handle Borland's C++ Builder. When you handle it and understand the code, you can read OOP PHP code like others read a book.

    What has caused me most frustration in compiling code and putting code on the web / test server, is configuration, includes, libs, resources etc. etc. So start by studying the / your environments and learn to handle them, before you start on using third party libraries.

    I have tried to make that clear in my example above with the config.php file.
    </off topic>

    Back to topic?

  4. #4
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    OOP solution for registration and log in form in PHP.

    Resources for this post.
    1. See first post.
    2. The SPLIB class library.
    3. PEAR and: Getting Started with PEAR - PHP's Low Hanging Fruit

    Note: Do not sign up unless you want to register in my client database.
    1. Signup

      6.php
      <?php
      require_once('../config/config.php');
      // Include the MySQL class

      require_once('Database/MySQL.php');

      // Include the Session class
      require_once('Session/Session.php');

      // Include the SignUp class
      require_once('AccessControl/SignUp.php');

      // Include the QuickForm class
      require_once ('HTML/QuickForm.php');

      // Include the phpmailer class
      require_once ('ThirdParty/phpmailer/class.phpmailer.php');

      // Settings for MySQL
      $host='db.Yourdatabase.com'; // Hostname of MySQL server
      $dbUser='YourUsername'; // Username for MySQL
      $dbPass='YourPassword'; // Password for user
      $dbName='YourDatabaseName'; // Database name

      // Settings for SignUp class
      $listener='http://www.kjellbleivik.com/AccessControl/6.php';
      $frmName='Kjell Bleivik';
      $frmAddress='noreply@kjellbleivik.com';
      $subj='Account Confirmation';
      $msg=<<<EOD
      <h2>Thank you for registering!</h2>
      <div>The final step is to confirm
      your account by clicking on:</div>
      <div><confirm_url/></div>
      <div>
      Your Site Team
      </div>
      EOD;

      // Instantiate the MySQL class
      $db=& new MySQL($host,$dbUser,$dbPass,$dbName);

      // Instantiate the Session class
      $session=new Session();

      // Instantiate the signup class
      $signUp=new SignUp($db,$listener,$frmName,$frmAddress,$subj,$m sg,TRUE);

      // Is this an account confirmation ?
      if ( isset ( $_GET['code'] ) ) {
      if ( $signUp->confirm($_GET['code']) ) {
      $display='Thank you. Your account has now been confirmed.
      '.
      'You can now login';
      } else {
      $display='There was a problem confirming your account.
      '.
      'Please try again or contact the site administrators';
      }

      // Otherwise display the form
      } else {

      // A function for comparing password
      function cmpPass($element, $confirmPass) {
      global $form;
      $password = $form->getElementValue('password');
      return ($password == $confirmPass);
      }

      // A function to encrypt the password
      function encryptValue($value) {
      return md5($value);
      }

      // Instantiate the QuickForm class
      $form =& new HTML_QuickForm('regForm', 'POST');
      $renderer =& $form->defaultRenderer();

      // Clear the default HTML templates
      $renderer->clearAllTemplates();

      // Define new templates
      $renderer->setFormTemplate('
      <table class="registration">
      <form{attributes}>{content}
      </form>
      </table>');

      $renderer->setHeaderTemplate('
      <tr>
      <td class="header" colspan="2">{header}</td>
      </tr>');

      $renderer->setElementTemplate('
      <tr valign="top">
      <td class="label">
      {label}
      </td>
      <td class="field">
      <span class="error">{error}</span>

      {element}
      <span class="required">*</span>
      </td>
      </tr>');

      $renderer->setRequiredNoteTemplate('
      <tr>
      <td></td>
      <td class="requiredNote">{requiredNote}</td>
      </tr>');

      // Add a header to the form
      $form->addElement('header', 'header', 'Registration Form');

      // Register the compare function
      $form->registerRule('compare', 'function', 'cmpPass');

      // The login field
      $form->addElement('text','login','Desired Username:','class="signupData"');
      $form->addRule('login','Please provide a username','required',false,'client');
      $form->addRule('login','Username must be at least 6 characters','minlength',6,'client');
      $form->addRule('login','Username cannot be more than 50 characters','maxlength',50,'client');
      $form->addRule('login','Username can only contain letters and numbers','alphanumeric',NULL,'client');

      // The password field
      $form->addElement('password','password','Password:','cla ss="signupData"');
      $form->addRule('password','Please provide a password','required',false,'client');
      $form->addRule('password','Password must be at least 6 characters','minlength',6,'client');
      $form->addRule('password','Password cannot be more than 12 characters','maxlength',50,'client');
      $form->addRule('password','Password can only contain letters and numbers','alphanumeric',NULL,'client');

      // The field for confirming the password
      $form->addElement('password','confirmPass','Confirm:','c lass="signupData"');
      $form->addRule('confirmPass','Please confirm password','required',false,'client');
      $form->addRule('confirmPass','Passwords must match','compare','function');

      // The email field
      $form->addElement('text','email','Email Address:','class="signupData"');
      $form->addRule('email','Please an email address','required',false,'client');
      $form->addRule('email','Please enter a valid email address','email',false,'client');
      $form->addRule('email','Email cannot be more than 50 characters','maxlength',50,'client');

      // The first name field
      $form->addElement('text','firstName','First Name:','class="signupData"');
      $form->addRule('firstName','Please enter your first name','required',false,'client');
      $form->addRule('firstName','First name cannot be more than 50 characters','maxlength',50,'client');

      // The last name field
      $form->addElement('text','lastName','Last Name:','class="signupData"');
      $form->addRule('lastName','Please enter your last name','required',false,'client');
      $form->addRule('lastName','Last name cannot be more than 50 characters','maxlength',50,'client');

      // The signature field
      $form->addElement('textarea','signature','Signature:','c lass="signature"');

      // Add a submit button called submit and "Send" as the text for the button
      $form->addElement('submit','submit','Register','class="c reateAccount"');

      // Specify the "required field" note for the bottom of the form
      $form->setRequiredNote('<span class="required">*</span> required');

      // If the form is submitted...
      if ( $form->validate() ) {
      // Apply the encryption filter to the password
      $form->applyFilter('password', 'encryptValue');

      // Build an array from the submitted form values
      $submitVars=array (
      'login'=>$form->getSubmitValue('login'),
      'password'=>$form->getSubmitValue('password'),
      'email'=>$form->getSubmitValue('email'),
      'firstName'=>$form->getSubmitValue('firstName'),
      'lastName'=>$form->getSubmitValue('lastName'),
      'signature'=>$form->getSubmitValue('signature') );

      // Create signup
      if ( $signUp->createSignup($submitVars) ) {
      // Send confirmation email
      if ( $signUp->sendConfirmation() ) {
      $display='Thank you. Please check your email to '.
      'confirm your account';
      } else {
      $display='Unable to send confirmation email.
      '.
      'Please contact the site administrators';
      }
      } else {
      $display='There was an error creating your account.
      '.
      'Please try again later or '.
      'contact the site administrators';
      }
      } else {
      // If not submitted, display the form
      $display=$form->toHtml();
      }
      }
      ?>
      <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <title> Sign Up </title>
      <meta http-equiv="Content-type" content="text/html"
      charset="iso-8859-1" />
      <style type="text/css">
      body, a, td, input, textarea
      {
      font-family: verdana;
      font-size: 11px;
      }
      .registration
      {
      width: 400px;
      }
      .header
      {
      color: navy;
      text-align: center;
      font-variant: small-caps;
      font-size: 15px;
      font-weight: bold;
      border-color: navy;
      border-style: dashed;
      border-width: 1px;
      background-color: #f6f7f8;
      }
      .label
      {
      color: navy;
      text-align: right;
      width: 40%;
      font-weight: bold;
      padding: 3px;
      }
      .required
      {
      color: red;
      }
      .field
      {
      width: 60%;
      padding: 3px;
      }
      .error
      {
      color: red;
      }
      .requiredNote
      {
      font-size: 9px;
      text-align: right;
      }
      .signupData
      {
      width: 200px;
      background-color: #f6f7f8;
      font-weight: bold;
      }
      .signature
      {
      background-color: #f6f7f8;
      font-weight: bold;
      width: 200px;
      height: 100px;
      }
      .createAccount
      {
      background-color: #f6f7f8;
      color: navy;
      font-weight: bold;
      }
      </style>
      </head>
      <body>
      <?php echo ( $display );?>
      </body>
      </html>

    2. Login

      4.php

      <?php
      // If $_GET['form'] comes from the Auth class
      if ( isset ( $_GET['from'] ) ) {
      $target=$_GET['from'];
      } else {
      // Default URL: usually index.php
      $target='5.php';
      }
      ?>
      <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <title> Login Form </title>
      <meta http-equiv="Content-type"
      content="text/html" charset="iso-8859-1" />
      <style type="text/css">
      body, a, td, input
      {
      font-family: verdana;
      font-size: 11px;
      }
      h1
      {
      font-family: verdana;
      font-size: 15px;
      color: navy
      }
      </style>
      </head>
      <body>
      <h1>Please log in</h1>
      <form action="<?php echo ( $target ); ?>" method="post">
      <table>
      <tr valign="top">
      <td>Login Name:</td>
      <td><input type="text" name="login" /></td>
      </tr>
      <tr valign="top">
      <td>Password:</td>
      <td><input type="password" name="password" /></td>
      </tr>
      <tr valign="top">
      <td></td>
      <td><input type="submit" value=" Login " /></td>
      </tr>
      </table>
      </form>
      </body>
      </html>
    3. Redirection to "secure page". (Not SSL).

      5.php

      <?php
      require_once('../config/config.php');
      // Include Magic Quotes stripping script
      require_once('MagicQuotes/strip_quotes.php');

      // Include MySQL class
      require_once ('Database/MySQL.php');

      // Include Session class
      require_once ('Session/Session.php');

      // Include Auth class
      require_once ('AccessControl/Auth.php');

      $host='db.Yourdatabase.com'; // Hostname of MySQL server
      $dbUser='YourUsername'; // Username for MySQL
      $dbPass='YourPassword'; // Password for user
      $dbName='YourDatabaseName'; // Database name

      // Instantiate MySQL connection
      $db=& new MySQL($host,$dbUser,$dbPass,$dbName);

      // Instantiate the Auth class
      $auth=& new Auth ($db,'4.php','secret');

      // For logging out
      if ( isset ( $_GET['action'] ) && $_GET['action'] == 'logout' ) {
      $auth->logout();
      }
      ?>
      <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <title> Welcome </title>
      <meta http-equiv="Content-type"
      content="text/html" charset="iso-8859-1" />
      <style type="text/css">
      body, a, td, input
      {
      font-family: verdana;
      font-size: 11px;
      }
      h1
      {
      font-family: verdana;
      font-size: 15px;
      color: navy
      }
      </style>
      </head>
      <body>
      <h1>Welcome</h1>


      You are now logged in</p>
      <?php
      if ( isset ($_GET['action']) && $_GET['action'] == 'test' ) {
      echo ( '

      This is a test page. You are still logged in' );
      }
      ?>


      Test page</p>


      Logout</p>
      </body>
      </html>
    4. Result.
      [list:933da64f26]
    5. Registration form. If you sign up you are registered in my database. Do not call it spam if you get an email.
    6. Login after registration.
    [*] How secure is the solution? [*] Can you use it? Yes, if you have (part of) the PEAR class library installed on your web server and if you have the SPLIB library that follows with the book. I think you get that for download as soon as you have bought the books. I am not an affiliate of SitePoint and get no commision for reccomending the books. [*] Note, the solution uses the Session class. That means that you are logged in as long as there are no timeout or you log out.[/list:o:933da64f26]

  5. #5
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    A fairly secure registration class.

    Don't submit the form with your credentials unless you want to register in my client database and get future OOP information. That is your responsibility.

    As long as you do not use SSL it is not easy to make the registration more secure than this form.
    Features:
    • Random image text verification.
    • Confirmation email.
    • Double checking Authentication.


    I had to rewrite a lot of code in 7.php. I could not find it at sitepoint.

    Here is the code:

    7.php Fairly much rewrittin from 7.php that follows with the book.

    <?php
    require_once('../config/config.php');
    // Include the MySQL class

    require_once('Database/MySQL.php');

    // Include the Session class
    require_once('Session/Session.php');

    // Include the SignUp class
    require_once('AccessControl/SignUp.php');

    // Include the QuickForm class
    require_once ('HTML/QuickForm.php');

    // Include the phpmailer class
    require_once ('ThirdParty/phpmailer/class.phpmailer.php');

    // Settings for MySQL
    $host='db.yoursite.com'; // Hostname of MySQL server
    $dbUser='YourUserName'; // Username for MySQL
    $dbPass='YourPassword'; // Password for user
    $dbName='YourDatabaseName'; // Database name

    // Settings for SignUp class
    $listener='http://www.kjellbleivik.com/AccessControl/7.php';
    $frmName='Kjell Bleivik';
    $frmAddress='noreply@kjellbleivik.com';
    $subj='Account Confirmation';
    $msg=<<<EOD
    <h2>Thank you for registering!</h2>
    <div>The final step is to confirm
    your account by clicking on:</div>
    <div><confirm_url/></div>
    <div>
    Your Site Team
    </div>
    EOD;

    // Instantiate the MySQL class
    $db=& new MySQL($host,$dbUser,$dbPass,$dbName);

    // Instantiate the Session class
    $session=new Session();

    // Instantiate the signup class
    $signUp=new SignUp($db,$listener,$frmName,$frmAddress,$subj,$m sg,TRUE);

    // Is this an account confirmation ?
    if ( isset ( $_GET['code'] ) ) {
    if ( $signUp->confirm($_GET['code']) ) {
    $display='Thank you. Your account has now been confirmed.
    '.
    'You can now login';
    } else {
    $display='There was a problem confirming your account.
    '.
    'Please try again or contact the site administrators';
    }

    // Otherwise display the form
    } else {

    // Register a session variable for use in the image
    if ( !$session->get('randomString') )
    $session->set('randomString',$signUp->createRandString());

    // A function for comparing password
    function cmpPass($element, $confirmPass) {
    global $form;
    $password = $form->getElementValue('password');
    return ($password == $confirmPass);
    }

    // A function to encrypt the password
    function encryptValue($value) {
    return md5($value);
    }

    // Instantiate the QuickForm class
    $form =& new HTML_QuickForm('regForm', 'POST');
    $renderer =& $form->defaultRenderer();

    // Clear the default HTML templates
    $renderer->clearAllTemplates();

    // Define new templates
    $renderer->setFormTemplate('
    <table class="registration">
    <form{attributes}>{content}
    </form>
    </table>');

    $renderer->setHeaderTemplate('
    <tr>
    <td class="header" colspan="2">{header}</td>
    </tr>');

    $renderer->setElementTemplate('
    <tr valign="top">
    <td class="label">
    {label}
    </td>
    <td class="field">
    <span class="error">{error}</span>

    {element}
    <span class="required">*</span>
    </td>
    </tr>');

    $renderer->setRequiredNoteTemplate('
    <tr>
    <td></td>
    <td class="requiredNote">{requiredNote}</td>
    </tr>');

    // Add a header to the form
    $form->addElement('header', 'header', 'Registration Form');

    // Register the compare function
    $form->registerRule('compare', 'function', 'cmpPass');

    // The login field
    $form->addElement('text','login','Desired Username:','class="signupData"');
    $form->addRule('login','Please provide a username','required',false,'client');
    $form->addRule('login','Username must be at least 6 characters','minlength',6,'client');
    $form->addRule('login','Username cannot be more than 50 characters','maxlength',50,'client');
    $form->addRule('login','Username can only contain letters and numbers','alphanumeric',NULL,'client');

    // The password field
    $form->addElement('password','password','Password:','cla ss="signupData"');
    $form->addRule('password','Please provide a password','required',false,'client');
    $form->addRule('password','Password must be at least 6 characters','minlength',6,'client');
    $form->addRule('password','Password cannot be more than 12 characters','maxlength',50,'client');
    $form->addRule('password','Password can only contain letters and numbers','alphanumeric',NULL,'client');

    // The field for confirming the password
    $form->addElement('password','confirmPass','Confirm:','c lass="signupData"');
    $form->addRule('confirmPass','Please confirm password','required',false,'client');
    $form->addRule('confirmPass','Passwords must match','compare','function');

    // The email field
    $form->addElement('text','email','Email Address:','class="signupData"');
    $form->addRule('email','Please an email address','required',false,'client');
    $form->addRule('email','Please enter a valid email address','email',false,'client');
    $form->addRule('email','Email cannot be more than 50 characters','maxlength',50,'client');

    // The first name field
    $form->addElement('text','firstName','First Name:','class="signupData"');
    $form->addRule('firstName','Please enter your first name','required',false,'client');
    $form->addRule('firstName','First name cannot be more than 50 characters','maxlength',50,'client');

    // The last name field
    $form->addElement('text','lastName','Last Name:','class="signupData"');
    $form->addRule('lastName','Please enter your last name','required',false,'client');
    $form->addRule('lastName','Last name cannot be more than 50 characters','maxlength',50,'client');

    // The signature field
    $form->addElement('textarea','signature','Signature:','c lass="signature"');

    // The image check field for "humanness"
    $form->addElement('text','imageCheck','Image Text:',
    'class="signupData"');
    $form->addRule('imageCheck','Please enter text from image',
    'required',false,'client');

    // Server side validation! Don't give away random string in JavaScript
    $form->addRule('imageCheck','Please confirm the text in the image',
    'regex','/^'.$session->get('randomString').'$/',
    'server');

    // The image check field
    // Changed
    $form->addElement('image',
    ' ',
    '8.php',
    'class="image"');

    // Add a submit button called submit and "Send" as the text for the button
    $form->addElement('submit','submit','Register','class="c reateAccount"');

    // Specify the "required field" note for the bottom of the form
    $form->setRequiredNote('<span class="required">*</span> required');

    // If the form is submitted...
    if ( $form->validate() ) {
    // Apply the encryption filter to the password
    $form->applyFilter('password', 'encryptValue');

    // Build an array from the submitted form values
    $submitVars=array (
    'login'=>$form->getSubmitValue('login'),
    'password'=>$form->getSubmitValue('password'),
    'email'=>$form->getSubmitValue('email'),
    'firstName'=>$form->getSubmitValue('firstName'),
    'lastName'=>$form->getSubmitValue('lastName'),
    'signature'=>$form->getSubmitValue('signature') );

    // Create signup
    if ( $signUp->createSignup($submitVars) ) {
    // Send confirmation email
    if ( $signUp->sendConfirmation() ) {
    $display='Thank you. Please check your email to '.
    'confirm your account';
    } else {
    $display='Unable to send confirmation email.
    '.
    'Please contact the site administrators';
    }
    } else {
    $display='There was an error creating your account.
    '.
    'Please try again later or '.
    'contact the site administrators';
    }
    } else {
    // If not submitted, display the form
    $display=$form->toHtml();
    }
    }
    ?>
    <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title> Sign Up </title>
    <style type="text/css">
    body, a, td, input, textarea
    {
    font-family: verdana;
    font-size: 11px;
    }
    .registration
    {
    width: 400px;
    }
    .header
    {
    color: navy;
    text-align: center;
    font-variant: small-caps;
    font-size: 15px;
    font-weight: bold;
    border-color: navy;
    border-style: dashed;
    border-width: 1px;
    background-color: #f6f7f8;
    }
    .label
    {
    color: navy;
    text-align: right;
    width: 40%;
    font-weight: bold;
    padding: 3px;
    }
    .info
    {
    color: navy;
    text-align: right;
    width: 40%;
    padding: 3px;
    }
    .required
    {
    color: red;
    }
    .field
    {
    width: 60%;
    padding: 3px;
    }
    .error
    {
    color: red;
    }
    .requiredNote
    {
    font-size: 9px;
    text-align: right;
    }
    .signupData
    {
    width: 200px;
    background-color: #f6f7f8;
    font-weight: bold;
    }
    .signature
    {
    background-color: #f6f7f8;
    font-weight: bold;
    width: 200px;
    height: 100px;
    }
    .createAccount
    {
    background-color: #f6f7f8;
    color: navy;
    font-weight: bold;
    }
    </style>
    </head>
    <body>
    <?php echo ( $display );?>
    </body>
    </html>

    8.php
    <?php
    require_once('../config/config.php');
    // Include Session class
    require_once ('Session/Session.php');

    // Include RandomImageText class
    require_once ('Images/RandomImageText.php');

    // Instantiate the Session class
    $session=new Session;

    // Instantiate RandomImageText giving the background image
    $imageText=new RandomImageText ('reg_image/reg_image.jpg');

    // Add the text from the session
    $imageText->addText($session->get('randomString'));

    // Send the right mime type
    header ( 'Content-type: image/jpeg' );

    // Display the image
    ImageJpeg($imageText->getImage());
    ?>

    You also need the image File: reg_image/reg_image.jpg

  6. #6
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    A Christmas gift

    We are a week from Christmas. If you are a PHP developer, I can reccomend the following book:

    Matt Zandstra (2004): "PHP 5 Objects, Patterns, and Practice." ISBN 1-59059-380-4.

    If you are starting on PHP, learn Design Patterns from the very start and make it object oriented. That may save you time and money. You will know why when you read that book. There is a major step in OO direction from PHP 4.x to PHP 5.x.

    Code that functions in PHP 4 may not longer function in PHP 5.x. Use the last version of PHP when you start your project. As of this writing that is PHP 5.2. If you are learning PHP, PHP 6.0 may be stable before you start on the real project.

    Advice: Do not buy an old book that is about PHP 4.x or lower unless you are rewriting earlier code and you need to understand the difference.

    I have not read the whole book, but I can tell you that the three frist chapters are worth the cost of the book.

  7. #7
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999
    I do not like to be responsible for broken links, but my example links in this post will be deleted because of changing the site.

    Main reason for links: Example.

Similar Threads

  1. Start Seeing Motorcycles => Start Obeying Traffic Laws
    By epep in forum The Castle Breakroom (General: Any Topic)
    Replies: 1
    Last Post: 10-08-2007, 10:30 AM
  2. Keywords: start with lots or start with a few?
    By gpconnelly in forum Google AdWords/Google AdSense
    Replies: 11
    Last Post: 11-09-2006, 11:05 AM
  3. unable to start MySQL
    By wood1e in forum Database Discussion Forum
    Replies: 1
    Last Post: 11-16-2004, 12:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •