6 - CSCI 3000 Web Programming

Mr. Justin “JET” Turner
CSCI 3000 – Fall 2015
CRN 6710 - Section A – TR 9:30-10:45
CRN 10570 – Section B – TR 5:30-6:45
CI - Helpers
 Helper files are used to provide access to a few stand-
alone functions
 The functions could be used to help with any
components of the MVC structure
 Note: These functions are not tied into the parent
object, so the $this variable is not defined
 If we need access to the CI object, we get it using:
 $CI =& get_instance();

=& is the PHP pass-by-reference, so instead of copying the
object, we are pointing at the same object
CI - Helpers
 We can define multiple functions in the same helper
file, but we should only do this if they are strongly
related
 We need to create the helper file at:
 /application/helpers/filename_helper.php
 Then we can load the helper using:
 $this->load->helper(“filename”);
 Finally, we can all the helper functions directly
 function_name();
CI - Helpers
if ( ! defined('BASEPATH')) exit('No direct script access
allowed');
if ( ! function_exists(‘function_name'))
{
function function_name() {
$CI =& get_instance();
}
}
CI - Libraries
 Building a custom library can be done entirely from
scratch, or extending from a CI library for added
functionality
 For CI to properly load the library, it is important the
class name and file name match
CI - Libraries
 Library file location:
 /application/libraries/My_library.php
 Load the library:
 $this->load->library(“My_library”);
 Finally, accessing the library:
 $this->My_library->libraryFunction();
 Our constructor will also assign the CI object reference
to some instance data to better enforce our OOP
CI - Libraries
if ( ! defined('BASEPATH')) exit('No direct script access
allowed');
class myLibrary
{
public function __construct()
{
$this->ci =& get_instance();
}
}
CI - Libraries
 If you want to extend a CI library, or fully replace a
default one, then you just need to have it in the
libraries folder, named exactly the same


Email.php
class CI_Email{}
 Many of the CI libraries are prefixed with CI_
 The load/autoload functions will check for a local
library before moving to the framework/system for
library
CI - Sessions
 Session data is a way for your web application to
maintain data with a given user
 You can store variables in the session, and use those
values on every page visited by a specific user
afterwards
 Maintaining authentication is almost always at least
partially done through storing session data
 We might load some profile data from a database into
a session for easier access on future pages, such as
which theme to use in the application
CI - Sessions
 CI has a full session library writing data into session
variables and getting it back out
 $this->session->set_userdata(“name”, “value”);

//can also take an associative array
 $this->session->userdata(“name”);
 $this->session->has_userdata(“name”);
 $this->session->unset_userdata(“name”);
CI - Sessions
 CI also has a concept called “flash data” which is
session data that is not kept after the very next page
load
 This can be useful for error messages you want to display
on the next page, but don’t need after that
 $this->session->set_flashdata(“name”, “value”);
 $this->session->flashdata(“name”);
 $this->session->keep_flashdata(“name”);
CI – Encryption/Hashing
 There are two terms we need to define:
 Hashing

A one-way process of converting data from an arbitrary size
into data of a fixed size
 Encryption / Decryption

A two-way process converting data into an unrecognizable
form and then back into the original form
 When working with passwords, we want to perform a
hash function
 When they try to log in, we hash the password entered
and compare the two hash values
CI – Encryption/Hashing
 Encryption can be used for data we want to protect,
but need to be able to get the original formatted data
back at some point
 CI used to provide a method for hashing, but with
changes to recent versions of PHP, the built in hash()
function suites that need without a custom function
 http://php.net/manual/en/function.hash.php
 We can use hash_algos() to get a list of available hashing
algorithms
CI - Encryption
 The CI library has extensive information on available
encryption methods
 One item you need is a key, which is often produced
using a hash method
 You could use something about your logged in user, such
as their username, to create the key, then their data is
better tied to their account
Project 2 - CodeIgniter
 Create a CodeIgniter application that uses the
following:
 Create a form to collect data from the user

Use the Form Helper, Form Validation Library, File Upload
Library, and Image Manipulation Library
 Manipulate the uploaded image by resizing it
 Use the Output library to help you render the image
using CI (not a direct link to the image)

Something like: application/files/image/2 would render the
image from the row with ID = 2
Project 2 - CodeIgniter
 This project should be on a separate sub-domain
 project2.yourdomain.com
 For 10% bonus (2pts)
 Set up a log in page, and use the Session Library to help
you ensure that no page loads unless the user is logged
in

Provide a Username/Password in the dropbox comments
Reminders
 Project 1 – Responsive Site – Due Oct 8th
 The Midterm project will be an in-class assignment
Next Time
 Instructions for configuring Sessions in GoDaddy
 A quick review of what you can expect the mid term to
be like
 Time to work on Project 1 and 2, and ask questions if
you are running into any issues