<?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>Short Like A Fox &#187; browser specific</title>
	<atom:link href="http://shortlikeafox.com/tag/browser-specific/feed/" rel="self" type="application/rss+xml" />
	<link>http://shortlikeafox.com</link>
	<description>They Say I'm Short...</description>
	<lastBuildDate>Wed, 29 Jul 2009 22:21:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to Write Browser Specific Code with PHP</title>
		<link>http://shortlikeafox.com/2008/07/21/how-to-write-browser-specific-code-with-php/</link>
		<comments>http://shortlikeafox.com/2008/07/21/how-to-write-browser-specific-code-with-php/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 04:17:10 +0000</pubDate>
		<dc:creator>ShortLikeAFox</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[browser specific]]></category>
		<category><![CDATA[quick fixes]]></category>

		<guid isPermaLink="false">http://shortlikeafox.com/?p=7</guid>
		<description><![CDATA[ So you want to write code that only appears on certain browsers&#8230; There are a number of reasons to want to do this. The first time I personally needed to do this occurred when I was trying to embed an mp3 on a certain page. For some reason I could not write the code [...]]]></description>
			<content:encoded><![CDATA[<!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	--><div style='float:right'><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://shortlikeafox.com/2008/07/21/how-to-write-browser-specific-code-with-php/&amp;t=How+to+Write+Browser+Specific+Code+with+PHP&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td></table></div><p>So you want to write code that only appears on certain browsers&#8230; There are a number of reasons to want to do this. The first time I personally needed to do this occurred when I was trying to embed an mp3 on a certain page. For some reason I could not write the code so that the mp3 would play on the browsers I test on (IE, Firefox, and Opera), and <a href="http://validator.w3.org/" target="_blank">validate</a> at the same time. If I remember correctly, it was Internet Explorer that was causing the problem. The solution I came up with was to use a little PHP to find out when the user was using IE, and then embed the mp3 in non-valid code if that was the case.</p>
<p>This solution led to the mp3 always playing correctly and the page always validating, because the W3C validator never identifies itself as IE. This might not be the most ethical way to reach W3C compliance, but it works.</p>
<p>Another time I remember needing to write browser specific code is when I was having a problem with IE 6 not displaying my .png images correctly. I googled around and found a couple of solutions to the problem, but both of them ended up messing up the overall layer locations on my pages. Instead of troubleshooting that problem, I went with the quick solution and decided to display .gifs when the user had IE 6. If the user had another browser that had .png problems I figured that was too bad for him. </p>
<p><strong>How to do it:</strong></p>
<p>First you need to write a little line of code to figure out what browser your user has. Here is how to do that with PHP: </p>
<ul>
<li><span class="style1"> <span class="style2">$visitorsOS =</span> <span class="style3">$_SERVER</span><span class="style2">[</span>'HTTP_USER_AGENT'<span class="style2">];</span></span></li>
</ul>
<p>Here are three examples of what $visitorsOS can look like </p>
<ul>
<li> <em>Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET</em></li>
<li><em>Opera/9.51 (Windows NT 5.1; U; pl)</em></li>
<li><em>Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox</em>
  </li>
</ul>
<p>The first user has Internet Explorer 6, the second Opera 9.51, and the third Firefox. </p>
<p>Now that we know more information than we really need about the visitor&#8217;s computer we need to put that information to good use. Say you want to write code that will only appear if the user is running any version of Internet Explorer. In that case you would do this:</p>
<blockquote>
<p class="style7"> <span class="style4">if</span> (<span class="style5">eregi</span>(&#8217;MSIE&#8217;,$visitorsOS)){</p>
<blockquote class="style7">
<p><em>CODE THAT WILL ONLY APPEAR IF USER HAS IE </em></p>
</blockquote>
<p class="style7">}</p>
<p class="style4">else<span class="style2">{</span> </p>
<blockquote class="style7">
<p><em>CODE THAT WILL APPEAR IN ALL OTHER CASES</em> </p>
</blockquote>
<p><span class="style7">}</span></p></blockquote>
<p>I use eregi, a case insensitive regular expression match instead of ereg, a case sensitive regular expression match. I don&#8217;t remember if I do this out of paranoia, or if I actually found a case where Internet Explorer identified itself as msie. In either case eregi won&#8217;t hurt anything so it is what I use. Wanting to write the code for specific versions of IE would only require a small change. instead of &#8230;(eregi(&#8217;MSIE&#8217;&#8230;) I would use something like (eregi(&#8217;MSIE 6.0&#8242;&#8230;) if I wanted code that only appeared for MSIE 6.0. </p>
]]></content:encoded>
			<wfw:commentRss>http://shortlikeafox.com/2008/07/21/how-to-write-browser-specific-code-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
