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:

32 Responses to “How to Speed Up CodeIgniter”

  1. Thomas B. D. August 8, 2011 at 3:04 pm #

    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.

  2. eventsrecorder December 27, 2011 at 9:55 pm #

    hi
    i try this but i get this error

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

    • Rick December 27, 2011 at 10:47 pm #

      Perhaps is the CodeIgniter version.

      • Ashish Mahana June 26, 2015 at 6:39 am #

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

      • yanirk May 3, 2016 at 11:38 pm #

    • Son December 29, 2011 at 12:59 pm #

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

  3. Son December 29, 2011 at 1:08 pm #

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

    • Rick December 29, 2011 at 9:44 pm #

      Awesome, thanks for sharing Son πŸ˜‰

  4. Thor January 20, 2012 at 7:07 pm #

    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)

  5. Dan February 27, 2012 at 4:13 pm #

    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.

    • Rick February 27, 2012 at 4:20 pm #

      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

    • Shayan July 26, 2012 at 3:37 am #

      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.

  6. mrj0909 December 13, 2012 at 8:36 pm #

    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.

    • Rick December 13, 2012 at 10:51 pm #

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

      Try:

      or

      let me know if this helps.

      Cheers.

  7. mrj0909 December 13, 2012 at 8:43 pm #

    what do you mean by “prematurely outputting”

    • Rick December 13, 2012 at 10:49 pm #

      It means you might have a space or something at the beggining of the code.

      Also, it says you can not use the php echo function such as:

      • Mohammed April 20, 2014 at 12:42 am #

        I cannot use echo function where exactly?

        Thanks Rick!

      • Rick April 20, 2014 at 10:46 pm #

        Hi there Mohammed,

        I don’t quite understand your problem. Isn’t the optimization working for you? Does it show any errors?

        Rick

  8. mrj0909 December 14, 2012 at 5:41 pm #

    Again I am getting ” unsupported form of compression ” error

  9. Anvar Ulugov December 28, 2012 at 4:57 pm #

    Nice tutorial, thank you!

    • Rick December 28, 2012 at 8:08 pm #

      You’re welcome Anvar!

  10. IvanZ April 11, 2013 at 3:39 pm #

    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

    • Rick April 11, 2013 at 3:44 pm #

      Yup, gzip should be enough to boost your site’s load πŸ˜‰

  11. IvanZ April 11, 2013 at 3:42 pm #

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

    • Rick April 11, 2013 at 3:44 pm #

      w00t! πŸ˜€

  12. Riyaz October 29, 2015 at 11:37 am #

    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/”;”

  13. Anand May 11, 2016 at 9:42 am #

    Nice Post,

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

  14. Mahipal July 1, 2016 at 6:44 am #

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

  15. Haris Haide November 19, 2017 at 9:12 am #

    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

  16. Nads January 11, 2018 at 8:39 am #

    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.

    • Rick January 11, 2018 at 9:18 am #

      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

Leave a Reply

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