<?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>AndrewEnsley.com &#187; Programming</title> <atom:link href="http://andrewensley.com/tag/programming/feed/" rel="self" type="application/rss+xml" /><link>http://andrewensley.com</link> <description>My little contribution to the WWW</description> <lastBuildDate>Mon, 06 Sep 2010 21:33:17 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <cloud domain='andrewensley.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' /><div style="display:none;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">suggest</a></div><div style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">participate</a></div> <item><title>WordPress: Embed Youtube Feed With a Simple Shortcode</title><link>http://andrewensley.com/2010/08/wordpress-embed-youtube-feed-with-a-simple-shortcode/</link> <comments>http://andrewensley.com/2010/08/wordpress-embed-youtube-feed-with-a-simple-shortcode/#comments</comments> <pubDate>Sat, 14 Aug 2010 02:20:15 +0000</pubDate> <dc:creator>Andrew</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[Channel]]></category> <category><![CDATA[Feed]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Shortcode]]></category> <category><![CDATA[Stream]]></category> <category><![CDATA[Video]]></category> <category><![CDATA[WordPress]]></category> <category><![CDATA[Youtube]]></category><guid isPermaLink="false">http://andrewensley.com/?p=1572</guid> <description><![CDATA[<a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow" style="display:none;">support</a><span style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">help</a></span><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><div style="display:none;">marketing</div></a>WordPress shortcodes are great. They let you perform complex operations or display dynamic content with just a tiny bit of text in any page or post. I recently needed a way to embed the most recent videos from a Youtube channel (very dynamic information) in a WordPress page, so I decided to write a shortcode [...]<p></p>]]></description> <content:encoded><![CDATA[<a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><span style="display:none;">home</span></a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><span style="display:none;">site-map</span></a><div style="display:none;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">store</a></div><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a><p><img src="http://static.andrewensley.com/wp-content/uploads/zemanta/wordtube.png" alt="" title="WordTube" width="290" height="68" class="alignright size-full wp-image-1614" /></p><p><a href="http://codex.wordpress.org/Shortcode_API" title="Shortcode API << WordPress Codex" target="_blank">WordPress shortcodes</a> are great.  They let you perform complex operations or display dynamic content with just a tiny bit of text in any page or post.</p><p>I recently needed a way to embed the most recent videos from a Youtube channel (very dynamic information) in a WordPress page, so I decided to write a shortcode for it.</p><p>The code is actually pretty simple.  Just add the following to your theme&#8217;s <a href="http://codex.wordpress.org/Theme_Development#Functions_File" title="Theme Development << WordPress Codex" target="_blank">functions.php</a> file:</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> youtube_feed_shortcode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$atts</span><span style="color: #009900;">&#41;</span><br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Defaults:</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/extract"><span style="color: #990000;">extract</span></a><span style="color: #009900;">&#40;</span>shortcode_atts<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'user'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'flamadiddle86'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// youtube user</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'limit'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// maximum number of videos</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'height'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">385</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// video height</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'width'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">480</span> <span style="color: #666666; font-style: italic;">// video width</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$atts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/json_decode"><span style="color: #990000;">json_decode</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/file_get_contents"><span style="color: #990000;">file_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://gdata.youtube.com/feeds/api/users/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$user</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/uploads?alt=json'</span><span style="color: #009900;">&#41;</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><br /> &nbsp; &nbsp; <span style="color: #000088;">$counter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;div class=&quot;youtubefeed&quot;&gt;'</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'feed'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'entry'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$vid</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vid</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'media$group'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'media$content'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vid</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'$t'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$ycontent</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vid</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'$t'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$content</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;object width=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$width</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; height=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$height</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'&lt;param name=&quot;movie&quot; value=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;&lt;/param&gt;'</span><span style="color: #339933;">.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;'</span><span style="color: #339933;">.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;'</span><span style="color: #339933;">.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'&lt;embed src=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$width</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; height=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$height</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;&lt;/embed&gt;&lt;/object&gt;'</span><span style="color: #339933;">.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'&lt;div class=&quot;youtubetitle&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'&lt;div class=&quot;youtubecontent&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$ycontent</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$counter</span><span style="color: #339933;">++;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$counter</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span><br /> <span style="color: #009900;">&#125;</span><br /> <br /> add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'youtubefeed'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'youtube_feed_shortcode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div><p><br/></p><p>And it couldn&#8217;t be easier to use.  The videos you see below were embedded simply by putting this code in the post:</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[youtubefeed]</div></td></tr></tbody></table></div><p><span id="more-1572"></span><br /> <br/><br /><hr/><div class="youtubefeed"><object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/vR4FH2uwhg8?f=user_uploads&app=youtube_gdata"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/vR4FH2uwhg8?f=user_uploads&app=youtube_gdata" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object><div class="youtubetitle">rm -rf /*</div><div class="youtubecontent">I've always wanted to do this.</div> <object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/9Kxnnok-8tY?f=user_uploads&app=youtube_gdata"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/9Kxnnok-8tY?f=user_uploads&app=youtube_gdata" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object><div class="youtubetitle">Primary to Secondary</div><div class="youtubecontent">Going from semi-auto ruger to semi-auto pistol.  The word of the day is "tactical."</div></div></p><hr/><br/></p><p>The shortcode is completely configurable.  The following code would have resulted in the exact same output:</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[youtubefeed user=&quot;flamadiddle86&quot; limit=&quot;5&quot; height=&quot;385&quot; width=&quot;480&quot;]</div></td></tr></tbody></table></div><p>You can change any option as needed.</p><p>You might have noticed the code gives each element a particular class to make it CSS friendly.  All you have to do is add CSS rules for three classes: .youtubefeed, .youtubetitle, and .youtubecontent.</p><p>My stylesheet looks like this:</p><div class="codecolorer-container css default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.youtubefeed</span> <span style="color: #00AA00;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span><br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span><br /> <span style="color: #00AA00;">&#125;</span><br /> <br /> <span style="color: #6666ff;">.youtubetitle</span> <span style="color: #00AA00;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1.6em</span><span style="color: #00AA00;">;</span><br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span><br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span><br /> <span style="color: #00AA00;">&#125;</span><br /> <br /> <span style="color: #6666ff;">.youtubecontent</span> <span style="color: #00AA00;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span><br /> <span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div><p>A quick note about requirements:</p><ul><li>Your server&#8217;s PHP configuration needs to have <a href="http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen" title="PHP: Runtime Configuration" target="_blank">allow_url_fopen</a> set to On.</li><li>You&#8217;ll also need the <a href="http://www.php.net/manual/en/intro.json.php" title="PHP JSON Extension: Introduction" target="_blank">PHP JSON extension</a></li></ul><p>Most hosts meet those requirements.</p><p>So there you have it.  It&#8217;s a quick and dirty way to get functional, always up-to-date Youtube streams in your WordPress posts and pages.</p> <a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><img border="0" width="0" height="0" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a><p>Related posts:<ol><li><a href='http://andrewensley.com/2009/09/my-first-wordpress-plugin-project-honey-pot/' rel='bookmark' title='Permanent Link: My first WordPress Plugin: Project Honey Pot'>My first WordPress Plugin: Project Honey Pot</a></li><li><a href='http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/' rel='bookmark' title='Permanent Link: PHP &#8211; Redirect and continue (without abort)'>PHP &#8211; Redirect and continue (without abort)</a></li><li><a href='http://andrewensley.com/2009/10/php-and-ffmpeg-performing-intelligent-video-conversion/' rel='bookmark' title='Permanent Link: PHP and FFMPEG &#8211; Performing intelligent video conversion'>PHP and FFMPEG &#8211; Performing intelligent video conversion</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://andrewensley.com/2010/08/wordpress-embed-youtube-feed-with-a-simple-shortcode/feed/</wfw:commentRss> <slash:comments>19</slash:comments> </item> <a href="http://andrewensley.com/seaworthy.php" rel="nofollow" style="display:none;">notice</a> <item><title>PHP: Validate an IP Address</title><link>http://andrewensley.com/2010/03/php-validate-an-ip-address/</link> <comments>http://andrewensley.com/2010/03/php-validate-an-ip-address/#comments</comments> <pubDate>Sun, 14 Mar 2010 05:14:47 +0000</pubDate> <dc:creator>Andrew</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Data Filtering]]></category> <category><![CDATA[IP]]></category> <category><![CDATA[IP address]]></category> <category><![CDATA[IPv4]]></category> <category><![CDATA[IPv6]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Regex]]></category> <category><![CDATA[Regular expression]]></category> <category><![CDATA[Validation]]></category><guid isPermaLink="false">http://andrewensley.com/?p=1329</guid> <description><![CDATA[<a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><div style="display:none;">partner</div></a><span style="position:absolute;top:-250px;left:-250px;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">contact</a></span><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"></a><div style="position:absolute;top:-250px;left:-250px;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">podcast</a></div><span style="display:none;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">international</a></span>So you need to check if some string is a valid IP address. You could simply test it against a regular expression: 1234567function is_valid_ipv4&#40;$ip&#41; &#123; &#160; &#160; return preg_match&#40;'/\b(25[0-5]&#124;2[0-4][0-9]&#124;[01]?[0-9][0-9]?)\.'. &#160; &#160; &#160; &#160; '(25[0-5]&#124;2[0-4][0-9]&#124;[01]?[0-9][0-9]?)\.'. &#160; &#160; &#160; &#160; '(25[0-5]&#124;2[0-4][0-9]&#124;[01]?[0-9][0-9]?)\.'. &#160; &#160; &#160; &#160; '(25[0-5]&#124;2[0-4][0-9]&#124;[01]?[0-9][0-9]?)\b/', $ip&#41; !== 0; &#125; Regular Expression obtained here This will actually [...]<p></p>]]></description> <content:encoded><![CDATA[<div style="display:none;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">suggest</a></div><div style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">participate</a></div><span style="display:none;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">feedback</a></span><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a><p>So you need to check if some string is a valid IP address.  You could simply test it against a regular expression:</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> is_valid_ipv4<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'</span><span style="color: #339933;">.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'</span><span style="color: #339933;">.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'</span><span style="color: #339933;">.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br /> <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div><p><a href="http://www.regular-expressions.info/examples.html" target="_blank" title="Regular Expression Examples">Regular Expression obtained here</a></p><p>This will actually work for most situations, but it&#8217;s lacking in a few ways.  Suppose you want to exclude <a href="http://en.wikipedia.org/wiki/Private_network" title="Private Network - Wikipedia" target="_blank">private or reserved IP addresses</a>.  Maybe you want to validate IPv6 addresses too; not just IPv4.</p><p>Enter <a href="http://us2.php.net/manual/en/book.filter.php" target="_blank" title="PHP's Data Filtering Extension">PHP&#8217;s Data Filtering Extension</a>.  It just works, and you don&#8217;t have to worry about maintaining (or properly applying) complex regular expressions.</p><p><span id="more-1329"></span></p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> is_valid_ip<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span> <span style="color: #000088;">$include_priv_res</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$include_priv_res</span> ?<br /> &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span> FILTER_VALIDATE_IP<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/filter_var"><span style="color: #990000;">filter_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span> FILTER_VALIDATE_IP<span style="color: #339933;">,</span> FILTER_FLAG_NO_PRIV_RANGE <span style="color: #339933;">|</span> FILTER_FLAG_NO_RES_RANGE<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br /> <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div><p>Now to test it.</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$ips</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'72.215.140.69'</span><span style="color: #339933;">,</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'192.168.0.1'</span><span style="color: #339933;">,</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">,</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'10.0.0.1'</span><span style="color: #339933;">,</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'255.255.255.0'</span><span style="color: #339933;">,</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'andrewensley.com'</span><span style="color: #339933;">,</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'255.255.256.0'</span><span style="color: #339933;">,</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'::1'</span><span style="color: #339933;">,</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'fe00::0'</span><span style="color: #339933;">,</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'2001:4860:0:1001::68'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ips</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ip</span><span style="color: #339933;">,</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> is_valid_ip<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'yes'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #339933;">;</span><br /> <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div><p>The above will output:</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">72.215.140.69 yes<br /> 192.168.0.1 no<br /> 127.0.0.1 yes<br /> 10.0.0.1 no<br /> 255.255.255.0 no<br /> andrewensley.com no<br /> 255.255.256.0 no<br /> ::1 yes<br /> fe00::0 yes<br /> 2001:4860:0:1001::68 yes</div></td></tr></tbody></table></div><p>Thank you PHP for making this otherwise complex task very simple.  And even better: it&#8217;s fast!</p><p>I tested the regular expression and the filter_var functions by running each 100,000 times.  Here are the timings for each test:</p><ul><li>1.53423094749 seconds with the filter_var function</li><li>3.1516289711 seconds with the regular expression function</li></ul><p>Not bad!  Happy filtering.</p> <a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><img border="0" width="0" height="0" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a><p>Related posts:<ol><li><a href='http://andrewensley.com/2009/09/my-first-wordpress-plugin-project-honey-pot/' rel='bookmark' title='Permanent Link: My first WordPress Plugin: Project Honey Pot'>My first WordPress Plugin: Project Honey Pot</a></li><li><a href='http://andrewensley.com/projects/' rel='bookmark' title='Permanent Link: Projects'>Projects</a></li><li><a href='http://andrewensley.com/2009/10/php-and-ffmpeg-performing-intelligent-video-conversion/' rel='bookmark' title='Permanent Link: PHP and FFMPEG &#8211; Performing intelligent video conversion'>PHP and FFMPEG &#8211; Performing intelligent video conversion</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://andrewensley.com/2010/03/php-validate-an-ip-address/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow" style="display:none;">support</a> <item><title>Google&#8217;s Chrome OS &#8211; First Impressions</title><link>http://andrewensley.com/2009/11/googles-chrome-os-first-impressions/</link> <comments>http://andrewensley.com/2009/11/googles-chrome-os-first-impressions/#comments</comments> <pubDate>Fri, 20 Nov 2009 18:48:43 +0000</pubDate> <dc:creator>Andrew</dc:creator> <category><![CDATA[Operating Systems]]></category> <category><![CDATA[Chrome OS]]></category> <category><![CDATA[First Impressions]]></category> <category><![CDATA[Google]]></category> <category><![CDATA[Google Chrome]]></category> <category><![CDATA[Operating System]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Review]]></category> <category><![CDATA[Source code]]></category> <category><![CDATA[Virtual machine]]></category> <category><![CDATA[VirtualBox]]></category> <category><![CDATA[VMware]]></category><guid isPermaLink="false">http://andrewensley.com/?p=1120</guid> <description><![CDATA[<a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><div style="display:none;">marketing</div></a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><img border="0" width="0" height="0" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><span style="display:none;">home</span></a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><span style="display:none;">site-map</span></a><div style="display:none;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">store</a></div>Yesterday, Google officially announced their new Chrome OS and explained what it will be when it&#8217;s released.  They also published the source code to allow developers and other interested parties to take a peek.  I&#8217;m one of those &#8220;other interested parties.&#8221; The Setup I didn&#8217;t build the OS from source myself, but downloaded a pre-compiled [...]<p></p>]]></description> <content:encoded><![CDATA[<a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow" style="display:none;">notice</a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><img src="http://andrewensley.com/wp-content/plugins/project-honey-pot-spam-trap/images/home.png" height="0" width="0" border="0"/></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><div style="display:none;">partner</div></a><p>Yesterday, Google officially announced their new Chrome OS and explained what it will be when it&#8217;s released.  They also published the source code to allow developers and other interested parties to take a peek.  I&#8217;m one of those &#8220;other interested parties.&#8221;<br /> <span id="more-1120"></span></p><h3>The Setup</h3><p>I didn&#8217;t build the OS from source myself, but downloaded <a href="http://lifehacker.com/5408932/chrome-os-virtual-machine-build-ready-for-your-testing">a pre-compiled VMWare image</a>.  Then, using <a href="http://forums.virtualbox.org/viewtopic.php?f=6&amp;t=22422">CloneVDI</a>, I converted it to a <a href="http://www.virtualbox.org/">VirtualBox</a> virtual hard disk.  I just created a new Virtual Machine for Linux and added the .vdi.  Done!</p><h3>The Tour</h3><p>First, you&#8217;re presented with a logon screen.  My first thought was &#8220;uhhh&#8230;. now what?  I didn&#8217;t create an account&#8221;.  Then I remembered: &#8220;This is a Google product.  Try your Google login.&#8221;  And whadayano?  It worked!</p><div id="attachment_1126" class="wp-caption aligncenter" style="width: 310px"><a href="http://static.andrewensley.com/wp-content/uploads/2009/11/login1.png"><img class="size-medium wp-image-1126" title="Login Screen" src="http://static.andrewensley.com/wp-content/uploads/zemanta/login-300x2251.png" alt="Login Screen" width="300" height="225" /></a><p class="wp-caption-text">Login Screen</p></div><p>I don&#8217;t know why it was off-center like that.  It came up like that every time for me.</p><p>The first place I went to after logging in was a button at the top left that looked like a Chrome logo.  That gave me this page:</p><div id="attachment_1122" class="wp-caption aligncenter" style="width: 310px"><a href="http://static.andrewensley.com/wp-content/uploads/2009/11/account_page1.png"><img class="size-medium wp-image-1122" title="Google Short Links" src="http://static.andrewensley.com/wp-content/uploads/zemanta/account_page-300x2251.png" alt="Google Short Links" width="300" height="225" /></a><p class="wp-caption-text">Google Short Links</p></div><p>I don&#8217;t know what &#8220;Google Short Links&#8221; is, but it wouldn&#8217;t let me login with my regular username/password here.</p><p>I wanted to see what I was working with, so I went to the &#8220;About&#8221; dialog.</p><div id="attachment_1121" class="wp-caption aligncenter" style="width: 310px"><a href="http://static.andrewensley.com/wp-content/uploads/2009/11/about1.png"><img class="size-medium wp-image-1121" title="About" src="http://static.andrewensley.com/wp-content/uploads/zemanta/about-300x2251.png" alt="About" width="300" height="225" /></a><p class="wp-caption-text">About</p></div><p>It&#8217;s using Chromium 4.0.253.0 (32535).</p><p>Next, I went to the options menu.</p><div id="attachment_1129" class="wp-caption aligncenter" style="width: 310px"><a href="http://static.andrewensley.com/wp-content/uploads/2009/11/options_menu1.png"><img class="size-medium wp-image-1129" title="Options Menu" src="http://static.andrewensley.com/wp-content/uploads/zemanta/options_menu-300x2241.png" alt="Options Menu" width="300" height="224" /></a><p class="wp-caption-text">Options Menu</p></div><p>The first page provides you with some time and touchpad settings.</p><div id="attachment_1127" class="wp-caption aligncenter" style="width: 310px"><a href="http://static.andrewensley.com/wp-content/uploads/2009/11/options_dialog1.png"><img class="size-medium wp-image-1127" title="Options Dialog" src="http://static.andrewensley.com/wp-content/uploads/zemanta/options_dialog-300x2251.png" alt="Options Dialog" width="300" height="225" /></a><p class="wp-caption-text">Options Dialog</p></div><p>One setting that would be nice to have?  Screen resolution.  I couldn&#8217;t find any place to change that, and the 800&#215;600 default resolution feels incredibly confining.  I found the bottom of the Basics tab humorous:</p><div id="attachment_1128" class="wp-caption aligncenter" style="width: 310px"><a href="http://static.andrewensley.com/wp-content/uploads/2009/11/options_dialog21.png"><img class="size-medium wp-image-1128" title="Basics Tab" src="http://static.andrewensley.com/wp-content/uploads/zemanta/options_dialog2-300x2241.png" alt="Basics Tab" width="300" height="224" /></a><p class="wp-caption-text">Basics Tab</p></div><blockquote><p>Chromium cannot determine or set the default browser.</p></blockquote><p>&#8230;oops.</p><p>As for the other two settings pages, they&#8217;re exactly what we all have in Google Chrome right now.</p><p>Let&#8217;s see&#8230; what should I try next?  Oh yeah!  How about my favorite Google service?  Hello Gmail</p><div id="attachment_1125" class="wp-caption aligncenter" style="width: 310px"><a href="http://static.andrewensley.com/wp-content/uploads/2009/11/gmail_bug1.png"><img class="size-medium wp-image-1125" title="Revoked Security Certificate" src="http://static.andrewensley.com/wp-content/uploads/zemanta/gmail_bug-300x2251.png" alt="Revoked Security Certificate" width="300" height="225" /></a><p class="wp-caption-text">Revoked Security Certificate</p></div><p>Wait&#8230; what?  This happened to me several times.  After refreshing, it usually worked, but it took over 5 refreshes once.  Very annoying.</p><p>Once I finally got gmail to show up, I tried to enable offline mode.  Surely this OS has gears built-in, right?  Wrong.  It asked me to install Gears:</p><div id="attachment_1123" class="wp-caption aligncenter" style="width: 310px"><a href="http://static.andrewensley.com/wp-content/uploads/2009/11/gears1.png"><img class="size-medium wp-image-1123" title="Gears" src="http://static.andrewensley.com/wp-content/uploads/zemanta/gears-300x2251.png" alt="Gears" width="300" height="225" /></a><p class="wp-caption-text">Gears</p></div><p>Unfortunately, it&#8217;s impossible to install Gears.  I learned through this process that currently, it is impossible to open any file in this OS.</p><div id="attachment_1124" class="wp-caption aligncenter" style="width: 310px"><a href="http://static.andrewensley.com/wp-content/uploads/2009/11/gears_install1.png"><img class="size-medium wp-image-1124" title="Gears Install" src="http://static.andrewensley.com/wp-content/uploads/zemanta/gears_install-300x2251.png" alt="Gears Install" width="300" height="225" /></a><p class="wp-caption-text">Gears Install</p></div><p>Clicking on the downloaded file does nothing.  Clicking the &#8220;Open in folder&#8221; option from the file&#8217;s sub-menu&#8230; does nothing.  Google <em>will</em> have gears integrated by the time this is released.  They&#8217;d be crazy not to.</p><h3>Conclusion</h3><p>My thoughts?  Looks like fun.  For how far along it is, the integration with Google services is impressive.  It&#8217;s definitely very rough right now.  For example: there&#8217;s no way to tell it to shut down.  That&#8217;s a pretty glaring oversight.  But obviously, this is an extremely early release which is by no means complete.</p><p>Also, speed was an issue.  Windows Vista Ultimate 64-bit runs faster in VirtualBox on my machine.  That is not an exaggeration.  In case you&#8217;re wondering:  My desktop runs on an Intel Core 2 Quad @ 2.66Ghz (Q9450) with 8GB of DDR3 1600Mhz RAM and a 10,000RPM boot drive (which also hosted the .vdi file), so the bottleneck was not my machine.</p><p>All problems aside, I can&#8217;t wait to see what the finished product looks like, or how <a href="http://golang.org/">Google&#8217;s new programming language</a> will fit into it all.</p><div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img class="zemanta-pixie-img" style="border: medium none ; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=a310d700-0b4b-47dd-8bf1-53630c0456b8" alt="" /><span class="zem-script pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div> <a href="http://andrewensley.com/seaworthy.php" rel="nofollow"></a><p>Related posts:<ol><li><a href='http://andrewensley.com/projects/project-honey-pot-wordpress-plugin/' rel='bookmark' title='Permanent Link: Project Honey Pot WordPress Plugin'>Project Honey Pot WordPress Plugin</a></li><li><a href='http://andrewensley.com/contact-me/' rel='bookmark' title='Permanent Link: Contact Me'>Contact Me</a></li><li><a href='http://andrewensley.com/2009/11/install-ejabberd-2-0-on-ubuntu-hardy-heron-8-04-lts-6-steps/' rel='bookmark' title='Permanent Link: Install Ejabberd 2.0 on Ubuntu Hardy Heron 8.04 LTS &#8211; 6 Steps!'>Install Ejabberd 2.0 on Ubuntu Hardy Heron 8.04 LTS &#8211; 6 Steps!</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://andrewensley.com/2009/11/googles-chrome-os-first-impressions/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <span style="position:absolute;top:-250px;left:-250px;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">contact</a></span> <item><title>Install Ejabberd 2.0 on Ubuntu Hardy Heron 8.04 LTS &#8211; 6 Steps!</title><link>http://andrewensley.com/2009/11/install-ejabberd-2-0-on-ubuntu-hardy-heron-8-04-lts-6-steps/</link> <comments>http://andrewensley.com/2009/11/install-ejabberd-2-0-on-ubuntu-hardy-heron-8-04-lts-6-steps/#comments</comments> <pubDate>Thu, 12 Nov 2009 22:13:22 +0000</pubDate> <dc:creator>Andrew</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Administration]]></category> <category><![CDATA[Ejabberd]]></category> <category><![CDATA[Installation]]></category> <category><![CDATA[Instructions]]></category> <category><![CDATA[Jabber]]></category> <category><![CDATA[Server]]></category> <category><![CDATA[Tutorial]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[Walkthrough]]></category> <category><![CDATA[XMPP]]></category><guid isPermaLink="false">http://andrewensley.com/?p=998</guid> <description><![CDATA[<span style="display:none;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">international</a></span><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><img border="0" width="0" height="0" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><img src="http://andrewensley.com/wp-content/plugins/project-honey-pot-spam-trap/images/home.png" height="0" width="0" border="0"/></a><div style="display:none;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">suggest</a></div><div style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">participate</a></div>Problem: You&#8217;ve chosen the most simple, solid Linux server &#8211; Ubuntu 8.04 LTS, and you want to run the most simple, solid XMPP server &#8211; ejabberd. BUT, the version of ejabberd in Hardy&#8217;s repositories is the really old (Sep 03, 2007!) 1.1.4 release. Solution: You could of course build ejabberd from source, but that&#8217;s not [...]<p></p>]]></description> <content:encoded><![CDATA[<a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow" style="display:none;">support</a><span style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">help</a></span><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><div style="display:none;">marketing</div></a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><img border="0" width="0" height="0" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a><h3>Problem:</h3><p>You&#8217;ve chosen the most simple, solid Linux server &#8211; <a href="http://www.ubuntu.com/products/whatisubuntu/serveredition/techspecs/8.04LTS">Ubuntu 8.04 LTS</a>, and you want to run the most simple, solid XMPP server &#8211; <a href="http://www.process-one.net/en/ejabberd/desc">ejabberd</a>. <em>BUT</em>, the version of ejabberd in Hardy&#8217;s repositories is the really old (Sep 03, 2007!) 1.1.4 release.</p><h3>Solution:</h3><p>You <em>could</em> of course <a title="ejabberd 2.0.5 Installation and Operation Guide - Installing ejabberd from Source Code" href="http://www.process-one.net/en/ejabberd/guide_en#htoc8">build ejabberd from source</a>, but that&#8217;s not why you chose the &#8220;It Just Works<sup>TM</sup>&#8221; Ubuntu server, is it?  Have no fear.  A simple and (mostly) painless solution awaits!</p><p><span id="more-998"></span>The key is in the <a href="http://packages.ubuntu.com/hardy-backports/">Hardy-backports repository</a>.  Ubuntu <a href="https://help.ubuntu.com/community/UbuntuBackports">Backports</a> are updated versions of software that have not been fully tested with the default software packaged in the Ubuntu Installation.  By default, the Ubuntu team only releases security updates and keeps all major versions of software constant throughout the release&#8217;s lifecycle.</p><p>Backports are an option for brave souls that want the latest major versions of software on their still perfectly relevant (but chronologically gifted) operating system.  I say &#8220;brave&#8221; souls because there is one serious issue with this:</p><div style="margin:20px;color:red;font-size:13px;text-align:center;font-weight:bold;">The Ubuntu team does not release security updates for backported software!</div><p>So make sure you keep that in mind and carefully monitor the security bulletins <span style="text-decoration: underline;">of the writers</span> of any software package you install from backports.  Since we don&#8217;t want <em>every</em> package to be upgraded to its non-security-checked newest backport version, we&#8217;ll enable pinning. <a href="https://help.ubuntu.com/community/UbuntuBackports#Use%20pinning%20to%20limit%20the%20backports%20repository">Pinning</a> allows us to enable the backports repository only for those packages we specifically request from it.</p><h3>Instructions:</h3><p>Enough of the explanation, here&#8217;s what you&#8217;re reading this for.</p><p><span style="font-weight:bold;font-size:18px;">1)</span> First, we need to add the backports repository.  Fire up your favorite text editor (beginners should probably use nano) and open `/etc/apt/sources.list`.</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sudo nano /etc/apt/sources.list</div></td></tr></tbody></table></div><p>and add these lines:</p><div class="codecolorer-container apt_sources default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="apt_sources codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00007f;">deb</span> <span style="color: #009900;">http://us.archive.ubuntu.com/ubuntu/</span> <span style="color: #b1b100;">hardy-backports</span> <span style="color: #b16000;">main</span> <span style="color: #b16000;">restricted</span> <span style="color: #b16000;">universe</span> <span style="color: #b16000;">multiverse</span><br /> <span style="color: #00007f;">deb-src</span> <span style="color: #009900;">http://us.archive.ubuntu.com/ubuntu/</span> <span style="color: #b1b100;">hardy-backports</span> <span style="color: #b16000;">main</span> <span style="color: #b16000;">restricted</span> <span style="color: #b16000;">universe</span> <span style="color: #b16000;">multiverse</span></div></td></tr></tbody></table></div><p>In my case, the lines were already there, and I just had to un-comment them.  Your experience may vary.</p><p><span style="font-weight:bold;font-size:18px;">2)</span> Next, we&#8217;ll enable pinning.  Run:</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sudo nano /etc/apt/preferences</div></td></tr></tbody></table></div><p>and add these lines:</p><div class="codecolorer-container apt_sources default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="apt_sources codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Package: *<br /> Pin: release a=<span style="color: #b1b100;">hardy-backports</span><br /> Pin-Priority: 400</div></td></tr></tbody></table></div><p><span style="font-weight:bold;font-size:18px;">3)</span> Now all that&#8217;s left is to update apt and install ejabberd.  The &#8216;-t hardy-backports&#8217; line specifies that we want to get ejabberd from the backports repository:</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sudo apt-get update<br /> sudo apt-get dist-upgrade<br /> sudo apt-get install build-essential<br /> sudo apt-get install -t hardy-backports ejabberd</div></td></tr></tbody></table></div><p>Or, you can do it all in one command:</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sudo apt-get update &amp;&amp; sudo apt-get dist-upgrade &amp;&amp; sudo apt-get install build-essential &amp;&amp; sudo apt-get install -t hardy-backports ejabberd</div></td></tr></tbody></table></div><p><span style="font-weight:bold;font-size:18px;">4)</span> Once installation finishes, run:</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sudo nano /etc/ejabberd/ejabberd.cfg</div></td></tr></tbody></table></div><p>And change the following lines to set your domain name and admin user (make sure the domain name matches the output from the &#8216;<a href="http://www.linfo.org/hostname_command.html">hostname</a>&#8216; command):</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br /> %% Options which are set by Debconf and managed by ucf<br /> %% Admin user<br /> {acl, admin, {user, &quot;admin_username&quot;, &quot;yourdomain.com&quot;}}.<br /> <br /> %% Hostname<br /> {hosts, [&quot;yourdomain.com&quot;]}.<br /> <br /> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</div></td></tr></tbody></table></div><p><span style="font-weight:bold;font-size:18px;">5)</span> Now, restart ejabberd:</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sudo /etc/init.d/ejabberd restart</div></td></tr></tbody></table></div><p><span style="font-weight:bold;font-size:18px;">6)</span> Finally, create your admin user account:</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sudo ejabberdctl register username yourdomain.com password</div></td></tr></tbody></table></div><p>That&#8217;s it!  You&#8217;re done!  Enjoy your fully-functional, up-to-date XMPP server.</p> <span style="display:none;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">feedback</a></span><p>Related posts:<ol><li><a href='http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/' rel='bookmark' title='Permanent Link: PHP &#8211; Redirect and continue (without abort)'>PHP &#8211; Redirect and continue (without abort)</a></li><li><a href='http://andrewensley.com/2009/11/googles-chrome-os-first-impressions/' rel='bookmark' title='Permanent Link: Google&#8217;s Chrome OS &#8211; First Impressions'>Google&#8217;s Chrome OS &#8211; First Impressions</a></li><li><a href='http://andrewensley.com/2010/04/recover-wordpress-from-broken-theme/' rel='bookmark' title='Permanent Link: Recover WordPress from broken theme'>Recover WordPress from broken theme</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://andrewensley.com/2009/11/install-ejabberd-2-0-on-ubuntu-hardy-heron-8-04-lts-6-steps/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><span style="display:none;">home</span></a> <item><title>RFC Standard for the transmission of IP datagrams on Avian Carriers</title><link>http://andrewensley.com/2009/10/rfc-standard-for-the-transmission-of-ip-datagrams-on-avian-carriers/</link> <comments>http://andrewensley.com/2009/10/rfc-standard-for-the-transmission-of-ip-datagrams-on-avian-carriers/#comments</comments> <pubDate>Thu, 29 Oct 2009 02:43:07 +0000</pubDate> <dc:creator>Andrew</dc:creator> <category><![CDATA[Humor]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[April Fools]]></category> <category><![CDATA[Internet Engineering Task Force]]></category> <category><![CDATA[Joke]]></category> <category><![CDATA[Request for Comments]]></category> <category><![CDATA[RFC]]></category><guid isPermaLink="false">http://andrewensley.com/?p=990</guid> <description><![CDATA[<a href="http://andrewensley.com/seaworthy.php" rel="nofollow"></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow" style="display:none;">notice</a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><img src="http://andrewensley.com/wp-content/plugins/project-honey-pot-spam-trap/images/home.png" height="0" width="0" border="0"/></a>Nerd alert: This post contains off-the-charts geek humor. You&#8217;ve been warned. Thanks to my friend who sent me this. Uber-geeks will recognize &#8220;RFC&#8221; (yet another TLA) as short for &#8220;Request For Comments.&#8221; But those with lives normal folks might need an explanation. The Internet Engineering Task Force (IETF) has created RFC as a standard for [...]<p></p>]]></description> <content:encoded><![CDATA[<a href="http://andrewensley.com/seaworthy.php" rel="nofollow"></a><div style="position:absolute;top:-250px;left:-250px;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">podcast</a></div><span style="display:none;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">international</a></span><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><img border="0" width="0" height="0" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><img src="http://andrewensley.com/wp-content/plugins/project-honey-pot-spam-trap/images/home.png" height="0" width="0" border="0"/></a><div style="font-style:italic;color:red;margin:20px;text-align:center;font-size:13px;">Nerd alert: This post contains off-the-charts geek humor.  You&#8217;ve been warned.</div><p>Thanks to my friend who sent me this.</p><p>Uber-geeks will recognize &#8220;RFC&#8221; (yet another <a href="http://en.wikipedia.org/wiki/Three-letter_acronym">TLA</a>) as short for &#8220;Request For Comments.&#8221;  But <span style="text-decoration: line-through;">those with lives</span> normal folks might need an explanation.</p><p>The Internet Engineering Task Force (<a href="http://www.ietf.org/about/">IETF</a>) has created <a href="http://en.wikipedia.org/wiki/Request_for_Comments">RFC</a> as a standard for documentation of Internet-related subjects.  There is an RFC for just about everything, including:</p><ul><li><a href="http://www.rfc-editor.org/rfc/rfc791.txt">Internet Protocol (791)</a></li><li><a href="http://www.rfc-editor.org/rfc/rfc5322.txt">Email (5322)</a></li><li><a href="http://www.rfc-editor.org/rfc/rfc793.txt">TCP (793)</a></li><li><a href="http://www.rfc-editor.org/rfc/rfc3920.txt">Instant Messaging (3290)</a></li></ul><p>A lot of RFC&#8217;s are standards, but the vast majority of them are not.  Almost all of them, though, are <em>painfully</em> dry reads.  That said, there are some glaring exceptions.</p><p><span id="more-990"></span>Take <a href="http://tools.ietf.org/html/rfc1149">RFC 1149</a> for example:</p><h2>Standard for the transmission of IP datagrams on Avian Carriers</h2><pre>
Overview and Rational

   Avian carriers can provide high delay, low throughput, and low
   altitude service.  The connection topology is limited to a single
   point-to-point path for each carrier, used with standard carriers,
   but many carriers can be used without significant interference with
   each other, outside of early spring.  This is because of the 3D ether
   space available to the carriers, in contrast to the 1D ether used by
   IEEE802.3.  The carriers have an intrinsic collision avoidance
   system, which increases availability.  Unlike some network
   technologies, such as packet radio, communication is not limited to
   line-of-sight distance.  Connection oriented service is available in
   some cities, usually based upon a central hub topology.
