Using native PHP sessions with CodeIgniter

Emir Buğra KÖKSALAN tarafından tarihinde yayınlandı

This so important thing. Becouse when you use some libraries with Codeigniter that library using native PHP session functions but Codeigniter not. This is so big problem. There is a method that you can use codeigniter’s session methods and native PHP session. We’re using Codeigniter version 2.2.0. I didn’t try this in Codeigniter 3 but it must work.

Step 1: Don’t autoload default session library.

In autoload.php you must remove default “session” library and add that line: “mysession” => “session”. Last status seems like that:

$autoload['libraries'] = array(
   "database",
   "mysession" => "session"
);

Step 2: Create new file that name is “mysession.php” in “application/libraries” folder

The file is basic file that manipulating native session via codeigniter’s predefined methods. For example when you call that $this->session->set_userdata($key, $val) the method is just making that: $_SESSION[$key] = $val; All codes are here:

<?

class session {
    public function userdata( $str ) {
        return $_SESSION[$str];
    }
    
    public function set_userdata( $key, $val ) {
        $_SESSION[$key] = $val;
    }

    public function sess_destroy() {
        session_destroy();
    }

    // TODO Add other necessary methods here.
}

Class name must be “session” not “mysession”. “mysession” is only filename. This is important.

Step 3: Add session_start() to index.php file

Now open the main index.php file and add session_start() to first line.

<?php
session_start();
require_once __DIR__ . '/vendor/autoload.php';

/*
 *---------------------------------------------------------------
 * APPLICATION ENVIRONMENT
 *---------------------------------------------------------------
 *
 * You can load different configurations depending on your
 * current environment. Setting the environment also influences
 * things like logging and error reporting.
 *
 * This can be set to anything, but default usage is:
 *
 * development
 * testing
 * production
 *
 * NOTE: If you change these, also change the error_reporting() code below
 *
 */
 define('ENVIRONMENT', 'production');
/*
 *---------------------------------------------------------------
 * ERROR REPORTING
 *---------------------------------------------------------------
 *
 * Different environments will require different levels of error reporting.
 * By default development will show errors but testing and live will hide them.
 */

if (defined('ENVIRONMENT'))
.............
.............

After this time you’re using native PHP session. You can use $this->session->[codeigniter methods] or directly $_SESSION variable or native PHP session functions.

For example you want to use any composer library with Codeigniter and that library is using native PHP session functions. You can entegrate this to your CI application and ready to rock. I'm using Facebook PHP SDK with Codeigniter 2 and it wants native PHP session. I downloaded Facebook PHP SDK with composer and I autoloaded it in my index.php file like this:

<?php
session_start();
require_once __DIR__ . '/vendor/autoload.php';
define('FB_APP_ID', 'MY FB ID');
define('FB_APP_SECRET', 'MY FB SECRET KEY');

Everything is fine. The working example is that: https://apps.facebook.com/extended-tools/

This code is in Github and link is: https://github.com/nixarsoft/native-php-session-codeigniter

Have a nice day 🙂


Emir Buğra KÖKSALAN

Java & PHP Developer

0 yorum

Bir yanıt yazın

Avatar placeholder

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Time limit is exhausted. Please reload the CAPTCHA.

Bu site, istenmeyenleri azaltmak için Akismet kullanıyor. Yorum verilerinizin nasıl işlendiği hakkında daha fazla bilgi edinin.