How to Speed Up CodeIgniter

3 Simple steps to speed up your CodeIgniter applications. HTML, Page Caching and Gzip.

1. Compress HTML output

We will need to create a hook. This hook will remove any whitespace in the HTML output.

Go to system/application/config/config.php and enable hooks:

Go to system/application/config/hooks.php and add the new hook:

Create the file system/application/hooks/compress.php and add the following code:

IMPORTANT: if you have any JavaScript within the HTML (not in a .JS file) make sure you don’t have JavaScripts comments in it.

Use the PHP comment system instead:

2. Cache functions

You can cache certain functions in your controllers.

Add the following line and it will create a cached HTML of the page.

Make sure the Application/cache folder is writable.

More info http://codeigniter.com/user_guide/general/caching.html

3. Enable Gzip Compression

Go to application/config/config.php and enable the Gzip compression:

35 comments
  • Be careful with this! White-space may be styled to be significant or may have meaning to specific browsers where:

    and

    Does NOT render equally.
    The best thing to do is simply to remove ^\s* and leave the rest alone. Even the “pros” haven’t found a right way of doing this without leaving some uncertainty about various scenarios regarding the significance of white-space.

    Simply compressing with GZip should suffice these days.

  • hi
    i try this but i get this error

    Fatal error: Call to undefined function compress() in system/core/Hooks.php on line 236

    • Perhaps is the CodeIgniter version.

      • you have to wrap the code for the file system/application/hooks/compress.php inside a function called compres()
        See how i did this

    • I am encountering this as well. Did you find the problem?

  • Figured out the problem. You need to wrap the code for the hook inside a function called “compress()”;

  • It would be nice if CodeIgniter’s caching library was:
    1. Mine-type aware,
    2. Query-string aware (easy hack),
    3. GZipped content before saving it to file, and then served the GZipped content to browsers that support decompression (99%?). Just a thought.
    4. Served HTTP cache headers (also easy hack)

  • WARNING: I am sure this is due to something I did wrong but this completely F**ked up my entire site. Even restoring the changed files to their originals did not save it. If this works for you great but I really wish I hadnt bothered.

    • Hi Dan,

      I’m sorry about that. All the tips in my post shouldn’t break your site.

      Even if they do (yeah, it could happen) you’d be able to restore with a backup.

      The output, the gzip or the cache can be turned off easily and without permanent effect!

      Please let me know if you can figure out the issue.

      Kind regards,

      Rick

    • Read this warning you must have use the echo in your controller which displays the blank pages specially inmac browsers:

      VERY IMPORTANT: If you are getting a blank page when compression is enabled it
      means you are prematurely outputting something to your browser. It could
      even be a line of whitespace at the end of one of your scripts. For
      compression to work, nothing can be sent before the output buffer is called
      by the output class. Do not “echo” any values with compression enabled.

  • I am getting this error after using this compression method

    The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.

    • Maybe you can try to got the index.php file and change error handling.

      Try:

      or

      let me know if this helps.

      Cheers.

  • what do you mean by “prematurely outputting”

  • Again I am getting ” unsupported form of compression ” error

  • Hello and thanks for these speedup suggestions.

    I had however a problem rising with the hooks, the W3C validator throws a 500 error, while the website renders correctly on firefox/chrome/ie

    For now I stick with Gzip and cleaned/minified css wich is sufficient I guess, even though i liked a lot the idea of compressing entire html source code

  • btw: Gzip reduced by 70/80% the size, a lot of benefits for few clicks and key strokes πŸ™‚

  • HI
    I have used your code but while every page getting load shows popup i can’t understand that
    “Please configure the polyfill’s absolute(!) script path before referencing the css-filters-polyfill.js, like so:

    var polyfilter_scriptpath = “/js/css-filters-polyfill/”;”

  • Nice Post,

    But getting problem while enabling hooks all the ajax functionality has stopped .

  • Can i change in .htaccess file for page speed ? how to minify css and js file in codeigniter.

  • I got these error ?

    This site can’t be reached

    The webpage at http://localhost:8080/metropolitan09/Grid/usergrid might be temporarily down or it may have moved permanently to a new web address.
    ERR_CONTENT_DECODING_FAILED

  • Not Working at all. A lot of bugs with this. when i did it its logged me in everywhere let suppose i logged in chrome browser and after that i hit any url to other browser or any other system its not asking to login.

    • Hello Nads,

      The code is 7 years old, we have so assume CodeIgniter has changed since then. The code above will probably no longer be valid.

      Best regards

  • Do you intend to update the code for newer version? And what is your take on the first comment regarding meaningful whitespaces?

    • Hello Deepak,

      No I do not intend to update the code I posted 7 years ago. I am no longer a PHP developer, I wouldn’t know how to do it πŸ™‚

      Best of luck!

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*