Codeigniter用户会话错误

时间:2013-09-28 16:18:54

标签: php mysql codeigniter session login-control

我在Codeigniter框架中遇到了一个新问题。她是我的外出

Array
(
    [id] => 2
    [firstname] => Maruf
    [lastname] => Ifftekhar
    [slug] => 
    [email] => support@russelhost.com
    [email_subscribe] => 1
    [self] => 
    [phone] => 01767820010
    [company] => Russel Host
    [default_billing_address] => 
    [default_shipping_address] => 
    [ship_to_bill_address] => true
    [password] => 0689d59aa30bdca7207db3d449255650
    [active] => 1
    [group_id] => 1
    [confirmed] => 0
    [group_discount_formula] => - 0
    [expire] => 1380390903
)
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/secure.php

Line Number: 46
abida Sultana

这是控制器

`$`email = `$`this->input->post('email');
`$`password = `$`this->input->post('password');
`$`remember = `$`this->input->post('remember');
`$`redirect = `$`this->input->post('redirect');
`$`login = `$`this->Customer_model->login(`$`email, `$`password, `$`remember);
   echo '/pre>-----';
   print_r(`$`login);
   echo 'abida Sultana'.`$`login->last_name; --------------------Line Number: 46
   exit();

和模型是

function login(`$`email, `$`password, `$`remember = false) {
    `$`this->db->select('*');
    `$`this->db->where('email', `$`email);
    `$`this->db->where('active', 1);
    `$`this->db->where('password', md5(`$`password));
    `$`this->db->limit(1);
    `$`result = `$`this->db->get('customers');
    `$`customer = `$`result->row_array();

    if (`$`customer) {

        // Retrieve customer addresses
        `$`this->db->where(array('customer_id' => `$`customer['id'], 'id' => `$`customer['default_billing_address']));
        `$`address = `$`this->db->get('customers_address_bank')->row_array();
        if (`$`address) {
            $fields = unserialize($address['field_data']);
            $customer['bill_address'] = $fields;
            $customer['bill_address']['id'] = $address['id']; // save the addres id for future reference
        }

        $this->db->where(array('customer_id' => $customer['id'], 'id' => $customer['default_shipping_address']));
        $address = $this->db->get('customers_address_bank')->row_array();
        if ($address) {
            $fields = unserialize($address['field_data']);
            $customer['ship_address'] = $fields;
            $customer['ship_address']['id'] = $address['id'];
        } else {
            $customer['ship_to_bill_address'] = 'true';
        }


        // Set up any group discount 
        if ($customer['group_id'] != 0) {
            $group = $this->get_group($customer['group_id']);
            if ($group) { // group might not exist
                if ($group->discount_type == "fixed") {
                    $customer['group_discount_formula'] = "- " . $group->discount;
                } else {
                    $percent = (100 - (float) $group->discount) / 100;
                    $customer['group_discount_formula'] = '* (' . $percent . ')';
                }
            }
        }

        if (!$remember) {
            $customer['expire'] = time() + $this->session_expire;
        } else {
            $customer['expire'] = false;
        }

        // put our customer in the cart
        $this->go_cart->save_customer($customer);


        return $customer;
    } else {
        return false;
    }
}

3 个答案:

答案 0 :(得分:1)

如果你在var_dump变量上做了login,你会发现它是一个数组而不是一个对象,所以你不能像对象一样处理它。

所以转到你的模型并修改以下内容:

$customer = $result->row_array();

是:

$customer['result'] = $result->row_array();

然后在第46行使用它:

$login['result']->last_name;

为什么会这样:

因为您最初将$customer定义为对象。您还可以使用$customer['something']赋值模式在模型中稍后将其重新定义为数组。 所以你不能按自己的方式使用它,它可以是一个数组或一个对象。

答案 1 :(得分:0)

相同的错误。

Message: Undefined property: stdClass::`$last_name`
Filename: controllers/secure.php
Line Number: 46
46-----        echo 'Russel Host'.$login['result']->last_name;

答案 2 :(得分:0)

 //You used this code because row_array return array not object
 `$`login["result"] = `$`this->Customer_model->login(`$`email, `$`password, `$`remember);
  echo  $login['result']["last_name"];