PinoyTech.org

CodeIgniter, Kohana, Mootools, jQuery and CSS

Add SESSIONS to Profiler in CodeIgniter

Posted by teejay on April 21, 2010

I am a profiler addict. I tend to put this ON on every application that I start to build. However, unlink Kohana that shows the sessions in their profiler, CodeIgniter doesn't. Here's a way to add it to your profiler without hacking the core.

Create a MY_Profiler.php file in your libraries folder in the application folder. Copy and paste this and voila! Sessions will now be shown on your profiler.

 <?php if(!defined('BASEPATH')) exit('No direct script access allowed');
 class MY_Profiler extends CI_Profiler {
  /**
   * Adds session data to the profiler
   * Adds a table row for each item of session data with the key and value
   * Shows both CI session data and custom session data
   */
  function _compile_session() {    
   $output  = "\n\n";
   $output .= '<fieldset style="border:1px solid #009999;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
   $output .= "\n";
   $output .= '<legend style="color:#009999;">  '.'SESSION DATA'.'  </legend>';
   $output .= "\n";
    
   if (!is_object($this->CI->session)) {
    $output .= "<div style='color:#009999;font-weight:normal;padding:4px 0 4px 0'>".'No SESSION data exists'."</div>";
   } else {
    $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
    
    $sess = get_object_vars($this->CI->session);
  
    foreach ($sess['userdata'] as $key => $val) {
     if ( ! is_numeric($key)) {
      $key = "'".$key."'";
     }
   
     $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>$_SESSION[".$key."]   </td><td width='50%' style='color:#009999;font-weight:normal;background-color:#ddd;'>";
     if (is_array($val)) {
      $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
     } else {
      $output .= htmlspecialchars(stripslashes($val));
     }
     $output .= "</td></tr>\n";
    }
   
    $output .= "</table>\n";
   }
   $output .= "</fieldset>";

   return $output;    
  }
   
  function run() {
   $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";

   $output .= $this->_compile_uri_string();
   $output .= $this->_compile_controller_info();
   $output .= $this->_compile_memory_usage();
   $output .= $this->_compile_benchmarks();
   $output .= $this->_compile_get();
   $output .= $this->_compile_post();
   $output .= $this->_compile_queries();
   $output .= $this->_compile_session();

   $output .= '</div>';

   return $output;
  }
 } 

Categories: How To, Web Development

Tags: codeigniter, profiler

1 Comments

Teejay

I should say that I got this script from Dam1an from the forums a long time ago.

April 21st 2010

Comments are not allowed