• 0

كيف أجلب بيانات من Session على Controller- على بيئة Codeigniter؟

على تطبيق مبرمج بـ codeigniter لدي متحكمين (Controllers) كالآتي:

1-connect facebook

2-connect create

وذلك أن صفحة connect_facebook.php تتيح للمستخدم الدخول وتسجيل بياناته على Session وبالتالي إرسالها -البيانات- إلى connect_create.php. المشكل أن هذه البيانات لا أتمكن من الحصول عليها داخل المتحكم الثاني من قبل Session.

  • connect facebook:
<?php

/*
 * Connect_facebook Website
 */

class Connect_facebook extends Website {

    /**
     * Constructor
     */
    function __construct() {
        parent::__construct();

        // Load the necessary stuff...
        $this->load->config('account');
        $this->load->helper('form');
        $this->load->helper(array('language', 'ssl', 'url'));
        $this->load->library(array('authentication', 'facebook_lib'));
        $this->load->model(array('account_model', 'account_facebook_model'));
        $this->load->language(array('general', 'sign_in', 'account_linked', 'connect_third_party'));
    }

    function index() {
        // Enable SSL?
        maintain_ssl($this->config->item("ssl_enabled"));

        // Check if user is signed in on facebook
        if ($this->facebook_lib->user) {
            // Check if user has connect facebook to a3m
            if ($user = $this->account_facebook_model->get_by_facebook_id($this->facebook_lib->user['id'])) {
                // Check if user is not signed in on a3m
                if (!$this->authentication->is_signed_in()) {
                    // Run sign in routine
                    $this->authentication->sign_in($user->account_id);
                }
                $user->account_id === $this->session->userdata('account_id') ? $this->session->set_flashdata('linked_error', sprintf(lang('linked_linked_with_this_account'), lang('connect_facebook'))) : $this->session->set_flashdata('linked_error', sprintf(lang('linked_linked_with_another_account'), lang('connect_facebook')));
                redirect('users/account_linked');
            }
            // The user has not connect facebook to a3m
            else {
                // Check if user is signed in on a3m
                if (!$this->authentication->is_signed_in()) {
                    // Store user's facebook data in session
                    $this->session->set_userdata('connect_create', array(array('provider' => 'facebook', 'provider_id' => $this->facebook_lib->user['id']), array('fullname' => $this->facebook_lib->user['name'], 'firstname' => $this->facebook_lib->user['first_name'], 'lastname' => $this->facebook_lib->user['last_name'], 'gender' => $this->facebook_lib->user['gender'], 'email' => $this->facebook_lib->user['email'],
                    // not a required field, not all users have it set
                            'picture' => 'http://graph.facebook.com/' . $this->facebook_lib->user['id'] . '/picture/?type=large' // $this->facebook_lib->user['link']
                            )));
                    // send this data to the user submit form

                    $data['locale'] = $this->facebook_lib->user['locale'];
                    $data['session'] = $this->session->userdata('connect_create');
                    $data['facebook_id'] = $this->facebook_lib->user['id'];
                    $data['username'] = $this->facebook_lib->user['name'];
                    $data['gender'] = $this->facebook_lib->user['gender'];
                    $data['email'] = $this->facebook_lib->user['email'];
                    $this->view('site/connect_create', $data);

                } else {
                    // Connect facebook to a3m
                    $this->session->userdata('account_id');
                    $this->session->set_flashdata('linked_info', sprintf(lang('linked_linked_with_your_account'), lang('connect_facebook')));
                    redirect('user/account_linked');
                }
            }
        }

        // Load facebook redirect view
        $this->load->view("site/redirect_fb");
    }

}
  • connect create:
class Connect_create extends Website {

    /**
     * Constructor
     */
    function __construct()
    {
        parent::__construct();

        // Load the necessary stuff...
        $this->load->config('account');
        $this->load->helper(array('language', 'ssl', 'url'));
        $this->load->library(array('authentication', 'form_validation'));
        $this->load->model(array('account_model', 'account_details_model', 'account_facebook_model'));
        $this->load->language(array('general', 'connect_third_party'));
    }

    /**
     * Complete facebook's authentication process
     *
     * @access public
     * @return void
     */
    function index()
    {
        // Enable SSL?
        maintain_ssl($this->config->item("ssl_enabled"));

        // Redirect user to home if sign ups are disabled
        if ( ! ($this->config->item("sign_up_enabled"))) redirect('tttt');

        // Redirect user to home if 'connect_create' session data doesn't exist
        if ( ! $this->session->userdata('connect_create')) redirect('bbbbbb');



        $data['connect_create'] = $this->session->userdata('connect_create');

        // Setup form validation
        $this->form_validation->set_error_delimiters('<span class="field_error">', '</span>');
        $this->form_validation->set_rules(array(array('field' => 'connect_create_username', 'label' => 'lang:connect_create_username', 'rules' => 'required'), array('field' => 'connect_create_email', 'label' => 'lang:connect_create_email', 'rules' => 'trim|required|valid_email|max_length[160]')));

        // Run form validation
        if ($this->form_validation->run())
        {
            // Check if username already exist
            if ($this->username_check($this->input->post('connect_create_username', TRUE)) === TRUE)
            {
                $data['connect_create_username_error'] = lang('connect_create_username_taken');
            }
            // Check if email already exist
            elseif ($this->email_check($this->input->post('connect_create_email'), TRUE) === TRUE)
            {
                $data['connect_create_email_error'] = lang('connect_create_email_exist');
            }
            else
            {
                // Destroy 'connect_create' session data
                $this->session->unset_userdata('connect_create');

                // Create user
                $user_id = $this->account_model->create();
                                // Add user details
                $this->account_details_model->update($user_id, $data['connect_create'][1]);


                // Connect third party account to user
                switch ($data['connect_create'][0]['provider'])
                {
                    case 'facebook':
                        $this->account_facebook_model->insert($user_id, $data['connect_create'][0]['provider_id']);
                        break;
                }

                // Run sign in routine
                $this->authentication->sign_in($user_id);
            }
        }

        $this->load->view('site/connect_create', isset($data) ? $data : NULL);
    }

ما سبب ذلك؟ وكيف أحلّ المشكل؟

انشر على الشّبكات الاجتماعية


رابط هذه المساهمة
  • 0

لحل المشكل اذهب إلى ملف config.php الموجود في المسار application/config.

عدل على القيمة config$ لتصبح:

$config['cookie_domain']="";

أيضا حاول تغيير session library داخل ملف autoload config من خلال application/config/autoload.php

وذلك بتغيير قيمة autoload$ إلى :

$autoload['libraries'] = array('session');

جرب الآن.

تمّ تعديل بواسطة E.Nourddine

انشر على الشّبكات الاجتماعية


رابط هذه المساهمة

يجب أن تكون عضوًا لدينا لتتمكّن من التعليق

انشاء حساب جديد

يستغرق التسجيل بضع ثوان فقط


سجّل حسابًا جديدًا

تسجيل الدخول

تملك حسابا مسجّلا بالفعل؟


سجّل دخولك الآن