Hi all,
You must have definitely used the Profiling in Codeigniter and error_reporting many a times in Development and Testing environment, but I am sure you must have missed it on a real Production environment.
As there are scenarios, where you want to quickly debug the Production application and find out what PHP errors is the application throwing, check the page profile, that too without putting the time and effort in replicating the whole production environment on your local machine, or perhaps a testing server.
This small piece of code(we could perhaps call it a hack), which I have used in almost all of my CI applications, will make your life very easy, without losing anything on the security of the system.
Following points, essentially sum up what exactly it does -
//we send profiler variable in the url
if ($this->input->get('profiler') == 'on' && $this->session->userdata('username') == 'dev') {
$this->session->set_userdata('app_profile', true);
} elseif ($this->input->get('profiler') !== 'on') {
$this->session->unset_userdata('app_profile');
}
//if the session data for app_profiler is set then enable the profile AND the error_reporting
if ($this->session->userdata('app_profile')) {
$this->output->enable_profiler(TRUE);
error_reporting(E_ALL);
}
As simple as that. :)
This code has to be placed in a file which is executed/called in all requests, like
Now when you change the URL which looks something like this
http://domain.com/app/index.php/ctrl1/fn1 to
http://domain.com/app/index.php/ctrl1/fn1?profiler=on the application will enable the error_reporting and the Codeigniter Profiler.
And to disable, all you need to do is, just change the url back to http://domain.com/app/index.php/ctrl1/fn1?profiler=off.
This hack is a very bare minimum one, which serves to my requirements. You could definitely add some bells and whistles to make it even more powerful and useful.
However, you need to be aware that if you are doing AJAX calls in the application, you need to slightly change the JSON returning functions, because if the application returns profiling data with the JSON, Javascript will not be able to parse it, and your app might break.
Hope this CI hack comes in handy for you. And also, I am sure that this small workaround can be implemented in other frameworks as well, although I have not tried implementing it myself yet!!
Happy Coding!!
Sandeep
You must have definitely used the Profiling in Codeigniter and error_reporting many a times in Development and Testing environment, but I am sure you must have missed it on a real Production environment.
As there are scenarios, where you want to quickly debug the Production application and find out what PHP errors is the application throwing, check the page profile, that too without putting the time and effort in replicating the whole production environment on your local machine, or perhaps a testing server.
This small piece of code(we could perhaps call it a hack), which I have used in almost all of my CI applications, will make your life very easy, without losing anything on the security of the system.
Following points, essentially sum up what exactly it does -
- Check for the dev(or root or admin, whichever name you use for the su access), if it is logged in, as we don't want others to see all the Profile data and other errors.
- Check for a specific query string sent in the URL(I am using profiler here), the value of which will enable/disable this feature.
- If the value is On/True, then enable both -
- Codeigniter Profiling
- Error Reporting
- Else disable both of them for any other value(which actually is the default behaviour)
//we send profiler variable in the url
if ($this->input->get('profiler') == 'on' && $this->session->userdata('username') == 'dev') {
$this->session->set_userdata('app_profile', true);
} elseif ($this->input->get('profiler') !== 'on') {
$this->session->unset_userdata('app_profile');
}
//if the session data for app_profiler is set then enable the profile AND the error_reporting
if ($this->session->userdata('app_profile')) {
$this->output->enable_profiler(TRUE);
error_reporting(E_ALL);
}
As simple as that. :)
This code has to be placed in a file which is executed/called in all requests, like
- MY_Controller, if all the controllers inherit this.
- Or in a hook which is executed in all the calls.
Now when you change the URL which looks something like this
http://domain.com/app/index.php/ctrl1/fn1 to
http://domain.com/app/index.php/ctrl1/fn1?profiler=on the application will enable the error_reporting and the Codeigniter Profiler.
And to disable, all you need to do is, just change the url back to http://domain.com/app/index.php/ctrl1/fn1?profiler=off.
This hack is a very bare minimum one, which serves to my requirements. You could definitely add some bells and whistles to make it even more powerful and useful.
However, you need to be aware that if you are doing AJAX calls in the application, you need to slightly change the JSON returning functions, because if the application returns profiling data with the JSON, Javascript will not be able to parse it, and your app might break.
Hope this CI hack comes in handy for you. And also, I am sure that this small workaround can be implemented in other frameworks as well, although I have not tried implementing it myself yet!!
Happy Coding!!
Sandeep
Comments