Welcome Guest, Not a member yet? Register   Sign In
[Workaround found] CodeIgniter 3.0 cannot access native $_SESSION
#1

(This post was last modified: 04-14-2015, 05:32 PM by gofrendi. Edit Reason: Find Solution )

Hi, I'm using CodeIgniter 3.0.
CI 3's session can also be accessed as $_SESSION.
However this $_SESSION is quiet different from PHP's native $_SESSION.

Let's say I have a controller like this:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends CI_Controller {
    public function 
index()
    {
        
$this->load->library('session');
        
var_dump($_SESSION);
    }


It will yield this:
array (size=1)
'__ci_last_regenerate' => int 1429015988


However, when I make another file in FCPATH
THIS IS NOT A CONTROLLER/VIEW/MODEL, JUST A PHP FILE WHICH IS CALLED DIRECTLY
PHP Code:
<?php
    if
(!isset($_SESSION)){
 
       session_start();
 
   }
 
   var_dump($_SESSION); 

It yield this:
array (size=0)
empty



This make CodeIgniter cannot share session with external application. Is it an expected behavior? Is there any workaround if I want to share codeigniter's session with external php script?

UPDATE:

Here is my ugly-but-work workaround:
CodeIgniter can't access native session, but external PHP can access CodeIgniter's session like this:
PHP Code:
ob_start();
include(
'index.php');
ob_end_clean();
$CI =& get_instance();
$CI->load->driver('session');
var_dump($_SESSION); 

This works, but it is actually awkward. When you include "index.php", it will also do all the process in your default controller's. However this is the easiest and best way I can find. Other solution (including decode the cookie) will be very hard.
Reply


Messages In This Thread
[Workaround found] CodeIgniter 3.0 cannot access native $_SESSION - by gofrendi - 04-14-2015, 06:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB