Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access codeigniter session values from external files

In my codeigniter project i have added KCK finder.

it needs some of session values which is controlled by codeigniter. how can i access CI session values from external files ?

like image 798
balaphp Avatar asked Oct 28 '11 07:10

balaphp


2 Answers

<?php
    ob_start();
    include('index.php');
    ob_end_clean();
    $CI =& get_instance();
    $CI->load->library('session'); //if it's not autoloaded in your CI setup
    echo $CI->session->userdata('name');
?>
like image 89
Ben Swinburne Avatar answered Nov 02 '22 13:11

Ben Swinburne


If you are looking to access the sessions (and they are file based) from outside of codeigniter and you do not want to load CI, you can do this:

define('ENVIRONMENT', 'development');

$ds = DIRECTORY_SEPARATOR;
define('BASEPATH', dirname(dirname(dirname(__FILE__))));
define('APPPATH', BASEPATH . $ds . 'application' . $ds);
define('LIBBATH', BASEPATH . "{$ds}system{$ds}libraries{$ds}Session{$ds}");

require_once LIBBATH . 'Session_driver.php';
require_once LIBBATH . "drivers{$ds}Session_files_driver.php";
require_once BASEPATH . "{$ds}system{$ds}core{$ds}Common.php";

$config = get_config();

if (empty($config['sess_save_path'])) {
    $config['sess_save_path'] = rtrim(ini_get('session.save_path'), '/\\');
}

$config = array(
    'cookie_lifetime'   => $config['sess_expiration'],
    'cookie_name'       => $config['sess_cookie_name'],
    'cookie_path'       => $config['cookie_path'],
    'cookie_domain'     => $config['cookie_domain'],
    'cookie_secure'     => $config['cookie_secure'],
    'expiration'        => $config['sess_expiration'],
    'match_ip'          => $config['sess_match_ip'],
    'save_path'         => $config['sess_save_path'],
    '_sid_regexp'       => '[0-9a-v]{32}',
);


$class = new CI_Session_files_driver($config);

if (is_php('5.4')) {
    session_set_save_handler($class, TRUE);
} else {
    session_set_save_handler(
        array($class, 'open'),
        array($class, 'close'),
        array($class, 'read'),
        array($class, 'write'),
        array($class, 'destroy'),
        array($class, 'gc')
    );
    register_shutdown_function('session_write_close');
}
session_name($config['cookie_name']);
session_start();
var_dump($_SESSION);
like image 26
Aziz Saleh Avatar answered Nov 02 '22 13:11

Aziz Saleh