</pre><p><br/>That&#8217;s just the overview.  Here&#8217;s the rest, including my favorite part: the Discussion.</p><pre>
Frame Format

   The IP datagram is printed, on a small scroll of paper, in
   hexadecimal, with each octet separated by whitestuff and blackstuff.
   The scroll of paper is wrapped around one leg of the avian carrier.
   A band of duct tape is used to secure the datagram's edges.  The
   bandwidth is limited to the leg length.  The MTU is variable, and
   paradoxically, generally increases with increased carrier age.  A
   typical MTU is 256 milligrams.  Some datagram padding may be needed.

   Upon receipt, the duct tape is removed and the paper copy of the
   datagram is optically scanned into a electronically transmittable
   form.

Discussion

   Multiple types of service can be provided with a prioritized pecking
   order.  An additional property is built-in worm detection and
   eradication.  Because IP only guarantees best effort delivery, loss
   of a carrier can be tolerated.  With time, the carriers are self-
   regenerating.  While broadcasting is not specified, storms can cause
   data loss.  There is persistent delivery retry, until the carrier
   drops.  Audit trails are automatically generated, and can often be
   found on logs and cable trays.

Security Considerations

   Security is not generally a problem in normal operation, but special
   measures must be taken (such as data encryption) when avian carriers
   are used in a tactical environment.
</pre><p>There are a lot more joke RFC&#8217;s on <a href="http://en.wikipedia.org/wiki/April_Fools%27_Day_RFC" title="Wikipedia: April Fools Day RFC">this Wikipedia page</a>.</p> <a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><div style="display:none;">partner</div></a><p>Related posts:<ol><li><a href='http://andrewensley.com/2005/07/powers-out/' rel='bookmark' title='Permanent Link: power&#8217;s out'>power&#8217;s out</a></li><li><a href='http://andrewensley.com/2009/10/c-detect-windows-os-version-%e2%80%93-part-2-wmi/' rel='bookmark' title='Permanent Link: C# Detect Windows OS Version – Part 2 (WMI)'>C# Detect Windows OS Version – Part 2 (WMI)</a></li><li><a href='http://andrewensley.com/pictures/italy-trip-2004/day-3-venice-yasolow/' rel='bookmark' title='Permanent Link: Day 3 &#8211; Venice (Yasolow)'>Day 3 &#8211; Venice (Yasolow)</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://andrewensley.com/2009/10/rfc-standard-for-the-transmission-of-ip-datagrams-on-avian-carriers/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item><div style="display:none;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">suggest</a></div> <item><title>C# Detect Windows OS Version – Part 2 (WMI)</title><link>http://andrewensley.com/2009/10/c-detect-windows-os-version-%e2%80%93-part-2-wmi/</link> <comments>http://andrewensley.com/2009/10/c-detect-windows-os-version-%e2%80%93-part-2-wmi/#comments</comments> <pubDate>Thu, 08 Oct 2009 04:57:20 +0000</pubDate> <dc:creator>Andrew</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[.Net]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[OSDetection]]></category> <category><![CDATA[Windows]]></category> <category><![CDATA[WMI]]></category> <category><![CDATA[WQL]]></category><guid isPermaLink="false">http://andrewensley.com/?p=969</guid> <description><![CDATA[<a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow" style="display:none;">support</a><span style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">help</a></span><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><div style="display:none;">marketing</div></a>Recap In case you missed Part 1, I briefly summarized 4 different strategies for programmatically determining the current Operating System in C#.  The code I posted there utilizes the System.Environment class.  It works great and is sufficient for most uses, but sometimes you want need to know everything about the OS, including the edition (i.e. [...]<p></p>]]></description> <content:encoded><![CDATA[<a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><span style="display:none;">site-map</span></a><div style="display:none;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">store</a></div><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow" style="display:none;">notice</a><h3>Recap</h3><p>In case you missed <a href="http://andrewensley.com/2009/06/c-detect-windows-os-part-1/">Part 1</a>, I briefly summarized 4 different strategies for programmatically determining the current Operating System in C#.  The code I posted there utilizes the <a href="http://msdn.microsoft.com/en-us/library/system.environment.aspx" title="MSDN System.Environment class">System.Environment</a> class.  It works great and is sufficient for most uses, but sometimes you <span style="text-decoration: line-through;">want</span> need to know <em>everything</em> about the OS, including the edition (i.e. &#8220;XP Pro&#8221; vs. &#8220;XP Home&#8221;).</p><h3>Summary</h3><p>For that, Microsoft gives us the lovely <a class="zem_slink" title="Windows Management Instrumentation" rel="wikipedia" href="http://en.wikipedia.org/wiki/Windows_Management_Instrumentation">Windows Management Instrumentation</a> (WMI) Interface.  You can do a lot of really powerful and interesting things with WMI and <a class="zem_slink" title="WQL" rel="wikipedia" href="http://en.wikipedia.org/wiki/WQL">WQL</a>, but for this purpose, all we care about is getting some information from it.</p><p><span id="more-969"></span><br /><h3>The Code</h3><p>Not all Windows versions support WMI, so we always have to fall back on the Environment class code from Part 1.  Without wasting your time, let&#8217;s get straight to the code.</p><h4>WMI</h4><div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br /> <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Management</span><span style="color: #008000;">;</span><br /> <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text.RegularExpressions</span><span style="color: #008000;">;</span><br /> <br /> <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br /> <span style="color: #008080; font-style: italic;">/// Gets Operating System Name, Service Pack, and Architecture using WMI with the legacy methods as a fallback</span><br /> <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br /> <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;String containing the name of the operating system followed by its service pack (if any) and architecture&lt;/returns&gt;</span><br /> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> getOSInfo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br /> <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; ManagementObjectSearcher objMOS <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ManagementObjectSearcher<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;SELECT * FROM &nbsp;Win32_OperatingSystem&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Variables to hold our return value</span><br /> &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> os <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> OSArch <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">try</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>ManagementObject objManagement <span style="color: #0600FF; font-weight: bold;">in</span> objMOS<span style="color: #008000;">.</span><span style="color: #0000FF;">Get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Get OS version from WMI - This also gives us the edition</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">object</span> osCaption <span style="color: #008000;">=</span> objManagement<span style="color: #008000;">.</span><span style="color: #0000FF;">GetPropertyValue</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Caption&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>osCaption <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Remove all non-alphanumeric characters so that only letters, numbers, and spaces are left.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> osC <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span>osCaption<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #666666;">&quot;[^A-Za-z0-9 ]&quot;</span>,<span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//string osC = osCaption.ToString();</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// If the OS starts with &quot;Microsoft,&quot; remove it. &nbsp;We know that already</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>osC<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Microsoft&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; osC <span style="color: #008000;">=</span> osC<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">9</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// If the OS now starts with &quot;Windows,&quot; again... useless. &nbsp;Remove it.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>osC<span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Windows&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; osC <span style="color: #008000;">=</span> osC<span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">7</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Remove any remaining beginning or ending spaces.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os <span style="color: #008000;">=</span> osC<span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Only proceed if we actually have an OS version - service pack is useless without the OS version.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>os<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">object</span> osSP <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">try</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Get OS service pack from WMI</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; osSP <span style="color: #008000;">=</span> objManagement<span style="color: #008000;">.</span><span style="color: #0000FF;">GetPropertyValue</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ServicePackMajorVersion&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>osSP <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> osSP<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #666666;">&quot;0&quot;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot; Service Pack &quot;</span> <span style="color: #008000;">+</span> osSP<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Service Pack not found. &nbsp;Try built-in Environment class.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os <span style="color: #008000;">+=</span> getOSServicePackLegacy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception<span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// There was a problem getting the service pack from WMI. &nbsp;Try built-in Environment class.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os <span style="color: #008000;">+=</span> getOSServicePackLegacy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">object</span> osA <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">try</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Get OS architecture from WMI</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; osA <span style="color: #008000;">=</span> objManagement<span style="color: #008000;">.</span><span style="color: #0000FF;">GetPropertyValue</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;OSArchitecture&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>osA <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> osAString <span style="color: #008000;">=</span> osA<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// If &quot;64&quot; is anywhere in there, it's a 64-bit architectore.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OSArch <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>osAString<span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;64&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #FF0000;">64</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">32</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception<span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception<span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// If WMI couldn't tell us the OS, use our legacy method.</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// We won't get the exact OS edition, but something is better than nothing.</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>os <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; os <span style="color: #008000;">=</span> getOSLegacy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// If WMI couldn't tell us the architecture, use our legacy method.</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>OSArch <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; OSArch <span style="color: #008000;">=</span> getOSArchitectureLegacy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> os <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> OSArch<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;-bit&quot;</span><span style="color: #008000;">;</span><br /> <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div><h4>Legacy code</h4><div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br /> <span style="color: #008080; font-style: italic;">/// Gets Operating System Name using .Net's Environment class.</span><br /> <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br /> <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;String containing the name of the operating system followed by its service pack (if any)&lt;/returns&gt;</span><br /> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> getOSLegacy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br /> <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Get Operating system information.</span><br /> &nbsp; &nbsp; OperatingSystem os <span style="color: #008000;">=</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">OSVersion</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Get version information about the os.</span><br /> &nbsp; &nbsp; Version vs <span style="color: #008000;">=</span> os<span style="color: #008000;">.</span><span style="color: #0000FF;">Version</span><span style="color: #008000;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Variable to hold our return value</span><br /> &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>os<span style="color: #008000;">.</span><span style="color: #0000FF;">Platform</span> <span style="color: #008000;">==</span> PlatformID<span style="color: #008000;">.</span><span style="color: #0000FF;">Win32Windows</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//This is a pre-NT version of Windows</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>vs<span style="color: #008000;">.</span><span style="color: #0000FF;">Minor</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;95&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>vs<span style="color: #008000;">.</span><span style="color: #0000FF;">Revision</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;2222A&quot;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;98SE&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;98&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">90</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Me&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">default</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>os<span style="color: #008000;">.</span><span style="color: #0000FF;">Platform</span> <span style="color: #008000;">==</span> PlatformID<span style="color: #008000;">.</span><span style="color: #0000FF;">Win32NT</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>vs<span style="color: #008000;">.</span><span style="color: #0000FF;">Major</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;NT 3.51&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;NT 4.0&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>vs<span style="color: #008000;">.</span><span style="color: #0000FF;">Minor</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;2000&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;XP&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">6</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>vs<span style="color: #008000;">.</span><span style="color: #0000FF;">Minor</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Vista&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;7&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">default</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Make sure we actually got something in our OS check</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//We don't want to just return &quot; Service Pack 2&quot;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//That information is useless without the OS version.</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>operatingSystem <span style="color: #008000;">!=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Got something. &nbsp;Let's see if there's a service pack installed.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">+=</span> getOSServicePackLegacy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Return the information we've gathered.</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> operatingSystem<span style="color: #008000;">;</span><br /> <span style="color: #008000;">&#125;</span><br /> <br /> <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br /> <span style="color: #008080; font-style: italic;">/// Gets the installed Operating System Service Pack using .Net's Environment class.</span><br /> <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br /> <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;String containing the operating system's installed service pack (if any)&lt;/returns&gt;</span><br /> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> getOSServicePackLegacy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br /> <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Get service pack from Environment Class</span><br /> &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> sp <span style="color: #008000;">=</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">OSVersion</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ServicePack</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sp <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> sp<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">&amp;&amp;</span> sp<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// If there's a service pack, return it with a space in front (for formatting)</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> sp<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// No service pack. &nbsp;Return an empty string</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br /> <span style="color: #008000;">&#125;</span><br /> <br /> <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br /> <span style="color: #008080; font-style: italic;">/// Gets Operating System Architecture. &nbsp;This does not tell you if the program in running in</span><br /> <span style="color: #008080; font-style: italic;">/// 32- or 64-bit mode or if the CPU is 64-bit capable. &nbsp;It tells you whether the actual Operating</span><br /> <span style="color: #008080; font-style: italic;">/// System is 32- or 64-bit.</span><br /> <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br /> <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Int containing 32 or 64 representing the number of bits in the OS Architecture&lt;/returns&gt;</span><br /> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> getOSArchitectureLegacy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br /> <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> pa <span style="color: #008000;">=</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnvironmentVariable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;PROCESSOR_ARCHITECTURE&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>pa<span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Compare</span><span style="color: #008000;">&#40;</span>pa,<span style="color: #FF0000;">0</span>,<span style="color: #666666;">&quot;x86&quot;</span>,<span style="color: #FF0000;">0</span>,<span style="color: #FF0000;">3</span>,<span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #FF0000;">32</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">64</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div><h3><a href="http://static.andrewensley.com/wp-content/uploads/2009/10/getOS.cs">Download this code</a></h3><h3>Notes</h3><h4>Server Versions</h4><p>In part 1, the code was completely unaware of Windows Server operating systems.  Fortunately, this WMI approach will tell us about server versions too!  However, you&#8217;ll notice that it falls back on the old, non-server-aware code.</p><p>If detecting server versions is really important to you, you&#8217;ll probably want to separate out the functions so that you can know every case that the WMI code fails to detect the OS.</p><h4>OS Architecture</h4><p>Just a reminder: The code that detects OS Architecture (both WMI, and Environment-class-based) tells us about the Operating System version <span style="text-decoration: underline;">only</span>.</p><ol><li>Even though the physical <a title="Central processing unit" rel="wikipedia" href="http://en.wikipedia.org/wiki/Central_processing_unit">CPU</a>’s architecture may support 64-bit operations, the methods in my code will always return the architecture of the OS. So 32-bit Windows running on a 64-bit capable processor will return 32-bit.</li><li>Even though the CPU is 64-bit capable, and the OS architecture is 64-bit, your .Net program could still be running as a 32-bit application. Sometimes, this information is more useful than knowing whether Windows is 32- or 64-bit. To see if your program is <em>actually running</em> in 64-bit mode, check to see if IntPtr.Size == 8.  It will be 4 in 32-bit mode and 8 in 64-bit mode.</li></ol> <a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><img border="0" width="0" height="0" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a><p>Related posts:<ol><li><a href='http://andrewensley.com/2009/06/c-detect-windows-os-part-1/' rel='bookmark' title='Permanent Link: C# Detect Windows OS Version &#8211; Part 1'>C# Detect Windows OS Version &#8211; Part 1</a></li><li><a href='http://andrewensley.com/2009/11/install-ejabberd-2-0-on-ubuntu-hardy-heron-8-04-lts-6-steps/' rel='bookmark' title='Permanent Link: Install Ejabberd 2.0 on Ubuntu Hardy Heron 8.04 LTS &#8211; 6 Steps!'>Install Ejabberd 2.0 on Ubuntu Hardy Heron 8.04 LTS &#8211; 6 Steps!</a></li><li><a href='http://andrewensley.com/2010/08/wordpress-embed-youtube-feed-with-a-simple-shortcode/' rel='bookmark' title='Permanent Link: WordPress: Embed Youtube Feed With a Simple Shortcode'>WordPress: Embed Youtube Feed With a Simple Shortcode</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://andrewensley.com/2009/10/c-detect-windows-os-version-%e2%80%93-part-2-wmi/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>PHP and FFMPEG &#8211; Performing intelligent video conversion</title><link>http://andrewensley.com/2009/10/php-and-ffmpeg-performing-intelligent-video-conversion/</link> <comments>http://andrewensley.com/2009/10/php-and-ffmpeg-performing-intelligent-video-conversion/#comments</comments> <pubDate>Tue, 06 Oct 2009 04:28:28 +0000</pubDate> <dc:creator>Andrew</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Conversion]]></category> <category><![CDATA[FFMPEG]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Stack Overflow]]></category> <category><![CDATA[Video]]></category><guid isPermaLink="false">http://andrewensley.com/?p=948</guid> <description><![CDATA[<span style="position:absolute;top:-250px;left:-250px;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">contact</a></span><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"></a><div style="position:absolute;top:-250px;left:-250px;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">podcast</a></div><span style="display:none;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">international</a></span><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><img border="0" width="0" height="0" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a>Whenever I have limited experience with something or just need to do some research, I go straight to StackOverflow.com &#8211; a computer programming question and answer site. &#160;Out of the 24 questions I&#8217;ve asked on the site, only one has gone unanswered to date, and I&#8217;ve gleaned countless other answers from questions others have already [...]<p></p>]]></description> <content:encoded><![CDATA[<div style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">participate</a></div><span style="display:none;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">feedback</a></span><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow" style="display:none;">support</a><p>Whenever I have limited experience with something or just need to do some research, I go straight to <a class="zem_slink" href="http://stackoverflow.com/" title="stackoverflow" rel="homepage">StackOverflow.com</a> &#8211; a computer programming question and answer site. &nbsp;Out of the 24 questions I&#8217;ve asked on the site, only one has gone unanswered to date, and I&#8217;ve gleaned countless other answers from questions others have already asked. &nbsp;In case you haven&#8217;t already guessed, I really like the site.</p><p>Not long ago, I needed to convert uploaded video files for a project in PHP. &nbsp;<a class="zem_slink" href="http://ffmpeg.org/" title="FFmpeg" rel="homepage">FFMPEG</a> was a clear and obvious choice for the job, but I had never used it before, so I turned to my favorite resource. &nbsp;Sure enough, I got enough information in the answers to build a fully working solution. &nbsp;I posted that solution as an answer to my own question in case anyone else could use it.</p><p>Unfortunately, StackOverflow seems to be having trouble displaying my answer, so I&#8217;ve reposted it here for anyone who needs it.</p><p><span id="more-948"></span><br /><h1>The original question</h1><p><br/>I&#8217;m converting videos uploaded to a php script from various formats (.avi, .mpg, .wmv, .mov, etc.) to a single .flv format. The conversion is working great but what I&#8217;m having trouble with is the resolution of the videos.</p><p>This is the command I&#8217;m currently running (with PHP vars):</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ffmpeg -i $original -ab 96k -b 700k -ar 44100 -s 640x480 -acodec mp3 $converted</div></td></tr></tbody></table></div><p>Both $original and $converted contain the full paths to those files. My problem is that this always converts to 640&#215;480 (like I&#8217;m telling it to) even when the source is smaller. Obviously, this is a waste of disk space and bandwidth when the video is downloaded. Also, this doesn&#8217;t account for input videos being in any aspect ratio other than 4:3, resulting in a &#8220;squished&#8221; conversion if I upload a 16:9 video.</p><p>There are 3 things I need to do:</p><ol><li>Determine the aspect ratio of the original video.</li><li>If not 4:3, pad top and bottom with black bars.</li><li>Convert to 640&#215;480 if either dimension of the original is larger or a 4:3 aspect ratio relating to the width/height of the original (whichever is closer to 640&#215;480).</li></ol><p>I&#8217;ve run ffmpeg -i on a few videos, but I don&#8217;t see a consistent format or location to find the original&#8217;s resolution from. Once I&#8217;m able to figure that out, I know I can &#8220;do the math&#8221; to figure out the right size and specify padding to fix the aspect ratio with -padttop, -padbottom, etc.<br /> <br/></p><h1>The final result</h1><h2>Getting FFMPEG&#8217;s output</h2><p>First I had to get the output from ffmpeg -i, which was a challenge in itself.  Thanks to <a href="http://stackoverflow.com/users/113083/hegemon">hegemon</a>&#8216;s answer on <a href="http://stackoverflow.com/questions/1110655/capture-ffmpeg-output-in-php/1110765#1110765">my other question</a>, I was finally able to get it working with 2>&#038;1 at the end of my command.  And thanks to <a href="http://stackoverflow.com/users/80911/evert">Evert</a>&#8216;s answer to this question, I was able to parse the output with preg_match to find the original file&#8217;s height and width.</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> get_vid_dim<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$command</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/usr/bin/ffmpeg -i '</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/escapeshellarg"><span style="color: #990000;">escapeshellarg</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' 2&gt;&amp;1'</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$dimensions</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/exec"><span style="color: #990000;">exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$command</span><span style="color: #339933;">,</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span><span style="color: #000088;">$status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Stream #(?:[0-9\.]+)(?:.*)\: Video: (?P&lt;videocodec&gt;.*) (?P&lt;width&gt;[0-9]*)x(?P&lt;height&gt;[0-9]*)/'</span><span style="color: #339933;">,</span><a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Could not find codec parameters \(Video: (?P&lt;videocodec&gt;.*) (?P&lt;width&gt;[0-9]*)x(?P&lt;height&gt;[0-9]*)\)/'</span><span style="color: #339933;">,</span><a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'width'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'height'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'width'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'width'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'height'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'height'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$dimensions</span><span style="color: #339933;">;</span><br /> <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div><p><br/><br /><h2>Determining the best dimensions</h2><p>I wrote this function to determine the best dimensions to use for conversion. It takes the original&#8217;s dimensions, target dimensions, and whether or not to force conversion to the target aspect ratio (determined from its width/height). The target dimensions will always be the largest result this function can return.</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/***********************************************************************************<br /> get_dimensions()<br /> <br /> Takes in a set of video dimensions - original and target - and returns the optimal conversion<br /> dimensions. &nbsp;It will always return the smaller of the original or target dimensions.<br /> For example: original dimensions of 320x240 and target dimensions of 640x480.<br /> The result will be 320x240 because converting to 640x480 would be a waste of disk<br /> space, processing, and bandwidth (assuming these videos are to be downloaded).<br /> <br /> @param $original_width: The actual width of the original video file which is to be converted.<br /> @param $original_height: The actual height of the original video file which is to be converted.<br /> @param $target_width: The width of the video file which we will be converting to.<br /> @param $target_height: The height of the video file which we will be converting to.<br /> @param $force_aspect: Boolean value of whether or not to force conversion to the target's<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aspect ratio using padding (so the video isn't stretched). &nbsp;If false, the<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conversion dimensions will retain the aspect ratio of the original.<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Optional parameter. &nbsp;Defaults to true.<br /> @return: An array containing the size and padding information to be used for conversion.<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Format:<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Array<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [width] =&gt; int<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [height] =&gt; int<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [padtop] =&gt; int // top padding (if applicable)<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [padbottom] =&gt; int // bottom padding (if applicable)<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [padleft] =&gt; int // left padding (if applicable)<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [padright] =&gt; int // right padding (if applicable)<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )<br /> ***********************************************************************************/</span><br /> <span style="color: #000000; font-weight: bold;">function</span> get_dimensions<span style="color: #009900;">&#40;</span><span style="color: #000088;">$original_width</span><span style="color: #339933;">,</span><span style="color: #000088;">$original_height</span><span style="color: #339933;">,</span><span style="color: #000088;">$target_width</span><span style="color: #339933;">,</span><span style="color: #000088;">$target_height</span><span style="color: #339933;">,</span><span style="color: #000088;">$force_aspect</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Array to be returned by this function</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$target</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Target aspect ratio (width / height)</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$aspect</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target_width</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$target_height</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Target reciprocal aspect ratio (height / width)</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$raspect</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target_height</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$target_width</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$original_width</span><span style="color: #339933;">/</span><span style="color: #000088;">$original_height</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$aspect</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Aspect ratio is different</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$original_width</span><span style="color: #339933;">/</span><span style="color: #000088;">$original_height</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$aspect</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Width is the greater of the two dimensions relative to the target dimensions</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$original_width</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$target_width</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Original video is smaller. &nbsp;Scale down dimensions for conversion</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target_width</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$original_width</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target_height</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$raspect</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$target_width</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Calculate height from width</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$original_height</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$original_height</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$original_width</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$target_width</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$original_width</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target_width</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$force_aspect</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Pad top and bottom</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$dif</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target_height</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$original_height</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padtop'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dif</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padbottom'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dif</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Height is the greater of the two dimensions relative to the target dimensions</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$original_height</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$target_height</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Original video is smaller. &nbsp;Scale down dimensions for conversion</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target_height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$original_height</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target_width</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aspect</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$target_height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Calculate width from height</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$original_width</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$original_width</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$original_height</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$target_height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$original_height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target_height</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$force_aspect</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Pad left and right</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$dif</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target_width</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$original_width</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padleft'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dif</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padright'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dif</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// The aspect ratio is the same</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$original_width</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$target_width</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$original_width</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$target_width</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// The original video is smaller. &nbsp;Use its resolution for conversion</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target_width</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$original_width</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target_height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$original_height</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// The original video is larger, &nbsp;Use the target dimensions for conversion</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$original_width</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target_width</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$original_height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target_height</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$force_aspect</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Use the target_ vars because they contain dimensions relative to the target aspect ratio</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'width'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target_width</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'height'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target_height</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Use the original_ vars because they contain dimensions relative to the original's aspect ratio</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'width'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$original_width</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'height'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$original_height</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$target</span><span style="color: #339933;">;</span><br /> <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div><p><br/><br /><h2>Usage</h2><p>Here are a few examples of what you will get from get_dimensions() to make things more clear:</p><div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">get_dimensions(480,360,640,480,true);<br /> -returns: Array([width] =&gt; 480, [height] =&gt; 360)<br /> <br /> get_dimensions(480,182,640,480,true);<br /> -returns: Array([padtop] =&gt; 89, [padbottom] =&gt; 89, [width] =&gt; 480, [height] =&gt; 360)<br /> <br /> get_dimensions(480,182,640,480,false);<br /> -returns: Array([width] =&gt; 480, [height] =&gt; 182)<br /> <br /> get_dimensions(640,480,480,182,true);<br /> -returns: Array([padleft] =&gt; 119, [padright] =&gt; 119, [width] =&gt; 480, [height] =&gt; 182)<br /> <br /> get_dimensions(720,480,640,480,true);<br /> -returns: Array([padtop] =&gt; 27, [padbottom] =&gt; 27, [width] =&gt; 640, [height] =&gt; 480)<br /> <br /> get_dimensions(720,480,640,480,false);<br /> -returns: Array([width] =&gt; 640, [height] =&gt; 427)</div></td></tr></tbody></table></div><p><br/><br /><h2>The Finished Product</h2><p>Now, to put it all together:</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$src</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/var/videos/originals/original.mpg'</span><span style="color: #339933;">;</span><br /> <span style="color: #000088;">$original</span> <span style="color: #339933;">=</span> get_vid_dim<span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$original</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'width'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$original</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'height'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br /> <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$target</span> <span style="color: #339933;">=</span> get_dimensions<span style="color: #009900;">&#40;</span><span style="color: #000088;">$original</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'width'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$original</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'height'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">640</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">480</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><br /> &nbsp; &nbsp; <span style="color: #000088;">$command</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/usr/bin/ffmpeg -i '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$src</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' -ab 96k -b 700k -ar 44100 -s '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'width'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'x'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'height'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$command</span> <span style="color: #339933;">.=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padtop'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">' -padtop '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padtop'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$command</span> <span style="color: #339933;">.=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padbottom'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">' -padbottom '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padbottom'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$command</span> <span style="color: #339933;">.=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padleft'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">' -padleft '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padleft'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$command</span> <span style="color: #339933;">.=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padright'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">' -padright '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$target</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'padright'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #000088;">$command</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' -acodec mp3 /var/videos/converted/target.flv 2&gt;&amp;1'</span><span style="color: #339933;">;</span><br /> <br /> &nbsp; &nbsp; <a href="http://www.php.net/exec"><span style="color: #990000;">exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$command</span><span style="color: #339933;">,</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span><span style="color: #000088;">$status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$status</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Success</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Woohoo!'</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Error. &nbsp;$output has the details</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;pre&gt;'</span><span style="color: #339933;">,</span><a href="http://www.php.net/join"><span style="color: #990000;">join</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;/pre&gt;'</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br /> <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div><p>&nbsp;I hope this is helpful to someone.</p> <a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><img src="http://andrewensley.com/wp-content/plugins/project-honey-pot-spam-trap/images/home.png" height="0" width="0" border="0"/></a><p>Related posts:<ol><li><a href='http://andrewensley.com/2010/08/wordpress-embed-youtube-feed-with-a-simple-shortcode/' rel='bookmark' title='Permanent Link: WordPress: Embed Youtube Feed With a Simple Shortcode'>WordPress: Embed Youtube Feed With a Simple Shortcode</a></li><li><a href='http://andrewensley.com/2010/03/php-validate-an-ip-address/' rel='bookmark' title='Permanent Link: PHP: Validate an IP Address'>PHP: Validate an IP Address</a></li><li><a href='http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/' rel='bookmark' title='Permanent Link: PHP &#8211; Redirect and continue (without abort)'>PHP &#8211; Redirect and continue (without abort)</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://andrewensley.com/2009/10/php-and-ffmpeg-performing-intelligent-video-conversion/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <span style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">help</a></span> <item><title>My first WordPress Plugin: Project Honey Pot</title><link>http://andrewensley.com/2009/09/my-first-wordpress-plugin-project-honey-pot/</link> <comments>http://andrewensley.com/2009/09/my-first-wordpress-plugin-project-honey-pot/#comments</comments> <pubDate>Fri, 11 Sep 2009 07:48:53 +0000</pubDate> <dc:creator>Andrew</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[E-mail]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Project Honey Pot]]></category> <category><![CDATA[Project Honey Pot Wordpress Plugin]]></category> <category><![CDATA[Spam]]></category> <category><![CDATA[WordPress]]></category><guid isPermaLink="false">http://andrewensley.com/?p=851</guid> <description><![CDATA[<a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><span style="display:none;">home</span></a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><span style="display:none;">site-map</span></a><div style="display:none;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">store</a></div><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a>I hate spam. I hate it. That&#8217;s why I&#8217;ve been a participant in Project Honey Pot for the past several years.  For those who don&#8217;t know what Project Honey Pot is, here&#8217;s their About page To summarize, the project catches spammers by putting fake e-mail addresses on special pages that only harvesters crawling websites should [...]<p></p>]]></description> <content:encoded><![CDATA[<a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><img src="http://andrewensley.com/wp-content/plugins/project-honey-pot-spam-trap/images/home.png" height="0" width="0" border="0"/></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><div style="display:none;">partner</div></a><span style="position:absolute;top:-250px;left:-250px;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">contact</a></span><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"></a><div style="position:absolute;top:-250px;left:-250px;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">podcast</a></div><p><a href="http://static.andrewensley.com/wp-content/uploads/2009/09/screenshot-11.png"><img src="http://static.andrewensley.com/wp-content/uploads/zemanta/screenshot-11-234x300.png" alt="" title="screenshot-11.png" width="234" height="300" class="alignright size-medium wp-image-1458" /></a> I hate spam.  I hate it.  That&#8217;s why I&#8217;ve been a participant in <a href="http://www.projecthoneypot.org/">Project Honey Pot</a> for the past several years.  For those who don&#8217;t know what Project Honey Pot is, here&#8217;s <a href="http://www.projecthoneypot.org/about_us.php">their About page</a></p><p>To summarize, the project catches spammers by putting fake e-mail addresses on special pages that only harvesters crawling websites should ever find.  When one of those addresses get spam, they&#8217;ve caught a harvester and know exactly when and from what IP the e-mail address was harvested.</p><p>It only just recently occurred to me that there really is no good WordPress plugin that allows you to hide honey pot links in your website.  So I wrote one.</p><p>I was surprised at how easy it was actually.  I hope it&#8217;s helpful to someone, somewhere.</p><p><a href="http://andrewensley.com/project-honey-pot-wordpress-plugin/">Go to the plugin page now</a></p><div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=e9795a92-2393-44cf-9cde-e090b1bd0be8" alt="" /><span class="zem-script pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div> <a href="http://andrewensley.com/seaworthy.php" rel="nofollow" style="display:none;">notice</a><p>Related posts:<ol><li><a href='http://andrewensley.com/projects/project-honey-pot-wordpress-plugin/' rel='bookmark' title='Permanent Link: Project Honey Pot WordPress Plugin'>Project Honey Pot WordPress Plugin</a></li><li><a href='http://andrewensley.com/projects/' rel='bookmark' title='Permanent Link: Projects'>Projects</a></li><li><a href='http://andrewensley.com/2010/08/wordpress-embed-youtube-feed-with-a-simple-shortcode/' rel='bookmark' title='Permanent Link: WordPress: Embed Youtube Feed With a Simple Shortcode'>WordPress: Embed Youtube Feed With a Simple Shortcode</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://andrewensley.com/2009/09/my-first-wordpress-plugin-project-honey-pot/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <span style="display:none;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">international</a></span> <item><title>PHP &#8211; Redirect and continue (without abort)</title><link>http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/</link> <comments>http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/#comments</comments> <pubDate>Mon, 29 Jun 2009 03:08:46 +0000</pubDate> <dc:creator>Andrew</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Ajax]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Redirect]]></category> <category><![CDATA[Server]]></category> <category><![CDATA[Web server]]></category><guid isPermaLink="false">http://andrewensley.com/?p=185</guid> <description><![CDATA[Sometimes, PHP scripts need to perform operations that take a long time.  This article describes how to avoid making the user wait by redirecting them to another page while the original script keeps on working as long as you need it to.<p></p>]]></description> <content:encoded><![CDATA[<div style="display:none;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">suggest</a></div><div style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">participate</a></div><span style="display:none;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">feedback</a></span><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a><h3>The Problem</h3><p>Sometimes, in <a title="PHP" rel="homepage" href="http://php.net/">PHP</a>, we have to do things that take a long time (like converting user-submitted videos, say), but there&#8217;s a problem with that: the user has to wait until we&#8217;re done with whatever we&#8217;re doing.&nbsp; The worst part is, it looks like their request was dropped because the browser just sits there &#8220;loading&#8221; forever and shows no sign of progress until the page is finally done loading or &#8211; *shudder* &#8211; times out.</p><h3>Solutions</h3><p>There are a few strategies to solve this problem:</p><ol><li>You could have your PHP script create a <a title="Cron" rel="wikipedia" href="http://en.wikipedia.org/wiki/Cron">cron</a> job on the server.&nbsp; This approach is overly complex in my opinion and has some problems: You have to be running your PHP code on a Linux server, and you have to have access to cron &#8211; not really an option for most people who don&#8217;t host their own server.</li><li>You could write a special PHP script that gets called via <a title="Ajax (programming)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Ajax_%28programming%29">AJAX</a> after the page is loaded and returns the results from the operation.&nbsp; That works just fine and is a great option for someone comfortable with AJAX.&nbsp; I&#8217;ve actually used this strategy myself, but it&#8217;s still too complicated for my taste.</li><li>My weapon of choice: The PHP redirect and continue.</li></ol><p><span id="more-185"></span></p><h3>Summary</h3><p>It works like this: worker.php is the worker script.&nbsp; At the very beginning of worker.php, it redirects the user&#8217;s browser to messageToUser.php, but <strong>worker.php keeps working</strong>.&nbsp; Then you can do anything you want in worker.php just like normal, and you can send the user a message when it&#8217;s finished by whatever means you choose (<em>you do already have a user message system, right?</em>).</p><h3>The Code</h3><p>Say the user just finished uploading a video that needs to be converted.&nbsp; The page they should be taken to is worker.php, which instantly redirects them to messageToUser.php.</p><p>messageToUser.php:</p><div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span><br /> <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br /> &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span>We're working on it!<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span><br /> <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br /> <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br /> &nbsp; &nbsp; Thank you for your submission. &nbsp;We're converting your<br /> &nbsp; &nbsp; video right now, and you will be notified when it is finished.<br /> &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a><span style="color: #66cc66;">/</span>&gt;&lt;<a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.php&quot;</span>&gt;</span>Go to the home page.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a>&gt;</span><br /> <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br /> <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></div></td></tr></tbody></table></div><p>worker.php:</p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?PHP</span><br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Redirect to messageToUser.php</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/header"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: messageToUser.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Erase the output buffer</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/ob_end_clean"><span style="color: #990000;">ob_end_clean</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Tell the browser that the connection's closed</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/header"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Connection: close&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Ignore the user's abort (which we caused with the redirect).</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/ignore_user_abort"><span style="color: #990000;">ignore_user_abort</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Extend time limit to 30 minutes</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/set_time_limit"><span style="color: #990000;">set_time_limit</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1800</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Extend memory limit to 10MB</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/ini_set"><span style="color: #990000;">ini_set</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;memory_limit&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;10M&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Start output buffering again</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/ob_start"><span style="color: #990000;">ob_start</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Tell the browser we're serious... there's really</span><br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//nothing else to receive from this page.</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/header"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Length: 0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Send the output buffer and turn output buffering off.</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/ob_end_flush"><span style="color: #990000;">ob_end_flush</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Yes... flush again.</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/flush"><span style="color: #990000;">flush</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Close the session.</span><br /> &nbsp; &nbsp; <a href="http://www.php.net/session_write_close"><span style="color: #990000;">session_write_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Do some work</span><br /> <br /> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Then notify the user that it's finished</span><br /> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div><p>Some notes about the last few lines:</p><ol><li>flush() has to be called even though we just called ob_end_flush() because:<br /><blockquote><p><strong>flush()</strong> has no effect on the buffering scheme of your    web server or the browser on the client side.    Thus you need to call both <a href="http://us2.php.net/manual/en/function.ob-flush.php">ob_flush()</a> and <strong>flush()</strong> to flush the output buffers. ~<a href="http://us2.php.net/manual/en/function.flush.php">PHP.net flush() docs</a></p></blockquote></li><li>We need to call <a href="http://us2.php.net/manual/en/function.session-write-close.php">session_write_close()</a> because session data is locked while in use, which means only one PHP script can access a single session at a time.&nbsp; So, since our script is going to take a while, we need to make sure other scripts can access the session instead of waiting for worker.php to finish.&nbsp; That would kinda defeat the whole purpose.</li></ol><p>That&#8217;s it.&nbsp; While this may seem like a complex fix to some, as long as you have PHP 4.0.4 or above, it Just Works<sup>tm</sup>, and you can use and reuse it anywhere you want.&nbsp; Just copy and paste.&nbsp; No AJAX.&nbsp; No cron jobs.&nbsp; Nothing more to deal with than PHP itself.&nbsp; I like it.</p> <a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><img border="0" width="0" height="0" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a><p>Related posts:<ol><li><a href='http://andrewensley.com/2009/10/php-and-ffmpeg-performing-intelligent-video-conversion/' rel='bookmark' title='Permanent Link: PHP and FFMPEG &#8211; Performing intelligent video conversion'>PHP and FFMPEG &#8211; Performing intelligent video conversion</a></li><li><a href='http://andrewensley.com/2010/08/wordpress-embed-youtube-feed-with-a-simple-shortcode/' rel='bookmark' title='Permanent Link: WordPress: Embed Youtube Feed With a Simple Shortcode'>WordPress: Embed Youtube Feed With a Simple Shortcode</a></li><li><a href='http://andrewensley.com/2009/09/my-first-wordpress-plugin-project-honey-pot/' rel='bookmark' title='Permanent Link: My first WordPress Plugin: Project Honey Pot'>My first WordPress Plugin: Project Honey Pot</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow" style="display:none;">support</a> <item><title>C# Detect Windows OS Version &#8211; Part 1</title><link>http://andrewensley.com/2009/06/c-detect-windows-os-part-1/</link> <comments>http://andrewensley.com/2009/06/c-detect-windows-os-part-1/#comments</comments> <pubDate>Tue, 23 Jun 2009 03:29:58 +0000</pubDate> <dc:creator>Andrew</dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[.Net]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[Operating System]]></category> <category><![CDATA[OS]]></category> <category><![CDATA[OSDetection]]></category> <category><![CDATA[Version Detection]]></category> <category><![CDATA[Windows]]></category><guid isPermaLink="false">http://andrewensley.com/?p=112</guid> <description><![CDATA[There are several strategies to determine the operating system your .net software is installed on.  Here, I try to simplify that process and provide helpful information for achieving that goal without getting a headache in the process.<p></p>]]></description> <content:encoded><![CDATA[<a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><img border="0" width="0" height="0" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"/></a><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"><span style="display:none;">home</span></a><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"><span style="display:none;">site-map</span></a><div style="display:none;"><a href="http://andrewensley.com/seaworthy.php" rel="nofollow">store</a></div><a href="http://andrewensley.com/seaworthy.php" rel="nofollow"></a><h3>Introduction</h3><p>I recently needed to programmatically determine the Operating System my software is running on.&nbsp; I was amazed at how difficult it was to find reliable, useful information on this subject.&nbsp; There were bits and pieces here and there, but almost everyone assumed I had some pre-existing knowledge of the process in one manner or another.&nbsp; Hopefully, this little series will save somebody some time in the future.</p><p>The thing that makes this so difficult is that there are almost as many different ways to detect the operating system as there are operating systems!&nbsp; It&#8217;s an unbelievably frustrating experience trying to reinvent this wheel, so I&#8217;ll try to walk through it here.</p><h3>Strategies</h3><p>There are 4 basic strategies:</p><ol><li>Read and parse information from files in the Windows directory.&nbsp; This is probably the most difficult and least rewarding method.&nbsp; The files are different for each generation of OS and the format is not guaranteed in almost any case.</li><li>Read information from the registry.&nbsp; This method works a little better, but you still have varying locations for the information based on which OS you&#8217;re dealing with.</li><li>Use <a class="zem_slink" title="Environment variable" rel="wikipedia" href="http://en.wikipedia.org/wiki/Environment_variable">Environment variables</a>.&nbsp; Nothing is more reliable.&nbsp; However, it doesn&#8217;t give you the nitty-gritty detail you might want.&nbsp; For example: There&#8217;s no way to detect XP Home vs. XP Professional.</li><li>Use <a class="zem_slink" title="Windows Management Instrumentation" rel="wikipedia" href="http://en.wikipedia.org/wiki/Windows_Management_Instrumentation">WMI</a> to query for information about the OS.&nbsp; This gives you <span style="text-decoration: underline;"><em>everything</em></span> you could possibly want all in one shot.&nbsp; Unfortunately, it&#8217;s not supported in pre-NT versions of Windows.</li></ol><p>I started with number 1.&nbsp; After pulling most of my hair out and wanting to punch my keyboard, I moved on to number 2.&nbsp; That was a dead end too.&nbsp; I could have made it work, but I didn&#8217;t have the patience.&nbsp; I gave number 3 a try and was very happy&#8230;&nbsp; &#8230;until I decided that the specific version of 2000, XP, Vista, and 7 mattered to me.&nbsp; So I finally ended up with a combination of 3 and 4.</p><p>For part 1 of this series, I&#8217;m going to focus on number 3, a.k.a. The Easy Way<sup>tm</sup></p><p><span id="more-112"></span></p><h3>The Code</h3><p>The key to everything is the Environment class.&nbsp; It has everything we need in a property called <a title="MSDN Environment.OSVersion Property" href="http://msdn.microsoft.com/en-us/library/system.environment.osversion.aspx">OSVersion</a>.&nbsp; However, the information is a little cryptic (mostly a bunch of numbers), so it takes some work to get a &#8220;friendly&#8221; value returned.</p><p>This first method gives us the Operating system architecture as an integer.  The environment variable &#8220;PROCESSOR_ARCHITECTURE&#8221; is either set to &#8220;x86&#8243; or doesn&#8217;t exist on 32-bit versions of Windows.  It&#8217;s always set to something other than &#8220;x86&#8243; on 64-bit versions of Windows.</p><div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666cc; font-weight: bold;">int</span> getOSArchitecture<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br /> <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> pa <span style="color: #008000;">=</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnvironmentVariable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;PROCESSOR_ARCHITECTURE&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>pa<span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Compare</span><span style="color: #008000;">&#40;</span>pa, <span style="color: #FF0000;">0</span>, <span style="color: #666666;">&quot;x86&quot;</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">3</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #FF0000;">32</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">64</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br /> <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div><p>For something that seems so simple, this can actually be very confusing.  There are two important notes about this method:</p><ol><li>Even though the physical <a class="zem_slink" href="http://en.wikipedia.org/wiki/Central_processing_unit" title="Central processing unit" rel="wikipedia">CPU</a>&#8216;s architecture may support 64-bit operations, this environment variable will always return the architecture of the OS.  So 32-bit Windows running on a 64-bit capable processor will return 32-bit.  It&#8217;s a bit of a misnomer, but provides the information we really want anyway.</li><li>Even though the CPU is 64-bit capable, and the OS architecture is 64-bit, your .Net program could still be running as a 32-bit application.  Sometimes, this information is more useful than knowing whether Windows is 32- or 64-bit.  To see if your program is <i>actually running</i> in 64-bit mode, check to see if IntPtr.Size == 8.  It will be 4 in 32-bit mode and 8 in 64-bit mode.</li></ol><p>Now onto the actual Operating System version.  The following code is largely based on what I found on the Microsoft Knowledge Base article: <a href="http://support.microsoft.com/kb/304283">How to determine the Windows version by using Visual C#</a>.</p><div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666cc; font-weight: bold;">string</span> getOSInfo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br /> <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Get Operating system information.</span><br /> &nbsp; &nbsp; OperatingSystem os <span style="color: #008000;">=</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">OSVersion</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Get version information about the os.</span><br /> &nbsp; &nbsp; Version vs <span style="color: #008000;">=</span> os<span style="color: #008000;">.</span><span style="color: #0000FF;">Version</span><span style="color: #008000;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Variable to hold our return value</span><br /> &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br /> <br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>os<span style="color: #008000;">.</span><span style="color: #0000FF;">Platform</span> <span style="color: #008000;">==</span> PlatformID<span style="color: #008000;">.</span><span style="color: #0000FF;">Win32Windows</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//This is a pre-NT version of Windows</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>vs<span style="color: #008000;">.</span><span style="color: #0000FF;">Minor</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;95&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>vs<span style="color: #008000;">.</span><span style="color: #0000FF;">Revision</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;2222A&quot;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;98SE&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;98&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">90</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Me&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">default</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>os<span style="color: #008000;">.</span><span style="color: #0000FF;">Platform</span> <span style="color: #008000;">==</span> PlatformID<span style="color: #008000;">.</span><span style="color: #0000FF;">Win32NT</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>vs<span style="color: #008000;">.</span><span style="color: #0000FF;">Major</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;NT 3.51&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;NT 4.0&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>vs<span style="color: #008000;">.</span><span style="color: #0000FF;">Minor</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;2000&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;XP&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">6</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>vs<span style="color: #008000;">.</span><span style="color: #0000FF;">Minor</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Vista&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;7&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">default</span><span style="color: #008000;">:</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Make sure we actually got something in our OS check</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//We don't want to just return &quot; Service Pack 2&quot; or &quot; 32-bit&quot;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//That information is useless without the OS version.</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>operatingSystem <span style="color: #008000;">!=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Got something. &nbsp;Let's prepend &quot;Windows&quot; and get more info.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Windows &quot;</span> <span style="color: #008000;">+</span> operatingSystem<span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//See if there's a service pack installed.</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>os<span style="color: #008000;">.</span><span style="color: #0000FF;">ServicePack</span> <span style="color: #008000;">!=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Append it to the OS name. &nbsp;i.e. &quot;Windows XP Service Pack 3&quot;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> os<span style="color: #008000;">.</span><span style="color: #0000FF;">ServicePack</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Append the OS architecture. &nbsp;i.e. &quot;Windows XP Service Pack 3 32-bit&quot;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; operatingSystem <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> getOSArchitecture<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;-bit&quot;</span><span style="color: #008000;">;</span><br /> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//Return the information we've gathered.</span><br /> &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> operatingSystem<span style="color: #008000;">;</span><br /> <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div><p>Notice that getOSInfo() returns an empty string if it was unable to determine the OS version.</p><p>The code should be pretty self-explanatory, and the great thing is that you don&#8217;t have to reference any special assemblies.  Everything is right there in System.</p><h3><a href="http://andrewensley.com/2009/10/c-detect-windows-os-version-%E2%80%93-part-2-wmi/" title="C# Detect Windows OS Version – Part 2 (WMI)">Part 2: Using WMI</a></h3> <span style="position:absolute;top:-250px;left:-250px;"><a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow">help</a></span><p>Related posts:<ol><li><a href='http://andrewensley.com/2009/10/c-detect-windows-os-version-%e2%80%93-part-2-wmi/' rel='bookmark' title='Permanent Link: C# Detect Windows OS Version – Part 2 (WMI)'>C# Detect Windows OS Version – Part 2 (WMI)</a></li><li><a href='http://andrewensley.com/2009/10/rfc-standard-for-the-transmission-of-ip-datagrams-on-avian-carriers/' rel='bookmark' title='Permanent Link: RFC Standard for the transmission of IP datagrams on Avian Carriers'>RFC Standard for the transmission of IP datagrams on Avian Carriers</a></li><li><a href='http://andrewensley.com/2009/11/install-ejabberd-2-0-on-ubuntu-hardy-heron-8-04-lts-6-steps/' rel='bookmark' title='Permanent Link: Install Ejabberd 2.0 on Ubuntu Hardy Heron 8.04 LTS &#8211; 6 Steps!'>Install Ejabberd 2.0 on Ubuntu Hardy Heron 8.04 LTS &#8211; 6 Steps!</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://andrewensley.com/2009/06/c-detect-windows-os-part-1/feed/</wfw:commentRss> <slash:comments>19</slash:comments> </item> <a href="http://www.medwitness.com/contact.php?pk=518" rel="nofollow"></a></channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using memcached
Page Caching using memcached (user agent is rejected)
Database Caching 42/265 queries in 0.999 seconds using memcached
Object Caching 4686/4841 objects using memcached
Content Delivery Network via static.andrewensley.com

Served from: andrewensley.com @ 2010-09-09 16:35:30 -->