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 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口