Independent Digital

Creating and managing dynamic websites using Php, MySql, HTML and Wordpress with a bit of PEAR on the side

Entries Comments



Email This Post    Print This Post   

Category: PEAR

PEAR Quickform – the ultimate tutorial now ONLINE!

15 May, 2007 (14:45) | PEAR | By: clive

At last the ultimate Quickform tutorial, reference or guide (call it what you will) is now available online. Access it here.

This is a very useful tool for beginers and professionals alike. Whether you’re learning Quickform or are a seasoned user, this reference guide will prove very useful to you. Use it to learn how to create forms in minutes or just refer to it when you want to brush up on how to do something that you are not quite sure how you did it in the past.

Best of all, it’s free!

Go to tutorial!.

PEAR: a php programer’s delight.

7 May, 2007 (13:54) | PEAR | By: clive

PEAR stands for Php Extension and Application Repository. PEAR is a framework and distribution system for reusable php components. These components are distributed in packages, each developed by a separate development team, and are distributed in a zipped format and can be installed on your website or local machine. Packages range from MDB2, a database abstraction layer, to HTML_Template_Flexy, an HTML templating system. Have a look at the packages and you are sure to find something that will not only be useful but will also save you a lot of time.

The goals of PEAR are not only to serve as a repository for php extensions and library code but also to define standards for developers who write the portable and re-usable code.

The code can be re-used in many applications, the majority being in web applications – a great time saver for webmasters who don’t have to waste time re-inventing the wheel. Instead they can rely on tried and tested code developed by experienced php developers who contribute to PEAR.

Perhaps the only problem with PEAR is the lack of documentation. Each package is documented but may be incomplete and rather un-user-friendly.

PEAR is definately worth looking into. You will find tried and tested php code that you will use over and over again, saving you a great deal of frustration and time. You will find PEAR here.

MDB2 Users tutorial – using results

24 April, 2007 (10:36) | MySQL, PEAR | By: clive

If you’re new to MDB2 or maybe even if you’re a bit rusty as to which method to use to get the results that you want out of the database, then this tutorial is for you. It’s also a reference tool so you’ll end up using it whenever you work with MDB2.

You run the index file and a drop-down list of all the methods you can use to get information out of your query result appears. Select one and a brief explanation of that method pops up together with an example of how it’s used. The using_results.php file contains all the code with comments for you to browse through. All in all a very useful tutorial and reference tool. I will be adding to this tutorial regularly until eventually it will contain all the methods applicable to the mdb2 package (configuration, connecting, querying, using results, etc). So check back here regularly to keep abreast of developments.

Download the tutorial here

PEAR:MDB2 tutorial for getting started and using it

18 April, 2007 (13:05) | MySQL, PEAR | By: clive

I thought I’d put together this tutorial for those of you, like me, would like to use something to make life easier when using databases but are somewhat overwhelmed when first dabbling with database abstraction layers such as PEAR:mdb2. Fear not, mdb2 is pretty easy to use once you know how. And that is the point of this tutorial, to show you how to use mdb2 without too much hassle.

This is not a tutorial in the sense that I ramble on and give snippets of code which eventually leaves you totally confused. Rather I have created a package of files covering the main aspects of mdb2. Each file has all the code commented to make understanding the code very easy. So for example the create_db.php file has all the code necessay to create a database. You actually run the code and enter a database name and the database is created. Here’s a sample from the code:

//create the data source name
$dsn = ‘mysql://root:sasha@localhost’;//database name is omitted as it will be added from your input
//do the connection thing
$mdb2 =& MDB2::connect($dsn);
//if there is an error
if (PEAR::isError($mdb2))
{
die($mdb2->getMessage());
}
// loading the Manager module
$mdb2->loadModule(‘Manager’);

By working through the code and acompanying comments, you will quickly see how easy it is to use mdb2. Naturally you can tweek the code and use it as you please.

