Professional CodeIgniter

(singke) #1

Chapter 3: A 10,000 - Foot View of CodeIgniter


60


//$username and $password are passed in from another process
//most likely a login form and then cleansed

$this- > db- > select(‘email’);
$this- > db- > from(‘users’);
$this- > db- > where(‘username’, $username);
$this- > db- > where(‘password’, $password);
$this- > db- > where(‘status’, ‘live’);
$this- > db- > limit(1);
$Q = $this- > db- > get();

if ($Q- > num_rows() == 1){
$row = $Q- > row();
$this- > session- > set_userdata(‘user_email’], $row- > email);
}

It would then be very simple to retrieve that e - mail if you ever wanted to print it in a view.

< ?php
echo $this- > session- > userdata(‘email’);
? >

Because CodeIgniter sessions are saved in a client - side cookie, many developers don ’ t trust them for
storage of sensitive information. Even with a 32 - character salt for encryption, it ’ s still possible that
somebody might tamper with these data. In this book, you use encryption on sessions but restrict its
usage to unimportant information. For the more sensitive things, like checking for a user login, you
should use PHP sessions.

CodeIgniter Helpers


Helpers , as their name implies, help you with specific tasks. Unlike libraries, helpers are not object -
oriented but procedural in nature. Each helper contains one or more functions, each focusing on a
specific task, with zero dependence on other functions.

Helpers can either be loaded locally or autoloaded in /system/application/config/autoload.php.
CodeIgniter ships with the following built - in helpers:

Array — The Array helper contains functions that help you work with arrays. For example, the
random_element() function takes an array as input and returns a random element from it.

Cookie — The Cookie helper contains functions that help you set, read, and delete cookie data.

Date — The Date helper contains functions that help you work with dates. For example, the
now() function returns the current time as a UNIX time stamp.

Directory — The Directory helper contains a single function that helps you work with directories.
For example, the directory_map function reads a specified directory path and builds an array
of it that contains all of its files and subdirectories.




Free download pdf