In this chapter, we will cover:
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:
Find the following config values in the path/to/codeigniter/application/config/database.php
file and amend them to reflect the following:
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 |
---|---|---|
|
|
The table primary key. |
|
|
The user's first name. |
|
|
The user's last name. |
|
|
The user's e-mail address, for example, |
|
|
The unix timestamp for the date the user was created within the database. |
|
|
The Boolean value represented as |
3.12.34.178