<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>alecs &#187; errors</title>
	<atom:link href="http://www.ylipsis.com/blog/tag/errors/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ylipsis.com/blog</link>
	<description>web development, SEO, technology</description>
	<lastBuildDate>Sat, 15 May 2010 11:51:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP: sending emails whenever an error occurs</title>
		<link>http://www.ylipsis.com/blog/2009/01/php-sending-emails-whenever-an-error-occurs/</link>
		<comments>http://www.ylipsis.com/blog/2009/01/php-sending-emails-whenever-an-error-occurs/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 21:43:00 +0000</pubDate>
		<dc:creator>alecs</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ylipsis.com/blog/?p=126</guid>
		<description><![CDATA[Problem PHP, by default, displays an error directly in the user&#8217;s browser. Something like this is common around the web: Notice: Undefined variable: name in /abc/public_html/index.php on line 29 This is not so useful because if someone else caused this error you have no way of knowing about it and because potentially sensitive information about [...]]]></description>
			<content:encoded><![CDATA[<h3>Problem</h3>
<p>PHP, by default, displays an error directly in the user&#8217;s browser. Something like this is common around the web:</p>
<pre>Notice: Undefined variable: name in /abc/public_html/index.php on line 29</pre>
<p>This is not so useful because if someone else caused this error you have no way of knowing about it and because potentially sensitive information about your PHP code is leaking.</p>
<h3>Solution</h3>
<p><a href="http://us2.php.net/manual/en/function.set-error-handler.php" target="_blank">set_error_handler()</a> can be used to change the way PHP deals with errors by specifying a custom function to be called. Now you can easily output any message you want to the client, keeping the error details to yourself.</p>
<pre>set_error_handler("myErrorHandler",E_ALL);</pre>
<p>I&#8217;ve used E_ALL because I want to know all the error types that occur. Now the only thing left to do is create the myErrorHandler() function in which you print a message to your user and also email the sensitive error details to your own address:</p>
<pre>function myErrorHandler($errno, $errstr, $errfile, $errline){
    $message = '&lt;p&gt;Error number &lt;strong&gt;'.$errno.'&lt;/strong&gt;
                was encountered in &lt;strong&gt;'.$errfile.'&lt;/strong&gt;
                on line &lt;strong&gt;'.$errline.'&lt;/strong&gt;
                &lt;br /&gt;
                '.$errstr.'
                &lt;/p&gt;';
    mail("youremail@example.com","Error at yourWebsite",$message,"Content-type: text/html; charset=utf-8");
    echo "&lt;div class=\"error\"&gt;Script error detected.&lt;/div&gt;";
    return true;
}</pre>
<p>You may also include any debug info in your message, such as the $_SERVER['REQUEST_URI'], time, the user who generated the error (if you have a website with users) etc.</p>
<p>If you want to work with uncaught exceptions, use the <a href="http://us2.php.net/manual/en/function.set-exception-handler.php" target="_blank">set_exception_handler()</a> function instead. It works the same way as <a href="http://us2.php.net/manual/en/function.set-error-handler.php" target="_blank">set_error_handler()</a>.</p>
<p>This is a PHP equivalent to the <a href="http://agilewebdevelopment.com/plugins/exception_notifier" target="_blank">Exception Notifier</a> in Ruby on Rails.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ylipsis.com/blog/2009/01/php-sending-emails-whenever-an-error-occurs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
