Skip to content

Making POST array persistent

Derek Jones edited this page Jul 5, 2012 · 6 revisions

(Originally posted in this topic pagination and post variables)

If the values from the**$_POST**array are used in a database query to extract a subset of records from the main database. and if this subset consists of more records than can be displayed on one page. Then, when a pagination link is clicked to create another page of records, the $_POSTarray is returned empty and the values are not available to replicate the filtering.

Therefore the problem is to make the $_POSTarray values persistent.

My way of doing this is involves extending the session class with an adaption of the code posted in this thread http://www.codeigniter.com/forums/viewthread/337/ by Kelvin Luck.

First, update your database with the following SQL:

ALTER TABLE `ci_sessions` ADD `lastpost_data` TEXT

then add the following code to the session class code (system/libraries/Session.php)

// --------------------------------------------------------------------

// Code to facilitate transfer of $_POST array to and from session database
// Based on code by Kelvin Luck published here http://www.codeigniter.com/forums/viewthread/337/

var $last_post;

/**
* Check if the $_POST global variable contains any data.
* if it does, copy it to the lastpost_data field in the sessions database.
* if it is empty, reload it with the last array .

* @author Dodmo
* @access public
* @param boolean
* @return void
*/
function get_last_post($clear='TRUE'){
if ((count($_POST)==0)&&($this->_is_last_post())){
    $_POST = $this->last_post;
}
else if (count($_POST)>0){
if($clear){
$last_post = array();
$this->set_last_post($_POST);
}
else{
$this->set_last_post($_POST);
$_POST = $this->last_post;
}
}
}
/**
* Test for previous $_POST data stored in session database
*
* @author Dodmo
* @access private
* @return boolean
*/
function _is_last_post()
{
$this->_readlast_post();
$myresult = (count($this->last_post)>0) ;
return $myresult; 
}


/**
* Fetch a specific item from the lastpost_data array in the session database
* (not required for get_last_post function but retained for future use.
  
* @authorKelvin Luck adapted by Dodmo
* @accesspublic
* @paramstring
* @returnstring
*/
function last_post($item)
{
if ($this->_readlast_post() ) {
return ( ! isset($this->last_post[$item])) ? FALSE : $this->last_post[$item];
} else {
return FALSE;
}
} // END last_post

/**
* Add or change data in the the lastpost_data array in the session database
* @authorKelvin Luck adapted by Dodmo
* @parammixed
* @paramstring
* @returnvoid
*/
function set_last_post($newdata = array(), $newval = '')
{
if ($this->_readlast_post()) {
if (is_string($newdata)) {
$newdata = array($newdata => $newval);
}

if (count($newdata) > 0) {
foreach ($newdata as $key => $val) {
$this->last_post[$key] = $val;
}
}

$this->_writelast_post();
}
} // END set_last_post

/**
* Internal function to read and unserialize the lastpost_data from the database
*
* @authorKelvin Luck adapted by Dodmo
* @accessprivate
* @returnboolean
*/
function _readlast_post(){
if ($this->use_database === TRUE) {
if (!isset($this->last_post)) {
$result = $this->object->db->query('SELECT lastpost_data FROM `'.$this->session_table.'` WHERE session_id=?', array($this->userdata['session_id']));
if ($result->num_rows() > 0) {
$row = $result->row();
$session = $row->lastpost_data;
if (count($session)>0){
$session = @unserialize($session);
}
if ($session == '') {
$session = array();
}
$this->last_post = $session;
return TRUE;
} else {
log_message('error', '_readlast_post called when there is not a valid session in the database');
return FALSE;
}
} else {
// already read it!
return TRUE;
}
} else {
log_message('error', 'You cannot access session->last_post unless you are using databases for your session!');
return FALSE;
}
} // END _readlast_post

/**
* Internal function to serialize and write the lastpost_data to the database
*
* @authorKelvin Luck adapted by Dodmo
* @accessprivate
* @returnvoid
*/
function _writelast_post()
{
$server_data_serialized = serialize($this->last_post);
$this->object->db->query($this->object->db->update_string($this->session_table, array('lastpost_data' => $server_data_serialized), array('session_id' => $this->userdata['session_id'])));
}

// END last_post

// End added code to facilitate transfer of $_POST array to and from session database

Now you have a new method you can call

$this->session->get_last_post(); // (the parameter defaults to TRUE) 

Ifget_last_post()is passedTRUEparameter or left void then thelastpost_datafield in the sessions database will be cleared before the a new $_POSTarray is inserted ($_POSTis left unmodified) If the function is passed FALSEparameter then the existinglastpost_datawill be amalgamated with the fresh $_POSTdata (exising keys are updated new ones added) and this amalgam is passed back into $_POSTfor the application to use.

DISCLAIMER I am new to php/mysql and anyone using this code should bear that in mind.

Clone this wiki locally