Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Sessions, forms and validation in CodeIgniter - early examples

CodeIgniter is becoming a very popular PHP Framework and increasingly I'm being asked to use/show elements of it on private PHP training courses.

All web based applications need certain elements of code such as form and session handling, data validation, routing of URLs to the appropriate script and separation of business logic from the look and feel of the page, and that's where a web framework come in, in addition to the base language such as PHP (or Ruby or Python or Perl). And CodeIgniter is a really appropriate framework for many of our customers, as it provides plentiful facilities in the major areas in which they're needed without too much bloat.

I wrote a "Hello CodeIgniter World" article [here] just the other day, let's now extend that to take a look at forms and sessions.


There's a need in most web applications to remember user's data from one page to the next, and at the same time to ensure that multiple users don't have their data mixed up with one another. There's a need to have that data secured, and cleared away if it's abandoned where someone leaves the site without completion a sale.

PHP has a built in session system, but there's an improved one available in CodeIgniter. Before you can use it, you need to set a security key in application/config/config.php, for example:

  $config['encryption_key'] = 'My training key is easy - yours should be mixedup';

You'll then load the session handler in your controller:


and you can read and save data using methods on the session object; here' an example that sets up a 'current' value if one doesn't exist and increments it each time the page is completed. More usually, you'll use session variables to store user names, products selected so far, and the like.

  $pagecount = $this->session->userdata('current');
  if ($pagecount === FALSE) $pagecount = 1;
  $pagecount += 1;

Form Validation

CodeIgniter has a form validation system built in. Again, you need to load the form validator into your controller:


and then you can set a whole load of rules:

  $this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');
  $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');

The rules can be applied in a single "hit", with the controller going on to produce either a refresh of the page for correction, or to save the data / update the model and proceed to the next page:

  if ($this->form_validation->run() === FALSE)
    // Calling for a form to be completed / corrected
  } else {
    // Update the model here! Move on to next page!

Form Display

Within a form, there's usually a requirement echo back a previous value, and the set_value method's provided to let you do that:

  <input type="text" name="username" value="<?= set_value('username') ?>" size="50" />

Here's the initial form:

There' also a requirement to flag errors, and there's a validation_errors method to do that:

  <?= validation_errors() ?>

Here's an output sample with some validation issues flagged:

There's far more flexibility than you'll see in this example - you can split the error messages out and put them with each input box if you wish, you can change the text and so on.

Personally, I prefer to wrap up the repetitive elements of form boxes into my own function as that makes for easier maintenance later on, and ensures consistency across each input field. So in an extension of that code above, my input boxes become

  <?= box("Username","username") ?>
  <?= box("yerpassword","password") ?>
  <?= box("AGAIN","passconf") ?>

and there's an extra function provided:

  function box ($prompt,$name) {
  $html = "<h5>$prompt</h5>";
  $html .= '<input type="text" name="' .$name .'" value="' . set_value($name) .'" size="50" />';
  return $html;

And finally the success page:

Links to source code:
Complete controller - [here].
View form - Mark 1 - [here]
View form - with function - [here]
Success page - [here]
Sample routing (via .htaccess) - [here]

(written 2013-04-13, updated 2013-04-20)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H321 - PHP - CodeIgniter
  [4053] Frameworks - learning through exploring and understanding data sources - (2013-03-27)
  [4060] CodeIgniter - an excellent PHP framework with an easy start point - (2013-04-06)
  [4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)

Back to
Seamless, integrated IT - we have a long way to go!
Previous and next
Horse's mouth home
Forward to
Backups by crossover between network centres - setting up automatic scp transfers
Some other Articles
MVC and Frameworks - a lesson from first principles in PHP
Handling requests to a forum - the background process
Apache httpd - a robust, open source web server
Backups by crossover between network centres - setting up automatic scp transfers
Sessions, forms and validation in CodeIgniter - early examples
Seamless, integrated IT - we have a long way to go!
Curl and curling from PHP
The highs and lows of customer service - Cheltenham
stdClass in PHP - using an object rather than an associative array
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4062_Ses ... mples.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb