Zend Certified Engineer

phpguru.org

Free PHP, Javascript and C# code

Displaying errors


7th May 2008, 288 views

In reply to this, here's a technique for controlling your error reporting:

<?php
    $isDev  
= $_SERVER['SERVER_NAME'] == 'dev.phpguru.org';
    
$isLive = $_SERVER['SERVER_NAME'] == 'www.phpguru.org';

    if (
$isDev) {
        
error_reporting(E_ALL);
        
ini_set('display_errors', 1);

    } elseif (
$isLive) {
        
error_reporting(0);
        
ini_set('display_errors', 0);
    }
?>

You don't have to create the $isDev/$isLive variables, but if you do you can reuse them elsewhere. I suppose you could if you wanted to use the Registry pattern I highlighted here some time ago, eg Config::$isDev

You could even use this technique to hide content on your live server that's not quite ready to go live yet.

Top 10 referrering pages

  1. http://209.85.171.104/translate_c?hl=es&sl=en&tl=... (1 referrals)
- +
Rate this article

Link to me

If you use any of the code on this site (and if you don't I guess) or it makes your life easier, I'd appreciate a link - http://www.phpguru.org. Thanks.

RSS Feed for Comments

Comments

Author: Mike G
Posted: 8th May 2008 04:08
In his article, Stanislav mentioned some of his concerns with security issues between local/live servers. Since anyone can change their 'Host' header, it's probably best to keep your development and production servers completely separate from one another.

I'm a fan of running Apache locally on top of my working copy, then uploading to production after I've thoroughly tested it on my end.

If you have a local HTTP server, you can configure the corresponding php.ini to have the display errors directive enabled always.
Quote
Author: harald
Posted: 8th May 2008 07:39
i never understood, why people hardcode error-level/error-reporting settings in their PHP applications. the most easy and most flexible way imo is just changing the settings in your servers php configuration file.
Quote
Author: Kae Verens
Posted: 8th May 2008 07:54
I agree with Harald.

i set my devel define (not a variable) in the same file as my DB settings.

btw - what happens if the HTTP_HOST is "phpguru.org" ? in that case, your method says it is not live and it is not dev.

why are there two variables at all? surely this is a boolean event - your site is either live or it is devel.
Quote
Author: Mike
Posted: 8th May 2008 09:24
I agree with the above commenters but would add that you also need to ensure that log_errors=1 and error_log is set to something writable.

While you don't want Joe Public to be able to see errors, developers need be able to for any obscure bugs you miss in local testing.
Quote
Author: Dave Marshall
Posted: 8th May 2008 09:28
I'm in agreement with all the fellas above, same code, different server settings.
Quote
Author: Dougal
Posted: 8th May 2008 09:58
Yeah, gotta agree with what i said above.

The only use of setting it up in your PHP code is if you don't have access to the PHP config. This happens with many shared hosting packages.

However, .htaccess can often do most of the config you need.

Quote
Author: Richard Heyes
Posted: 8th May 2008 10:37
Mike G:
> In his article, Stanislav mentioned some of his
> concerns with security issues between
> local/live servers. Since anyone can change
> their 'Host' header, it's probably best to keep
> your development and production servers
> completely separate from one another.

Yes anyone could change thir Host: header, but what they can't change is what my web server answers to. Not only that I could restrict my dev website to certain IPs addresses. In fact, who's to say that the two web sites (dev/live) are even on the same machine?

> I'm a fan of running Apache locally on top of
> my working copy, then uploading to production
> after I've thoroughly tested it on my end.

I run Windows locally and Linux on my web server. As far as I'm concerned, developing a website on a different OS than what you run on is a big no-no.

> If you have a local HTTP server, you can
> configure the corresponding php.ini to have the
> display errors directive enabled always.
Quote
Author: Richard Heyes
Posted: 8th May 2008 10:39
harald:
> i never understood, why people hardcode
> error-level/error-reporting settings in their
> PHP applications. the most easy and most
> flexible way imo is just changing the settings
> in your servers php configuration file.

I really don't see how that's more flexible than doing it your code.
Quote
Author: Richard Heyes
Posted: 8th May 2008 10:46
> btw - what happens if the HTTP_HOST is
> "phpguru.org" ? in that case, your method says
> it is not live and it is not dev.

No, my web server wouldn't give you anything, in fact with a normal web browser you wouldn't even get to my server as there's no DNS record for "phpguru.org".

> why are there two variables at all? surely this
> is a boolean event - your site is either live or
> it is devel.

Normally I use three - an intermediate "staging" web site that runs on the same box as live so the only real difference is the Host: header.
Quote
Author: Dougal
Posted: 8th May 2008 10:49
Most browsers just assume www. then i guess. Since going to phpguru.org in firefox takes me to www.phpguru.org
Quote
Author: Richard Heyes
Posted: 8th May 2008 10:51
> However, .htaccess can often do most of the
> config you need.

But if performance is important, you'd have override files off.
Quote
Author: Richard Heyes
Posted: 8th May 2008 11:06
Dougal:
> Most browsers just assume www. then i guess.
> Since going to phpguru.org in firefox takes me
> to www.phpguru.org

"Most"?

You've tried it in one and from that you assume most do the same? They don't. Opera doesn't, MSIE (7) doesn't, in fact the only browser that does that I've got is Firefox.
Quote
Author: Dougal
Posted: 8th May 2008 11:09
I said I guessed, not assumed ;)

I've only got FF on this machine so wasn't able to try others.
Quote
Author: harald
Posted: 8th May 2008 11:24
Richard Heyes:
> harald:
> > i never understood, why people hardcode
> > error-level/error-reporting settings in
> their
> > PHP applications. the most easy and most
> > flexible way imo is just changing the
> settings
> > in your servers php configuration file.
>
> I really don't see how that's more flexible
> than doing it your code.

in my opinion it's _always_ more flexible and less error-prone if you don't have to touch your source code, if you want to change configuration related settings.


Quote
Author: Ivan
Posted: 8th May 2008 12:06
you don't need variable/registry pattern or something else. One constant is enough. You can use it anywhere
Quote
Author: Dave Marshall
Posted: 8th May 2008 13:44
Richard Heyes:
> No, my web server wouldn't give you anything,
> in fact with a normal web browser you wouldn't
> even get to my server as there's no DNS record
> for "phpguru.org".

Your webserver gives me the web support solutions page for whatever domain I want to type in my address bar. A simple entry in a hosts file can sort that.

For example:

217.160.188.12 phpguru.org
Quote
Author: Dave Marshall
Posted: 8th May 2008 13:47
Basically, HTTP_HOST can be manipulated by the user and your code example directly depends on it. Change you if ... else if to and if ... else and default to a 'live' environment.
Quote
Author: Richard Heyes
Posted: 8th May 2008 19:22
> Your webserver gives me the web support
> solutions page for whatever domain I want to
> type in my address bar. A simple entry in a
> hosts file can sort that.
>
> For example:
>
> 217.160.188.12 phpguru.org

But you won't get my dev site. Security by obscurity comes in handy...
Quote
Author: Aaron Saray
Posted: 8th May 2008 21:24
Hello,

I would assume that you'd want to use $_SERVER['SERVER_NAME'] instead - this is something that could be counted on - not the HOST.
Quote
Author: Dave Marshall
Posted: 9th May 2008 09:21
Only because of the ordering of your virtual hosts. All I'm saying is that the code you posted shouldn't be relied on.

Shot in the dark though ...

http://dev.websupportsolutions.co.uk

SQL printed at the bottom
Quote
Author: Richard Heyes
Posted: 9th May 2008 10:55
Dave Marshall:
> Only because of the ordering of your virtual
> hosts. All I'm saying is that the code you
> posted shouldn't be relied on.

Perhaps. I've changed the variable in the article. In the past though anything serious I've worked has only had one website per server.
Quote
Author: Wiesbert Henning
Posted: 11th May 2008 15:41
You should give linux a try as dev env.
Believe me, once started, you will never want to go back. Test locally, copy over and all is fine.




Richard Heyes:

> I run Windows locally and Linux on my web
> server. As far as I'm concerned, developing a
> website on a different OS than what you run on
> is a big no-no.
>
> > If you have a local HTTP server, you can
> > configure the corresponding php.ini to have
> the
> > display errors directive enabled always.
Quote
Author: Richard Heyes
Posted: 11th May 2008 16:40
Wiesbert Henning:
> You should give linux a try as dev env.
> Believe me, once started, you will never want
> to go back. Test locally, copy over and all is
> fine.

So you don't even test before hand on your "live" box? You really should.
Quote
Author: DrSlump
Posted: 11th May 2008 23:27
My current solution is to define a 'debug enabled' variable and then override error/exception handling and act upon that setting.

The 'debug enabled' is defined in the application config but can also be overridden by sending a cookie with the request. This allows to display errors even in production when needed by just clicking on a simple bookmarklet.

Here is an example bookmarklet which just sets/unsets a cookie named "MYAPP_DEBUG":

javascript:(function(){var minutes = 60, exp = new Date();document.title = 'MyApp Debug Cookie disabled';if (document.cookie.match(/(^|;)\s*MYAPP_DEBUG=/)) {minutes=-minutes;} else {document.title='MyApp Debug Cookie enabled';}exp.setTime( exp.getTime() + minutes*60*1000 );document.cookie = 'MYAPP_DEBUG=true; expires=' + exp.toGMTString()}() )
Quote
Author: Richard Quadling
Posted: 12th May 2008 10:42
If you are on PHP 5.3+, then there is a great facility you can use which sets the error reporting level in the INI file dependent upon the path of the file being executed.

[PATH=/path/to/file/may/be/partial/or/complete]
error_reporting = E_NONE

Unfortunately, it doesn't seem to work on Windows.

[HOST=dev.site.com]
error_reporting = E_ALL

does work though.
Quote

Post Comment

Your name:
Your email:
(Don't worry, I won't spam you. Also, if you do put your email address in here, you'll get notified of new comments. If you don't, you won't.)
Comments:
  Do not post support type questions please

 
CAPTCHA image If you can't read the CAPTCHA then press the submit button to get another. Your comment will re-appear (as if by magic...).
Captcha image