dtrj74376 2015-05-07 09:42
浏览 46

PHP范围? 还是Codeigniter? 都? (我自己的愚蠢?)

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.

  • 写回答

3条回答 默认 最新

  • douyijin7741 2015-05-07 09:50
    关注

    Looking at your code, you don't seem to load the cookie helper from CodeIgniter. The documentation for cookies in CI2 and CI3 show that you need to load the helper by doing:

    $this->load->helper('cookie');
    

    I am assuming you aren't autoloading the helper though.

    评论

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题