So, I am new to Codeigniter. I've set up a model class to handle user authorization, both for storing tokens in the db and an index in a cookie. I set up a cookie as a property of the class but can't seem to get it to store. This is one of those problems that is most likely stupidity on my part (see title) but I'm at a loss. Here's the relevant code:
class Auth_model extends CI_Model {
public function __construct() {
parent::__construct();
// Include files from google-php-client library
include_once APPPATH . "libraries/google-api-php-client-master/src/Google/autoload.php";
include_once APPPATH . "libraries/google-api-php-client-master/src/Google/Client.php";
include_once APPPATH . "libraries/google-api-php-client-master/src/Google/Service/Oauth2.php";
}
var $mm_cookie = array(
'name' => 'ID',
'value' => '',
'expire' => '86500',
'domain' => '',
'path' => '/',
'prefix' => 'pref',
'secure' => TRUE
);
public function setUserData ($p_aUserData) {
log_message("debug", "GET vars:" . print_r($_GET,true));
// Check for existing service ID
if( isset($p_aUserData['id']) ) { // Check the 'id'
$SQL = "SELECT ID FROM tbl_user WHERE tbl_user.id = " . $p_aUserData['id'];
$query = $this->db->query($SQL);
$result = $query->result();
if ($query->num_rows() > 0) {
$this->mm_cookie['value'] = $p_aUserData->ID;
...
$SQL = "INSERT INTO tbl_user (" [my columns] ") VALUES (" . $valueString . ")";
$query = $this->db->query($SQL);
if( !$query ) $retArray = array( 'status' => 'error', 'info' => array('message' => $this->db->_error_message(), 'code' => $this->db->_error_number(), 'SQL' => $SQL ) );
else {
$lastInsertID = $this->db->insert_id();
$this->mm_cookie['value'] = $lastInsertID;
$retArray = array( 'status' => 'success', 'info' => array('message' => "INSERT successful", 'lastInsertID' => $lastInsertID, 'SQL' => $SQL ) );
}
}
== 1 ==> log_message("debug", "Cookie Storing AUTH_MODEL: " . print_r($this->mm_cookie, true));</b>
$this->input->set_cookie($this->mm_cookie);
} else {
$retArray = array( 'status' => 'error', 'info' => array('message' => "No Service ID specified") );
}
return $retArray;
}
}
I've used this basic pattern before with other languages, including PHP (just not CI). Either I'm making a boneheaded mistake that I'm not seeing or something is wacky with CI. I am new to PHP namespaces but I don't see how that would affect this.
Sorry for the additional cruft but I wanted to show everything in case there is something I messed up.
At my mark in the code of "== 1 ==>" the $this->mm_cookie
array displays just as I would expect. The next line never results in a cookie. It seems that if I do not make it a property of the class, it works. I've set it as both a public and private property.
I'd like to know why it doesn't work as a property and if I've done something principally wrong that will reveal itself more significantly later in the process.
Scott.