Access Values โ€‹โ€‹for Coded Value from External Files

In my codeigniter project, I added KCK finder.

he needs some session values โ€‹โ€‹that are controlled by codeigniter. How can I access CI session values โ€‹โ€‹from external files?

+4
source share
6 answers
<?php ob_start(); include('index.php'); ob_end_clean(); $CI =& get_instance(); $CI->load->library('session'); //if it not autoloaded in your CI setup echo $CI->session->userdata('name'); ?> 
+4
source

If you want to access sessions (and they are file-based) from outside the simulator code, 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); 
+1
source

Two solutions come to mind (which do not include decoding CI cookies)

1) Copy them to regular PHP sessions when you assign an internal CI:

 $_SESSION['name'] = $this->session->userdata('name'); 

So, you have access to every php file on your server; I think this is the fastest solution.

2) save the sessions in the database and connect to it to get the values.

0
source

Hi, before I get this code using googling.

  //path to your database.php file require_once("../frontend/config/database.php"); $cisess_cookie = $_COOKIE['ci_session']; $cisess_cookie = stripslashes($cisess_cookie); $cisess_cookie = unserialize($cisess_cookie); $cisess_session_id = $cisess_cookie['session_id']; $cisess_connect = mysql_connect($db['default']['hostname'], $db['default'] ['username'], $db['default']['password']); if (!$cisess_connect) { die("<div class=\"error\">" . mysql_error() . "</div>"); } $cisess_query = "SELECT user_data FROM ci_sessions WHERE session_id = '$cisess_session_id'"; mysql_select_db($db['default']['database'], $cisess_connect); $cisess_result = mysql_query($cisess_query, $cisess_connect); if (!$cisess_result) { die("Invalid Query"); } $cisess_row = mysql_fetch_assoc($cisess_result); $cisess_data = unserialize($cisess_row['user_data']); // print all session values print_r($cisess_data); 
0
source

I am using an external application to login and wanted to use data from CI sessions. I found this information http://codeigniter.com/forums/viewthread/86380/ on how to do this without starting anything from the CI infrastructure. This is an ideal solution for me, as it will prevent the occurrence of any incompatibilities.

If you have problems extracting information from a link:

  • If you use a ci session cookie, then only get the session cookie.
  • If you are using a ci session with a database, then to verify the session, you will receive a session cookie and a compliance request in the database in the ci_session table.
  • Use data and add data as necessary in the cookie (and in the database if you are using a database session).
0
source

The answer is Bens with include ('index.php') and getting the CI instance is not bad, but in my situation, these actions are too slow.

Since I have a codec installed to use files, I made this solution, which is slightly faster:

 function CIsession() { $pathToSessionFiles = 'path/to/files/set/in/codeigniter/config/'; $h = md5($_SERVER['REMOTE_ADDR']); // $config['sess_match_ip'] = TRUE; foreach( glob($pathToSessionFiles . '*' ) as $f ) { if( strpos($f, $h) ) { $s[ $f ] = filemtime($f); } } arsort($s); $s = array_keys($s); $s = reset($s); $s = file($s); $s = reset($s); $s = explode(';', $s); foreach($s as $k => $v) { $s[$k] = str_getcsv($v, ":", '"'); $s[$k][0] = substr(reset($s[$k]), 0, strpos(reset($s[$k]), '|')); $s[reset($s[$k])] = end($s[$k]); unset($s[$k]); } return $s; } 

Hope this helps someone!

0
source

All Articles