Blog
Native Input, Session, and Flash Variables in CodeIgniter
Posted on June 26, 2015 in CodeIgniter, MVC, PHP by Matt Jennings
CodeIgniter Input Variables
<?php
// Run a filter globally to fight against XSS attacks by
// updating the "application/config/config.php" file to "TRUE" like below:
$config['global_xss_filtering'] = TRUE;
// Capture post data in an input tag with an attribute of "name" like below AND
// the 2nd parameter (optional) of "TRUE" runs the data through the XSS filter
$name = $this->input->post('name', TRUE);
// Return all post items WITH the XSS filter
$this->input->post(NULL, TRUE);
// Return all post items WITHOUT the XSS filter
$this->input->post();
?>
CodeIgniter Session Variables
<?php
// Create a session variable, with the 1st parameter being the variable name AND
// the 2nd parameter being the variable value
// Example below assigns a "counter" session variable to 0
$this->session->set_userdata('counter', 0);
// Example below echos out 0
// as the "counter" session variable is assigned to 0
echo $this->session->userdata('counter');
// Get an array of all session variables that are set
$this->session->all_userdata();
// Unset a "counter" session variable
$this->session->unset_userdata('counter');
?>
CodeIgniter Flash Variables
<?php
// Create a flash variable, with the 1st parameter being the variable name AND
// the 2nd parameter being the variable value
// Example below assigns a "counter" session variable to 0
// When the page loads the Flash data is lost!!!
$this->session->set_flashdata('counter', 0);
// Example below echos out 0
// as the "counter" flash variable is assigned to 0
// ONLY after a form is submitted once
echo $this->session->flashdata('counter');
// Preserves a flash data variable "counter" through one or more HTTP requests
$this->session->keep_flashdata('counter');
?>
Leave a Reply