Step 6 – Verify CodeIgniter configuration. Is everything OK?

Created at: December 3, 2014; Last update: December 7, 2015

(created at: December 3, 2014; last update: December 7, 2015)

We’ve installed CodeIgniter framework in 5 steps (or less, depends on how you count…). Now everything should be OK.

But is it? To see if everything is OK with your fresh installation of CodeIgniter, you would have to look in a lot of files. So why not make a controller that can take a look in various location and present you the status of your CodeIgniter installation? It’s like a phpinfo() of your CodeIgniter

Let’s do it. For those who are too busy or too scared to copy/paste long parts of code, I’ve made a zip archive with the files so that you can only look at the important parts in here and then just put the files where they belong. Also, if you have some proposals to change the files, you can find them on Github (https://github.com/avenirer/CI-configuration-verifier).

We create a file named Verify.php inside application/controllers, and write the following lines:

First of all we need to make sure we are in a development environment or at least that this controller can be seen only by your IP address (you’ll have to replace XXX.XXX.XXX with your IP address, of course):

Now that we’ve finished the constructor, let’s move on to our main method, the index(). We will start by getting the environment of our CI app:

The next thing we do is to see what classes, helpers and models are loaded by default. For this we can use some of the Loader.php properties. Being protected, the properties cannot be accessed directly, so we will need to make some getters in a MY_Loader.php. After we’ve done the Controller we will create a MY_Loader.php. Now let’s see what important info can we retrieve from the Loader.php:

Now we will retrieve everything that can be found inside the config.php file:

After that, let’s turn to our database. First of all, do we have a database connection established by default (with autoload – which I don’t recommend and you will see why in a next step)? If we do have the connection established let’s see what are the parameters of the connection:

Now we should verify that the directories that should be writable by our Framework are indeed writable:

Optionally, if you think you will have some directories where you or your users will upload files, you can add another check for those directories:

I guess we have all we need for now. It’s time to load our view so that we can show our analysis results:

As you can see at lines 19 to 24 we used some methods that are not native to the framework. Being loader methods, we must write a MY_Loader.php file inside application/core that extends the Loader controller. So let’s create the file and write the following lines:

Now, all we have to do is write the view that will display the data. So, create verify_view.php inside application/views with the following lines:

If everything went ok, we should call our controller from the browser. So go to http://localhost/verify and you should see something as below:

ci_verify

If you don’t see that, maybe something went wrong, so I’ve made a zip archive with the files for you to just copy/paste them. If it still doesn’t work, please write a comment and we will see where we both did something wrong.

19 thoughts on “Step 6 – Verify CodeIgniter configuration. Is everything OK?

  1. Gustavo

    Hey there!

    First thank you for the tutorial. Really helpful! The verify controller is very useful too.

    I’m just new to code igniter 3, but I’ve already created somethings with code igniter 2 in my localhost. The problem that I have is that when I run verify, in the database section, it displays “database is not loaded”. Do you know why? I’m using MAMP and my database settings are the same as the application that runs code igniter 2, which is this one:

    $active_group = ‘default’;
    $query_builder = TRUE;

    $db[‘default’] = array(
    ‘dsn’ => ”,
    ‘hostname’ => ‘localhost’,
    ‘username’ => ‘root’,
    ‘password’ => ‘root’,
    ‘database’ => ‘citest’,
    ‘dbdriver’ => ‘mysql’,
    ‘dbprefix’ => ”,
    ‘pconnect’ => TRUE,
    ‘db_debug’ => TRUE,
    ‘cache_on’ => FALSE,
    ‘cachedir’ => ”,
    ‘char_set’ => ‘utf8’,
    ‘dbcollat’ => ‘utf8_general_ci’,
    ‘swap_pre’ => ”,
    ‘encrypt’ => FALSE,
    ‘compress’ => FALSE,
    ‘stricton’ => FALSE,
    ‘failover’ => array(),
    ‘save_queries’ => TRUE
    );

    Cheers!

    Reply
    1. avenirer Post author

      Hello. I think that if you didn’t autoload the database (or you didn’t load any model until then), then the verify controller will tell you that there is no database connection

      Reply
      1. Shohanul Alam

        Hey Avenirer, how is it going? y, I am a CodIgniter fan and I am a learner. I’ve gone through your tutorials and I’m really loving it. It’s different then the others and really helpful.
        I need some opinion from you that where I can learn a to z CodeIgniter to be a pro like you. And I would really be happy if you give me a reply.

        Thanks.

        Reply
        1. avenirer Post author

          You’ve got it all wrong. I am not a pro. Pros are the ones that developed the framework. What I can tell you though is that, if you want to take a path towards something you only need the courage to take the first steps. And i mean “steps”, not “step”, because you may find yourself disappointed sometime for not knowing a solution to a given problem. You must persevere. Start by creating simple websites (the “wordpress” kind of sites – only presentation). Everything will come natural after a while. Also, don’t limit yourself to CodeIgniter. Learn PHP. There are functions in PHP that I am still finding out about. Don’t throw yourself to other frameworks like Symfony or Laravel. Those will be too big for a beginner to handle and you will become their prisoner. It’s like learning a new language and forgetting about PHP. Believe me, I’ve tried it… If you really want to learn PHP (but only for that, and not for productivity where speed is important), you may try Simple MVC Framework (http://simplemvcframework.com/php-framework). This one will teach you the hard way what a framework means.

          Learn everyday

          If you want to learn CodeIgniter, the most important thing is to open your browser everyday having the CodeIgniter manual (http://www.codeigniter.com/user_guide/) opened in a tab. I think that the manual put together by the developers is the best thing ever. Although CodeIgniter got to version 3, there are books for previous versions that are still good. Also, new books appeared about CodeIgniter. Buy them. It won’t cost you much, but it will mean a lot to the authors.

          Do something

          Do something. Don’t just follow tutorials. Do a website and work on it. If you just do the same coding that you see in tutorials and then forget about it, you waste your time. Have the courage to start a project in CodeIgniter and keep at it. Soon you will find out that you can’t imagine a website without the framework you like. I can’t stress it enough when I’m saying this. If you want to become a pro, start doing websites.

          Write about what you’ve learned

          Promote what you’ve learned. There are a lot of copy/paste websites that do not explain well. Also CodeIgniter needs promoters and this is the best way some of us can help the framework popularity. So, get yourself a blog and write. Even the simplest things may help some people. By the way, I started this website by writing HTML tutorials? Can you believe it? Besides the fact the you can help teach new beginners, you will feel compelled to learn new things in order to teach new things. Also… it’s good for your CV.

          Buy me a coffee

          You may not believe it but this website is voluntary work. In time, this idealism of doing voluntary work may fade away without coffee… So, on your first salary don’t be shy and do a donation 🙂 I promise to keep helping people. Also, if you have questions you can ask me by writing me at avenir.ro@gmail.com. This way we can learn togheter.

          Have faith

          Reply
  2. Jacky

    In my case, when trying to access localhost/verify, it just sends me back to welcome page.
    A few things I tried
    1. modifying route.php in ./application/config/development/ (to see if I can get the verify to work) and it presents a “The page isn’t redirecting properly”

    2. blanking the default_route in the route.php -> errors indicating there’s no default route.

    3. Intentionally making some errors in the php files. I just did a “//Some random text” at top of page and it is shown in the welcome page. So CI seems to be able to see the newly created php files.

    I’m now totally stumped 🙁

    Reply
    1. avenirer Post author

      Don’t worry…
      As you might have seen in the tutorial, we’ve made sure that no-one but us can access that page by verifying if the environment is set to “development” or by verifying that the IP address from which the page is visited is our IP (second block of code). So, to access the page you can either set the environment to “development” or (if you’re environment is in production mode) replace those “XXX.XXX.XXX” with your IP address.

      Reply
      1. Jacky

        Hey Avenirer

        I find troubleshooting helps a lot, that’s how I learn mostly. Makes me go through the code many times and really trace back step by step what’s going on.

        I found my problem…very newbie problem LOL

        It was in the verify controller…

        if(ENVIRONMENT!==’development’ || $_SERVER[‘REMOTE_ADDR’]==’localhost’)
        {
        $this->load->helper(‘url’);
        redirect(‘/’);

        }

        I was using “127.0.0.1” instead of “localhost”….ah well…

        Other errors follow but I don’t think they are a showstopper for me. At some point i’d like to fix them but I’ll move to step 7 😀

        1. Message: Undefined property: CI_DB_mysqli_driver::$autoinit. Filename: controllers/Verify.php
        2. Message: Undefined index: sess_encrypt_cookie. Filename: views/verify_view.php
        3. Message: Undefined index: sess_match_useragent. Filename: views/verify_view.php

        Reply
  3. Yeye

    Thanks for the tutorial, after loading the page am getting the following error:
    A PHP Error was encountered

    Severity: Notice

    Message: Undefined index: sess_match_useragent

    Filename: views/verify_view.php

    Line Number: 299

    Backtrace:

    File: C:\xampp\htdocs\CodeIgniter\application\views\verify_view.php
    Line: 299
    Function: _error_handler

    File: C:\xampp\htdocs\CodeIgniter\application\controllers\Verify.php
    Line: 91
    Function: view

    File: C:\xampp\htdocs\CodeIgniter\index.php
    Line: 292
    Function: require_once

    and

    Database:
    Database is not loaded

    Reply
    1. avenirer Post author

      This is weird… You should have a sess_match_useragent config key inside your application/config/config.php. Also, when you get a “database is not loaded” that means that you didn’t use any models up until the Verify controlle was run.

      Reply
  4. scion

    Hi Avenirer! First of all, thanks for this site, I working with CI too and get from your site many useful information. Dont u mind if ask u something

    // first of all we need to make sure we are in a development environment or at least that this controller can be seen only by your IP address (you’ll have to replace XXX.XXX.XXX with your IP address, of course)
    if(ENVIRONMENT!==’development’ || $_SERVER[‘REMOTE_ADDR’]==’XXX.XXX.XXX’)

    Here I see next “first of all we need to make sure we are in a development environment” but in code we first check developer environment – with it ok, but then we check IP, so how its working?

    If I understood right in ‘XXX.XXX.XXX’ i will have my ip that allow for access to this page, so if we have development condition and in REMOTE_ADDR my IP I will never see this page, or i missing something?

    Reply
  5. Mal

    Great Tut Avenirer,

    Very simple to follow after working through steps 1 – 5 on this step verify section I ran into a few issues using version CI 3.0.4
    application/config/database.php (doesn’t have autoinit so I was getting an error here)

    also from the step you said add ‘database’ to the autoloader, then remove it again, you don’t mention adding ‘database’ back into the autoloader or did I miss something??
    application/config/autoload.php
    $autoload[‘libraries’] = array(‘database’);
    finally in CI 3.0.4
    application/config/development/config.php the following is missing, so it throws an error
    $config[‘sess_encrypt_cookie’] = FALSE;

    After tinkering around a bit I got it all working apart from the uploads, this is where I’m abit confused I setup a vhost environment using xammp on windows 8 under the following:
    C:\xammp\htdocs\sites\my-sites\citest
    and set the following
    application/config/development/config.php
    $config[‘base_url’] = ‘citest’;
    and created a folder in C:\xammp\htdocs\sites\my-sites\citest\uploads so the structure is as follows:

    citest
    – application
    — cache
    — config
    – uploads (is this the correct place to put it as I keep getting the following citest/uploads is not writable)
    – system
    I’ve changed all the permissions possible and I still can’t get it writeable???

    Thanks once again, as it’s a good starting base for everyone this, it’s just the uploads have got me beat…

    Cheers

    Mal

    Reply
  6. Gabriel O

    Thanks for the terrific tutorial… I’m getting an error :
    Unable to load the requested file: helpers/my_loader_helper.php

    Not sure where it’s coming from. I don’t have anything in the helpers folder. And I don’t see where out files are pointing to the a helper class. Could the issue be that I’m including the prefix “MY_” in the subclass when I don’t need it, because it’s added by the config file?
    or should I change :
    $data[‘loaded_classes’] = $this->load->get_loaded_classes();
    to be
    $this->load->helper(‘MY_loader’);
    $data[‘loaded_classes’] = $this->load->get_loaded_classes();

    Thoughts?

    Reply
    1. avenirer Post author

      Did you create the MY_Loader inside the application/core directory, as the tutorial says?

      Reply
  7. Bernard

    Hello
    It is possible to add to this table, additionalinformation.
    I tested:
    * $route[‘default_controller’]
    and
    * $route[‘404_override’]
    Like this, for example:
    $defaut_page = $this->router->default_controller;
    $page_404 = $this->router->routes[‘404_override’];

    Reply
  8. MarcoCZen

    For the next newbie,

    If you get a
    “404 Error – Page Not found” when you load the url http://localhost/verify

    It means the class is not found.

    In my case , this was because of;

    class Welcome extends CI_Controller {

    which promptly edited to;

    class Verify extends CI_Controller {

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.