Chapter 2. User Management

In this chapter, we will cover:

  • Viewing users
  • Creating users
  • Editing users
  • Deleting users
  • Generating passwords with CodeIgniter
  • Generating passwords with CodeIgniter – the bare bones
  • Forgot password? – resetting passwords with CodeIgniter

Introduction

Chances are that a lot of the sites and apps you'll build with CodeIgniter will need users, and there will be a need to manage them and their details directly, that is create, update, edit, and delete them.

In this chapter, we'll look at basic user management and, build a simple CRUD interface to manage and maintain those users in a database. Later, in Chapter 7, Creating a Secure User Environment, we will be looking at securing your user information with login and session functionality, but for now, we will concentrate on building a user management interface.

Before we begin, we'll need to alter some settings in a couple of config files in the application/config folder. We'll be editing the following files:

  • path/to/codeigniter/application/config/config.php
  • path/to/codeigniter/application/config/database.php

Find the following config values in the path/to/codeigniter/application/config/config.php file and amend them to reflect the following:

Config item

Change to

Description

$config['sess_cookie_name']

ci_session

This should be the name of the cookie written to the users computer.

$config['sess_expiration']

7200

This is the number of seconds a session should remain active after a period of no activity before becoming void.

$config['sess_expire_on_close']

TRUE

This specifies that if the user closes their browser, the session becomes void.

$config['sess_encrypt_cookie']

TRUE

This specifies that if the cookie should be encrypted on the user's computer; for security purposes, this should be set to TRUE.

$config['sess_use_database']

TRUE

This specifies whether or not to store sessions in the database. For security purposes, this should be set to TRUE. You will also need to create the session table, which can be found in the Database schema section.

$config['sess_table_name']

sessions

This specifies the name of the database table used to store session data.

$config['sess_match_ip']

TRUE

This specifies CodeIgniter should monitor the IP address of requests and against that of the session_id. If the IP of an incoming request doesn't match the previous values, the session is disallowed.

$config['sess_match_useragent']

TRUE

This specifies CodeIgniter should monitor the user agent address of requests and against that of the session_id. If the user agent of an incoming request doesn't match the previous values, the session is disallowed.

Find the following config values in the path/to/codeigniter/application/config/database.php file and amend them to reflect the following:

Config item

Change to value

Description

$db['default']['hostname']

localhost

The hostname of your database; this is usually either localhost or an IP address

$db['default']['username']

?

The username you wish to use to connect to your database

$db['default']['password']

?

The password used to connect to your database

$db['default']['database']

?

The name of the database, which you wish to connect to, for example, users

Database schema

Using the method of your choice (command line, phpmyadmin, and so on) enter the following code into your database:

CREATE TABLE IF NOT EXISTS `sessions` (
  `session_id` varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
  `ip_address` varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
  `user_agent` varchar(120) COLLATE utf8_bin DEFAULT NULL,
  `last_activity` int(10) unsigned NOT NULL DEFAULT '0',
  `user_data` text COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`session_id`),
  KEY `last_activity_idx` (`last_activity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(125) NOT NULL,
  `last_name` varchar(125) NOT NULL,
  `email` varchar(255) NOT NULL,
  `created_date` int(11) NOT NULL COMMENT 'unix timestamp',
  `is_active` varchar(3) NOT NULL COMMENT 'yes or no',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

INSERT INTO `users` (`id`, `first_name`, `last_name`, `email`, `created_date`, `is_active`) VALUES
(5, 'First Name', 'Last name', '[email protected]', 0, '0'),

What are the columns for and what type of data will we store in them? The following table is a guide to the preceding database schema:

Item name

Attributes

Description

user_id

INTEGER(11)

The table primary key.

user_first_name

VARCHAR(125)

The user's first name.

user_last_name

VARCHAR(125)

The user's last name.

user_email

VARCHAR(255)

The user's e-mail address, for example, .

user_created_date

INTEGER(11)

The unix timestamp for the date the user was created within the database.

user_is_active

INTEGER(1)

The Boolean value represented as 0 or 1, if the user is active. This variable specifies whether the user is active within the system. An active user can login, while inactive users cannot.

Tip

If you have already created a sessions table, then you can omit that table.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.34.178