<?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>Knowledge Sharing &#187; PHP</title>
	<atom:link href="http://blog.danigunawan.com/category/web-programming-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.danigunawan.com</link>
	<description>"Say it... although a word..."</description>
	<lastBuildDate>Wed, 25 Jan 2012 00:14:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>IF Construct in One Line Using PHP</title>
		<link>http://blog.danigunawan.com/programming/if-construct-in-one-line-using-php/</link>
		<comments>http://blog.danigunawan.com/programming/if-construct-in-one-line-using-php/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 03:49:49 +0000</pubDate>
		<dc:creator>Dagu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Condition]]></category>
		<category><![CDATA[Construct]]></category>
		<category><![CDATA[If]]></category>
		<category><![CDATA[Script]]></category>

		<guid isPermaLink="false">http://blog.danigunawan.com/?p=582</guid>
		<description><![CDATA[The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C: if (expr) statement The general format of if construct is like shown below: if (expr1) { //  statement if expr [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>The <em>if</em> construct is one of the most important   features of many languages, PHP included.  It allows for   conditional execution of code fragments.  PHP features an   <em>if</em> structure that is similar to that of C:</p>
<pre>if (expr)
   statement</pre>
<p>The general format of if construct is like shown below:</p>
<pre>if (expr1) {
   //  statement if expr 1 true
} else if (expr2) {
   // statement if expr 2 true
} else {
   // statement if expr 1 and 2 false
}</pre>
<p>You may find useful documentation about<em> </em><a title="if construct" href="http://us3.php.net/manual/en/control-structures.if.php" target="_blank"><em>if</em> construct</a>, <em><a title="else condition" href="http://us3.php.net/manual/en/control-structures.else.php" target="_blank">else</a> </em>and<em> <a title="else if" href="http://us3.php.net/manual/en/control-structures.elseif.php" target="_blank">else if</a> </em>in <a title="control structures" href="http://us3.php.net/manual/en/language.control-structures.php" target="_blank">php.net</a>.</p>
<p>But how if you only need two condition to produce a word or digit, and then save it into a variable or just echo it? You may write it like shown below:</p>
<pre>if ($status = '0')
   echo 'Draft';
else
   echo 'Published';</pre>
<p>In code example above, you need to write four lines. Compare with code shown below:</p>
<pre>echo $status == '0' ? 'Draft' : 'Published';</pre>
<p>The syntax is show below</p>
<pre>echo expr ? 'value if true' : 'value if false';</pre>
<p>You may change echo with variable assignment such as:</p>
<pre>$var = expr ? 'value if true' : 'value if false';</pre>
<p>Easy right? It cuts down four lines into one line only.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.danigunawan.com/programming/if-construct-in-one-line-using-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Load PHP Extension</title>
		<link>http://blog.danigunawan.com/programming/load-php-extension/</link>
		<comments>http://blog.danigunawan.com/programming/load-php-extension/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 16:19:10 +0000</pubDate>
		<dc:creator>Dagu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://blog.danigunawan.com/?p=357</guid>
		<description><![CDATA[A few CMSs need PHP library which is not activated as default. For example, in installation an e-commerce named magento (www.magentocommerce.com), needs a few PHP extensions. Usually, a good CMS checks system requirements before installation begins. Image below is an example for system requirements checking on Magento Installation. We could use phpinfo() function to view [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>A few CMSs need PHP library which is not activated as default. For example, in installation an e-commerce named magento (<a href="http://www.magentocommerce.com">www.magentocommerce.com</a>), needs a few PHP extensions. Usually, a good CMS checks system requirements before installation begins. Image below is an example for system requirements checking on Magento Installation.</p>
<div id="attachment_358" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.danigunawan.com/wp-content/uploads/2008/11/magento_config.jpg"><img class="size-medium wp-image-358" title="Pengecekan Kebutuhan Sistem di Magento" src="http://blog.danigunawan.com/wp-content/uploads/2008/11/magento_config-300x98.jpg" alt="Pengecekan Kebutuhan Sistem di Magento" width="300" height="98" /></a><p class="wp-caption-text">System Requirement Checking on Magento Commerce</p></div>
<p>We could use phpinfo() function to view which extensions have been loaded. The php.ini file is used to configure it. We could find it in PHP installation directory. If you use XAMPP, PHP configuration can be found in <strong>[XAMPP's Installation directory]/apache/bin</strong>.</p>
<p><span id="more-357"></span></p>
<p>To edit it, simply use a text editor (such as notepad). Find which extension will be activated. For example, in magento case, it needs <strong>curl </strong>and <strong>mcrypt </strong>extensions to be activated. All we need is finding lines that have words <strong>php_curl.dll</strong> and <strong>php_mcrypt.dll</strong> in php.ini. As default, the configuration for those extensions is:</p>
<pre>;extension=php_curl.dll
;extension=php_mcrypt.dll</pre>
<p>To activate them we need to delete semicolon in the first character of those two lines. If we have done, restart apache and check extensions status using phpinfo().</p>
<div id="attachment_359" class="wp-caption aligncenter" style="width: 490px"><a href="http://blog.danigunawan.com/wp-content/uploads/2008/11/phpinfo.jpg"><img class="size-full wp-image-359" title="PHP Info" src="http://blog.danigunawan.com/wp-content/uploads/2008/11/phpinfo.jpg" alt="PHP Info" width="480" height="282" /></a><p class="wp-caption-text">PHP Info</p></div>
<p><strong>Question: why does we delete that semicolon?</strong><br />
In PHP configuration file (you will find the same thing in apache configuration), a semicolon is used to mark that the line after that will not be executed. Because of that, the programmers use it to make comments.</p>
<p><strong>Becareful </strong>if you need to edit configuration file. If you make a mistake, possibly your PHP won&#8217;t work well. Always make a configuration file backup before make changes, in order to make sure it would be easy to restore the configuration file if something wrong happens.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.danigunawan.com/programming/load-php-extension/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Avoid Direct Download</title>
		<link>http://blog.danigunawan.com/web/avoid-direct-download/</link>
		<comments>http://blog.danigunawan.com/web/avoid-direct-download/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 02:29:00 +0000</pubDate>
		<dc:creator>Dagu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Download]]></category>

		<guid isPermaLink="false">http://blog.danigunawan.com/2007/12/19/mencegah-direct-download/</guid>
		<description><![CDATA[Case study: We would like to provide pdf files to be downloaded by visitors, but the file location is hidden. Visitors can download file with URL given only. Programming language used is PHP. Analysis: We could make a link directly to the file location. Example: &#60;a href="myebook.pdf"&#62;Download MyEbook&#60;/a&#62; What happen if the link above clicked? [...]
Related posts:<ol>
<li><a href='http://blog.danigunawan.com/database/creating-database-in-mysql/' rel='bookmark' title='Creating Database in MySQL'>Creating Database in MySQL</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><span style="font-weight: bold;">Case study:</span><br />
We would like to provide pdf files to be downloaded by visitors, but the file location is hidden. Visitors can download file with URL given only. Programming language used is PHP.</p>
<p><span style="font-weight: bold;">Analysis:</span><br />
We could make a link directly to the file location. Example:</p>
<pre><span style="font-family: courier new;">&lt;a href="myebook.pdf"&gt;Download MyEbook&lt;/a&gt;</span></pre>
<p>What happen if the link above clicked? It depends to the visitors&#8217; browser. If the plugin to read pdf is installed, then browser will open that file inside browser. But, how if there&#8217;s no pdf reader plugin? Then a save file dialog box appear. We could save that file.</p>
<p>A problem is done if the visitors don&#8217;t have pdf reader plugin. But what about the visitors who have it? Of course the problem is not solved. Another problem is the visitors know the file location,  so it allows the visitors to download the file directly (download it directly by typing/copy paste the URL in the browser, without clicking from link given).</p>
<p><span id="more-92"></span></p>
<p><span style="font-weight: bold;">Solution:</span><br />
We need a bit code (PHP) to solve this problem.</p>
<p>The code to avoid direct download is shown below:</p>
<div class="geshi no php">
<div class="head">$task = $_REQUEST[&#39;task&#39;];</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">switch</span><span class="br0">&#40;</span><span class="re1">$task</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw1">case</span> <span class="st0">&#39;download&#39;</span><span class="sy0">:</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="co1">// file location</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="re1">$file_path</span> <span class="sy0">=</span> <span class="st0">&#39;dl/myebook.pdf&#39;</span><span class="sy0">;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"> <span class="co1">// function to get file name without path</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="re1">$file_name</span> <span class="sy0">=</span> <span class="kw3">basename</span><span class="br0">&#40;</span><span class="re1">$file_path</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"> <span class="co1">// get file size</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="re1">$fsize</span> <span class="sy0">=</span> <span class="kw3">filesize</span><span class="br0">&#40;</span><span class="re1">$file_path</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"> <span class="co1">// set headers</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;Pragma: public&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;Expires: 0&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;Cache-Control: must-revalidate, post-check=0, pre-check=0&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;Cache-Control: public&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;Content-Description: File Transfer&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;Content-Type: application/pdf&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&#39;Content-Disposition: attachment; filename=&quot;&#39;</span> <span class="sy0">.</span> <span class="re1">$file_name</span> <span class="sy0">.</span> <span class="st0">&#39;&quot;&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;Content-Transfer-Encoding: binary&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;Content-Length: &quot;</span> <span class="sy0">.</span> <span class="re1">$fsize</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"> <span class="co1">// start downloading from here</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="re1">$file</span> <span class="sy0">=</span> <span class="sy0">@</span><span class="kw3">fopen</span><span class="br0">&#40;</span><span class="re1">$file_path</span><span class="sy0">,</span><span class="st0">&quot;rb&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re1">$file</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> </div>
</li>
<li class="li1">
<div class="de1">   <span class="kw1">while</span><span class="br0">&#40;</span><span class="sy0">!</span><span class="kw3">feof</span><span class="br0">&#40;</span><span class="re1">$file</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> </div>
</li>
<li class="li1">
<div class="de1">   <span class="kw3">print</span><span class="br0">&#40;</span><span class="kw3">fread</span><span class="br0">&#40;</span><span class="re1">$file</span><span class="sy0">,</span> <span class="nu0">1024</span><span class="sy0">*</span><span class="nu0">8</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> </div>
</li>
<li class="li1">
<div class="de1">   <span class="kw3">flush</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">     <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw3">connection_status</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">!=</span><span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">         <span class="sy0">@</span><span class="kw3">fclose</span><span class="br0">&#40;</span><span class="re1">$file</span><span class="br0">&#41;</span><span class="sy0">;</span> </div>
</li>
<li class="li1">
<div class="de1">         <span class="kw3">die</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">     <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">   <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">   <span class="sy0">@</span><span class="kw3">fclose</span><span class="br0">&#40;</span><span class="re1">$file</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"> <span class="kw1">break</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"> <span class="kw2">default</span><span class="sy0">:</span></div>
</li>
<li class="li1">
<div class="de1">   <span class="kw3">echo</span> <span class="st0">&#39;&lt;a href=&quot;dlfile.php?task=download&quot;&gt;Download MyEbook&lt;/a&gt;&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">   <span class="kw1">break</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>You need to concern to this line:</p>
<pre><span style="font-family: courier new;">header('Content-Disposition: attachment; filename="myebook.pdf"');</span></pre>
<p>This line forces browser to show save dialog box for &#8220;myebook.pdf&#8221; although there&#8217;s pdf reader plugin in the browser.</p>
<p>To change file type (exe or zip), we need to change Content-Type only. Here are the lists:</p>
<pre><span style="font-family: courier new;">// archives</span>
<span style="font-family: courier new;">application/zip</span></pre>
<pre><span style="font-family: courier new;">// documents</span>
<span style="font-family: courier new;">application/pdf</span>
<span style="font-family: courier new;">application/msword</span>
<span style="font-family: courier new;">application/vnd.ms-excel</span>
<span style="font-family: courier new;">application/vnd.ms-powerpoint</span>
<span style="font-family: courier new;">// executables</span>
<span style="font-family: courier new;">application/octet-stream</span></pre>
<pre><span style="font-family: courier new;">// images</span>
<span style="font-family: courier new;">image/gif</span>
<span style="font-family: courier new;">image/png</span>
<span style="font-family: courier new;">image/jpeg</span><span style="font-family: courier new;">
</span></pre>
<pre><span style="font-family: courier new;">// audio</span>
<span style="font-family: courier new;">audio/mpeg</span>
<span style="font-family: courier new;">audio/x-wav</span></pre>
<pre><span style="font-family: courier new;">// video</span>
<span style="font-family: courier new;">video/mpeg</span>
<span style="font-family: courier new;">video/quicktime</span>
<span style="font-family: courier new;">video/x-msvideo</span></pre>
<p>File starts downloading from the line after this line:</p>
<pre><span style="font-family: courier new;">// start downloading from here</span></pre>
<p>There&#8217;s a question. Why do we need to avoid direct download? The purpose is to check how many times a file has been downloaded. If the visitors are allowed to download files directly, I&#8217;m sure it will be difficult to know how many times a file is downloaded. If you don&#8217;t need to check how many times the visitors download your files, you could use both direct download or indirect download.</p>
<p>Related posts:<ol>
<li><a href='http://blog.danigunawan.com/database/creating-database-in-mysql/' rel='bookmark' title='Creating Database in MySQL'>Creating Database in MySQL</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.danigunawan.com/web/avoid-direct-download/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

