<?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>code-diesel &#187; pear</title>
	<atom:link href="http://www.codediesel.com/tag/pear/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Disabling the silence @-operator in PHP</title>
		<link>http://www.codediesel.com/php/disabling-the-silence-operator-in-php/</link>
		<comments>http://www.codediesel.com/php/disabling-the-silence-operator-in-php/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 08:47:34 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[peck]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2673</guid>
		<description><![CDATA[How to disable the silence error operator in php]]></description>
			<content:encoded><![CDATA[<p>PHP supports one error control operator: the at sign (@). When prepended  to an expression any error generated by that expression will be ignored. It can also be useful for hiding errors generated by various functions.Take the following simple example:</p>

<div class="wp_codebox"><table><tr id="p26731"><td class="code" id="p2673code1"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'data'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If the &#8216;data&#8217; parameter is not defined the expression will generate an error.</p>

<div class="wp_codebox"><table><tr id="p26732"><td class="code" id="p2673code2"><pre class="php" style="font-family:monospace;">Notice<span style="color: #339933;">:</span> Undefined index<span style="color: #339933;">:</span> data in <span style="color: #339933;">/</span><span style="color: #000000; font-weight: bold;">var</span><span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">9</span></pre></td></tr></table></div>

<p>You can hide the error using the silence @-operator.</p>

<div class="wp_codebox"><table><tr id="p26733"><td class="code" id="p2673code3"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span>  <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'data'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Although quite useful at some times, using the @-operator can have some annoying side effects. Say you are using some external libraries in your application which uses the @-operator. If everything works fine than good. But if the library is generating some errors than it becomes difficult to point the exact location where the error occurs, as the @-operator hides it. If the external library is large, it becomes a headache to remove all the @ from the code. One nice option I found is the <a target="_blank" href="http://pecl.php.net/package/scream">Scream</a> Pecl extension. The extension allows you to easily disable the @-operator in your code without making any actual changes to the code.<br />
<span id="more-2673"></span></p>
<h4>Installing the Scream extension</h4>
<p>As a pre-complied binary is not available, you need to make it yourself. The following shows commands to compile the extension on Ubuntu.</p>
<p>First you need to install the Pear distribution environment.</p>

<div class="wp_codebox"><table><tr id="p26734"><td class="code" id="p2673code4"><pre class="shell" style="font-family:monospace;">sudo apt-get install php-pear</pre></td></tr></table></div>

<p>Next you will need to install the php5-dev package to get the required PHP5 source files to compile additional modules.</p>

<div class="wp_codebox"><table><tr id="p26735"><td class="code" id="p2673code5"><pre class="shell" style="font-family:monospace;">sudo apt-get install php5-dev</pre></td></tr></table></div>

<p>Finally we are ready to actually create and install the extension.</p>

<div class="wp_codebox"><table><tr id="p26736"><td class="code" id="p2673code6"><pre class="shell" style="font-family:monospace;">sudo pecl install scream-0.1.0</pre></td></tr></table></div>

<p>Once the extension is created and installed, we need to add one to the php.ini file.</p>

<div class="wp_codebox"><table><tr id="p26737"><td class="code" id="p2673code7"><pre class="shell" style="font-family:monospace;">sudo gedit /etc/php5/apache2/php.ini</pre></td></tr></table></div>

<p>In the &#8216;extensions&#8217; section add the following line:<br />
extension=scream.so;</p>
<p>After the php.ini has been updated, you need to restart Apache, so that the new extension is loaded.</p>

<div class="wp_codebox"><table><tr id="p26738"><td class="code" id="p2673code8"><pre class="shell" style="font-family:monospace;">sudo /etc/init.d/apache2 restart</pre></td></tr></table></div>

<p>If hopefully all went well, the Scream extension should now be loaded, which you can confirm using phpinfo().</p>
<h4>Breaking the Silence operator</h4>
<p>Now you can disable the @-operator in your code using the following:</p>

<div class="wp_codebox"><table><tr id="p26739"><td class="code" id="p2673code9"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'display_errors'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">E_STRICT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Disable the @-operator</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'scream.enabled'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'data'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Or you can directly enable the extension in your php.ini.</p>

<div class="wp_codebox"><table><tr id="p267310"><td class="code" id="p2673code10"><pre class="php" style="font-family:monospace;">scream<span style="color: #339933;">.</span>enabled<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span></pre></td></tr></table></div>

<p>Now even though the silence operator is present the above code generates an error if the &#8216;data&#8217; parameter is not set. Atlast no need to hunt down for @&#8217;s while debugging.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/disabling-the-silence-operator-in-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Detecting user agents in php</title>
		<link>http://www.codediesel.com/pear/detecting-user-agents-in-php/</link>
		<comments>http://www.codediesel.com/pear/detecting-user-agents-in-php/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 13:48:29 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[browser]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[agents]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1660</guid>
		<description><![CDATA[how to detect browser agents in php]]></description>
			<content:encoded><![CDATA[<p>Every time you use your browser to access a website a User-Agent header is sent to the respective server.<br />
Detecting user agents on the server can be useful for many reasons. </p>
<p>1. Browsers Quirks – Despite the standardization in browsers, there will remain some quirks in various browsers that you will need to iron out on a regular basis.<br />
2. Personalize Content – It may be required to deliver different type of content depending on the browser type (although it is usually not recommended); whether mobile or otherwise.<br />
3. Illegal Access – Prevent bandwidth hogging bots and poorly programmed clients from downloading your content.<br />
<span id="more-1660"></span><br />
Below is a sample header for my site when using Firefox.</p>

<div class="wp_codebox"><table><tr id="p166011"><td class="code" id="p1660code11"><pre class="text" style="font-family:monospace;">http://www.codediesel.com/
&nbsp;
GET / HTTP/1.1
Host: www.codediesel.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6)
Gecko/2009011913 Firefox/3.0.6 FirePHP/0.2.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive</pre></td></tr></table></div>

<p>PHP already has a builtin function &#8211; <a target="_blank" href="http://us2.php.net/manual/en/function.get-browser.php">get_browser()</a> that lets you determine the capabilities of the user&#8217;s browser, but it requires a browscap.ini to be installed, which sadly is not bundled with PHP, and also the function can slow down your php script. The other way to get a user agent is to use php server variables like below,</p>

<div class="wp_codebox"><table><tr id="p166012"><td class="code" id="p1660code12"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_USER_AGENT'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In this post we will use the <a target="_blank" href="http://pear.php.net/package/net_useragent_detect/redirected">Net_UserAgent_Detect</a> Pear package to determine user agents.</p>
<h4>Installation</h4>
<p>Net_UserAgent_Detect being a Pear package we will use the Pear installer as below. I recommend to always use the Pear installer to download packages rather than downloading it manually as the Pear installer automatically downloads any dependent packages.</p>

<div class="wp_codebox"><table><tr id="p166013"><td class="code" id="p1660code13"><pre class="text" style="font-family:monospace;">pear install Net_UserAgent_Detect-2.5.1</pre></td></tr></table></div>

<h4>Usage</h4>
<p>Usage is quite simple. For example the following code:</p>

<div class="wp_codebox"><table><tr id="p166014"><td class="code" id="p1660code14"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Net/UserAgent/Detect.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getUserAgent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>will return the following string on Firefox 3.0.6: This can be quite useful if you need to log the users browser or OS type.</p>

<div class="wp_codebox"><table><tr id="p166015"><td class="code" id="p1660code15"><pre class="text" style="font-family:monospace;">Mozilla/5.0 (Windows; U; Windows NT 5.1; 
en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 FirePHP/0.2.4</pre></td></tr></table></div>

<p>getUserAgent() is only one of the many methods available in the class.</p>

<div class="wp_codebox"><table><tr id="p166016"><td class="code" id="p1660code16"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Net/UserAgent/Detect.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Browser String: '</span><span style="color: #339933;">.</span>Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getBrowserString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'OS String: '</span><span style="color: #339933;">.</span>Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getOSString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Javascript: '</span><span style="color: #339933;">.</span>Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getFeature</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'javascript'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Some other useful functions to directly test the type of browser are:<br />
isIE()<br />
isNavigator( )<br />
isNetscape( )</p>
<p>So to detect if the user is running Firefox:</p>

<div class="wp_codebox"><table><tr id="p166017"><td class="code" id="p1660code17"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$browser</span> <span style="color: #339933;">=</span> Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">isNetscape</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/pear/detecting-user-agents-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