The files in the package cover the following:

  • Creating a database
  • Deleting a database
  • Creating a table
  • Deleting a table
  • Creating an index
  • Deleting an index
  • Creating sequences
  • Deleting sequences
  • Creating constraints
  • Deleting constraints
  • Inserting records
  • Searching records
  • You can get the file package here

    Using PEAR:AUTH and PEAR:MDB2 to secure your website – bare essentials

    10 April, 2007 (14:37) | PEAR | By: clive

    This is a basic authentication system using the PEARS Auth package. It is very similar to the previous tutorial, authentication tutorial, but is far simpler in that it only has the bare essentials for an authentication system. You can add your own HTML, templates, etc.

    The files

      login.php
      members.php
      register.php
      remove_user.php
      user_removed.php
      index.php
      get_user_details.php
      send_email.php
      menu.php

    You’ll need to create a table with 3 columns, username, password and email. Make sure that you change the configuration in the options array in each of the above files to allow connection to your database and table.

    A brief description of each file


    login.php This is the login page. It displays a login form. The user enters their user name and password, these are compared to those in the database and if they validate, the user is allowed access to the members page. If not, then the user is returned to the home page and an error message is displayed.

    members.php This is the members only page. If the user is logged in then they are allowed access to this page. The page also checks the $user_ok variable which is set on successful login. If it is true then the user is allowed access to the page. If not then the user is returned to the home page. This variable is used to prevent access to the page by merely entering the address in browser.

    register.php This page allows the new user to register by entering their username, password and email address. The new username is first checked with existing user names and only if it is unique, is the record stored in the database. If the new name is not unique, the user is returned to the register page and an error message is displayed. On successful registration, the user is redirected to the login page.

    remove_user.php This page allows the user to enter their username and password. Their email address is retrieved from the database and an email sent to them to confirm the removal.

    user_removed.php Once the user has clicked on the link provided in the email sent to them, they arrive at this page where they are given the final chance to delete their record. Once they delete it here, it is not reversible.

    index.php This is the home page containing the menu.

    get_user_details.php This page is accessed when the user has forgotten their username or password. It allows them to enter their email address which is then used by the send_email.php page.

    send_email.php This page checks whether the email address entered in get_user_details.php is in the database. If it is then the username and a new password are sent to that address. If not then the user is redirected to the home page where an error message is displayed.

    menu.php This is the menu containing the links to the various pages which are displayed on each page.

    Get the files


    These files can be downloaded here. Each one contains easily understood comments which explains the logic of the files. Once you have read through them, it will be very easy to adapt these files to suite your needs. Download

    Using Flexy in a simple web page – a tutorial

    5 April, 2007 (12:44) | PEAR | By: clive


    The Php page


    In this tutorial I will show how easy it is to use Flexy to create a HTML template. I decided on a simple two column page design with a top title section. On the left side is a brief description which is passed to the template as a variable. Below that is another text variable which is only displayed once the form (which appears on the right hand side) has been submitted. On submission, a variable ($showName) is set to be true, this variable is then passed through an if-else statement in the template. If true, then some text is displayed (as well as a picture on the right) else if false, then the form is displayed. There is also a date/time display on the left. This is passed as the value returned by a method. There are two arrays which are handled within the template using a foreach loop. The one is the list of items in the table on the left and the other, the links displayed below the picture after the form has been submitted.

    In order to follow, you will need a basic knowledge of HTML and Php. Make sure that you have a working installation of Php and PEAR and that you have the following two packages installed: PEAR and HTML_Template_Flexy. To make sense of the following explantion you will have to refer to the test_static.php file. The comments in that file as indicated by // will be repeated here with perhaps a little more of an explanation so as to make the logic easier to follow. Let’s start at the begining.

    Firstly we need to load the class definitions so make sure that you have the PEAR and HTML_Template_Flexy packages installed. Next, you need a reference to the static properties of the $options variable. This is done using &PEAR::getStaticProperty and is part of the process of setting up the template object. Then you need to set up the configuration options. You can set the configurations in an .ini file or use the $options array – in which case you don’t need the .ini file.You can also use the $options array to override the .ini configuration. This is done when we instantiate the class with this line of code $template = new HTML_Template_Flexy($options); Next, we set up the page controller classs, class controller_test wherein we declare the class member variables and the constructor method, function controller_test() as well as the other class methods: function start(), function output() and function get_date (). The constructor method controller_test() is automatically executed when the class is instantiated.

    The start () method checks whether the $_POST['your_name'] variable, sent when the form was submitted, is set. If it is set (in other words the name field in the form was filled in and the send button was clicked) then the variable $your_name is set to the value of the POST variable and $this->showName is set to be true. The $title member variable $this->title is set and a new object, anObject is created with $this->anObject = new StdClass. We can now add the members (as well as their values): web_address, picture, form_blurb, your_name and idig_blurb to this object. We then assign values to the arrays, an_array and links.

    Next we load the class definitions for the form elements with this line of code require_once ‘HTML/Template/Flexy/Element.php’; Then we create the HTML element for the form element your_name and then assign a value to it with this line $this->elements['your_name']->setValue($your_name); Here the value of the variable $your_name is assigned to the your_name element.

    Then we have the get_date() method which uses the Php’s date() function to return the current date and time. Finally we get the output section consisting of the output() method. This constructs the output object and if neccessary converts the Template markup into PHP code, and writes it to the compileDir (./templates/test_templates/) directory as set when we configured the options. Next, $output->outputObject($this,$this->elements); merges the controller object ($this) and the associative array of form elements ($this->elements) with the template (test_template.html) and outputs the result. Essentially this makes the values of the supplied object, $this (and loads the HTML_Template_Flexy_Elements elements, $this->elements) available to the template, test_template.html when it runs.

    The template


    When the above page is run, it uses the template described below. Basically this template consists of HTML code with Flexy placeholders which are then converted into php code.

    I won’t be commenting on the HTML code itself but only on the Flexy parts within the HTML document.

    The first Flexy placeholder that we come across is the {title} placeholder in the heading just below the first tag, . This defines the title of the page and appears in the title bar of the browser window. The {title} placeholder again appears in the body of the page, just below the tag, {title}. In both cases, the title gets it’s value from the member variable, $title in the php page. The next Flexy placeholder that we come across is {if:!showName}, this creates a php if statement using the variable $showName on compiling the template. What it does here, is to check whether the variable is false in which case it will execute the following code until it reaches the {else:} statement. If the $showName variable is true then the code following the {else:} statement is executed. So if $showName is false then the form and the form blurb will be displayed, else a picture and a number of links will be displayed. $showName will remain false until the user enters a name and submits the form. This will set $showName to be true and the relevant code within the template will be executed. The form’s input variable, your_name, will be passed to the variable $your_name using the POST form method and then to the object member, anObject.your_name within the start() method of the php page. The object member form_blurb is displayed with this piece of code, {anObject.form_blurb} and will display this piece of text:

    Enter your name in the input field and press SEND. The form will be sent (back to test_static.php) and the variable showName will be set to TRUE. This variable will then be used in the template if statement to determine whether or not to show certain sections of the page.

    Next we see {anObject.picture:h} which will display the picture as contained in the HTML code in test_static.php. The placeholder {anObject.picture:h}, with the h modifier will interpret the HTML code accordingly. The placeholder {anObject.picture} without the h modifier will merely echo the HTML code .

    Below the picture placeholder is the following code:

    {foreach:links,key,value}
       <a href=”{value}”>{key}</a>
    {end:}

    This loops through the links array and displays a set of links below the picture. Each link has value as the URL of the destination page and key as the label. Note that the foreach loop is closed with an end. A little way below this end is another end which closes off the {if:!showName} {else:} loop.

    Then we have {anObject.idig_blurb} which displays the object member variable, $idig_blurb. Followed by the object member, {anObject.web_address} which displays the web address. Then there is another if loop, {if:showName} containing the your_name object member variable which displays the name entered in the input field of the form and which was submitted on clicking the send button. The name is only displayed if the condition within the if statement, namely showName, is true. After this is the {get_date():h} placeholder which displays the return value of the get_date() method which is the current date and time in the specific format as determined by the parameters set in the php date() function.

    Finally there is another foreach loop, this time looping through the an_array array, listing the value of the key on the left and the value as the URL of the destination web page.

    Hopefully this should all make sense. You can download the relevant files here.

    Using PEAR:AUTH and PEAR:MDB2 to secure your website

    23 March, 2007 (15:07) | MySQL, PEAR | By: clive

    It’s easy and secure.

    Requirements


    It is assumed that you have a working knowledge of HTML and php and have the necessary PEAR packages installed. You will also need a database (I have used MySql here but you can use others, just make sure that you then install the necessary MDB2 driver for that database). The PEAR packages should include:

  • Auth – the authentication package
  • MDB2 – the database abstraction layer
  • MDB2_Driver_mysql – the MySql MDB2 driver
  • HTML_Quickform – which creates and processes HTML forms
  • HTML_Common2 – the abstract base class for HTML classes

  • The basic structure of the website


    I used the Andreas02 template created by Andreas Viklund for this tutorial. He has some greate templates, some of which are open source.

    This imaginary website consists of 4 pages, the main home or index page, a login page, a register page and a members page. I used the same HTML template (andreas02) for all the pages.

    Navigational structure


    The index page has a sidebar containing the login and register links. The user must register before he can log in. The login link takes the user to the login page where, on successfully logging in, the user is taken to the members page. The members page has a logout button which logs the user out and returns him to the index page. The register link takes the user to the register page. Successful registration takes the user to the login page. A failed registration takes the user back to the registration page and displays an error message. A failed login returns the user to the index page and displays an error message.

    The Index page


    This is basically the home page of the website and consists almost entirely of HTML. It is the standard Andreas02 template with a few php lines and 2 added links. The file is then saved as a php file.

    The Login page


    The login page uses the same Andreas02 template and includes some php code. This code displays a Quickform login form consisting of the username and password text boxes and a login button. On completing the form, the form is processed and the username and password are compared to those stored in the database. If a match is made, the user is redirected to the members page. If no match is made, the user is returned to the login page with an error message included in the URL. The login page uses Quickform to create and process the login form and MDB2 to connect to and query the database. It also uses Auth to authenticate the user.

    The Register page


    The register page uses the Andreas02 template and includes some php code. The code displays a Quickform consisting of the username and password text boxes and the register button. On completing the form, the form is processed and the database is querried to see whether the username exists. If it does then the user is returned to the register page with an error message stating that the user exists. If the user does not exist then the username and password are added to the database and the user is redirected to the login page. If the data is not added successfully to the database, then the user is redirected to the register page.

    The Members page


    The members page uses the Andreas02 template and includes some php code. The code displays a Quickform logout button. When clicked, it destroys cookies, sessions, flushes the output buffer, logs the user out and directs him to the index page.

    Download the file containing all the files for the imaginary website here

      Secure_site.zip (34.1 KiB, 371 hits)
    You need to be a registered user to download this file.

    . Useful to analyse the use of the php code (commented) which shows how to use Auth, MDB2 and Quickform within a website.

    Hope you find this short tutorial useful. Let me know if you need any help using Auth, Quickform, MDB2 or Flexy.

    PEAR:Quickform FAQ’s – a reference guide

    9 March, 2007 (10:44) | PEAR | By: clive

    If you’re not comfortable with using the PEAR:Quickform online manual and you would like to use Quickform then Quickform FAQ – a reference guide is what you need. Menu driven in a FAQ format, this guide is an absolute gem to have. Use it to create your forms, whether you’re creating forms for the first time or do so occasionally, this guide is great to have. Just select what you want to do from the menu and a brief explanation complete with sample code pops up. If there are any relevant methods, then these too are displayed at the click of a mouse.

    All in all a very handy reference tool. Use it to jog your memory, quickly. No more having to slog through the manual. Use online NOW!

    PEAR: Quickform and Flexy – a tutorial

    9 March, 2007 (10:01) | PEAR | By: clive


    PEAR Template_Flexy: a tutorial

    If you’re looking for an easy-to-use, quick-to-learn templating system then Flexy is the one for you. Flexy is a PHP code builder templating system like Smarty. Flexy uses a Tokenizer which allows the use of HTML tags and attributes to provide looping and conditionals within the template which is then compiled for final use. Flexy is also very fast.

    This tutorial is aimed at those who want to get up and running with Template _Flexy as fast as possible. It also covers using Flexy with Quickform forms. Ideally you should have a basic understanding of HTML and Php. Knowledge of php classes will prove useful but is not essential. After working through this tutorial and having a look at the relevant files, you should be able to comfortably produce your own Flexy templates. Download the free tutorial

    PEAR:HTML_QuickForm – using select in drop-down menus

    12 December, 2006 (12:59) | PEAR | By: clive

    I have included the code below in response to a query concerning the use of the Quickform select element and particularly the ability to select muliple options from the options displayed in the drop-menu. Have a look at the sample code click here to download the sample

    You can also use our free, online reference guide to Quickform. It’s in the format of a list of FAQ’s. For example, you select “How do I use the select element” from the drop-down list and a new window opens up explaining how to do this, together with example code. Have a look NOW!