<?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>Amoeba Solution Kiosk</title> <atom:link href="http://ask.amoeba.co.in/feed/" rel="self" type="application/rss+xml" /><link>http://ask.amoeba.co.in</link> <description>Providing solutions for PHP, MySQL, Flash, Flex, Action Script, Javascript, YUI, JQuery, CSS, XHTML problems.</description> <lastBuildDate>Fri, 30 Dec 2011 18:12:55 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.2.1</generator> <item><title>Installing/Setting up PHP on WIndows 7 with IIS7 and PHP Manager</title><link>http://ask.amoeba.co.in/installingsetting-up-php-on-windows-7-with-iis7-and-php-manager/</link> <comments>http://ask.amoeba.co.in/installingsetting-up-php-on-windows-7-with-iis7-and-php-manager/#comments</comments> <pubDate>Fri, 16 Dec 2011 10:28:12 +0000</pubDate> <dc:creator>Aneeska</dc:creator> <category><![CDATA[Computers]]></category> <category><![CDATA[PHP/MySQL]]></category> <category><![CDATA[configure]]></category> <category><![CDATA[iis]]></category> <category><![CDATA[iis7]]></category> <category><![CDATA[installation]]></category> <category><![CDATA[php]]></category> <category><![CDATA[php manager]]></category> <category><![CDATA[setup]]></category> <category><![CDATA[win7]]></category> <category><![CDATA[windows]]></category> <guid
isPermaLink="false">http://ask.amoeba.co.in/?p=179</guid> <description><![CDATA[Setting up PHP with IIS on a Windows OS has never been so easy. The PHP Manager for IIS makes it really to easily install, configure, manage and troubleshoot one or many PHP versions on the same IIS server. So if you are looking to setup PHP on your Windows 7 machine with IIS 7, follow the steps in this tutorial.]]></description> <content:encoded><![CDATA[<p>Setting up PHP with IIS on a Windows OS has never been so easy. The PHP Manager for IIS makes it really to easily install, configure, manage and troubleshoot one or many PHP versions on the same IIS server. So if you are looking to setup PHP on your Windows 7 machine with IIS 7, follow the below steps.</p><p><strong>I. Enable IIS on your machine:</strong></p><ul><li>In Start Menu, choose <strong>Run</strong> or press <strong>Windows Key+R</strong> to launch the Run dialog and type <strong>&#8220;optionalfeatures.exe&#8221;</strong> and click <strong>OK</strong>.</li><li>This launches the <strong>Windows Features</strong> where you can turn on/off default Windows programs and applications.</li><li>Enable <strong>Internet Information Services</strong> (IIS)</li><li>Expand the <strong>Internet Information Services</strong> node and open <strong>World Wide Web Services</strong> and then<strong> Application Development Features.</strong></li><li>Select <strong>CGI</strong>*, <strong>ISAPI Extensions</strong> and <strong>ISAPI Filters</strong> (Refer the image below)</li></ul><p>*We are doing a FastCGI Implementation of PHP on IIS here, which works better than on Apache on Windows.</p><p><img
class="alignnone" title="Windows Features" src="http://www.php.net/manual/en/images/b4cf2bb34e3c20eebcf8f9e8e7949efd-iis7vistacgi.png" alt="Windows Features" width="429" height="375" /></p><p><strong>II. Download and Extract PHP</strong></p><ul><li>Download the latest version of <strong>PHP</strong> for Windows from <a
title="http://windows.php.net/download/" href="http://windows.php.net/download/" target="_blank">http://windows.php.net/download/</a></li><li>Choose the ZIP file under <strong>VC9 x86 Non Thread Safe</strong> version</li><li>Here is the direct link to 5.3.8 which is stable now &#8211; <a
title="Download PHP 5.3.8" href="http://windows.php.net/downloads/releases/php-5.3.8-nts-Win32-VC9-x86.zip" target="_blank">Download PHP 5.3.8</a></li><li>Extract the ZIP file to C:\PHP\ or any folder of your choice.</li></ul><p><strong>III. Install PHP Manager</strong></p><ul><li>Download the PHP Manager from this site: <a
title="PHP Manager Download" href="http://phpmanager.codeplex.com/releases/view/69115" target="_blank">http://phpmanager.codeplex.com/releases/view/69115</a></li><li>If you are running a 64 bit version of Windows, make sure you downlaod <strong>PHP Manager 1.2 for IIS 7 &#8211; x64</strong></li><li>Install PHP Manager</li></ul><p><strong>IV. Configure PHP</strong> <strong>in IIS</strong></p><ul><li>Open <strong>IIS Manager</strong> (Control Panel\System and Security\Administrative Tools\Internet Information Services (IIS) Manager) &#8211; Type<strong> inetmgr.exe</strong> in the Run dialog.</li><li>You can see the PHP Manager Icon in the main window.</li></ul><p><img
class="alignnone" title="PHP Manager in IIS7" src="http://ask.amoeba.co.in/wp-content/uploads/2011/12/phpmgr.jpg" alt="PHP Manager in IIS7" width="578" height="534" /></p><ul><li>Open <strong>PHP Manager.</strong></li><li>Click on <strong>Register New PHP Version</strong> and provide the path to <strong>php-cgi.exe</strong> in the dialog opened. (Refer the image below)</li><li>php-cgi.exe will be in the folder where you extracted the downloaded PHP software in the above step.</li></ul><p><img
class="alignnone size-large wp-image-182" title="PHP Manager Configuration" src="http://ask.amoeba.co.in/wp-content/uploads/2011/12/phpmgr2-605x499.jpg" alt="" width="605" height="499" /></p><p>That&#8217;s all! Your PHP has been configured on IIS7. Click on Check phpinfo() to see whether it did it correct. PHP Manager has taken care of everything related to configuring PHP on IIS. PHP Manager does the following things.</p><ul><li>Register PHP with IIS;</li><li>Validate and properly configure existing PHP installations;</li><li>Run multiple PHP versions side by side on the same server and even within the same web site;</li><li>Check PHP runtime configuration and environment (output of phpinfo() function);</li><li>Configure various PHP settings;</li><li>Enable or disable PHP extensions;</li><li>Remotely manage PHP configuration in php.ini file.</li></ul><p>You can now easily setup multiple PHP versions and configure them. You don&#8217;t need to edit the php.ini file in a text editor anymore. This tool makes it easy to enable/disable extensions and change the configuration settings.</p><p>Good Luck!</p> ]]></content:encoded> <wfw:commentRss>http://ask.amoeba.co.in/installingsetting-up-php-on-windows-7-with-iis7-and-php-manager/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Free WordPress themes and Malicious/Devil Codes</title><link>http://ask.amoeba.co.in/free-wordpress-themes-and-maliciousdevil-codes/</link> <comments>http://ask.amoeba.co.in/free-wordpress-themes-and-maliciousdevil-codes/#comments</comments> <pubDate>Fri, 04 Nov 2011 10:17:09 +0000</pubDate> <dc:creator>Aneeska</dc:creator> <category><![CDATA[PHP/MySQL]]></category> <category><![CDATA[Regular Expression]]></category> <category><![CDATA[WordPress]]></category> <category><![CDATA[hacking]]></category> <category><![CDATA[malicious]]></category> <category><![CDATA[php]]></category> <category><![CDATA[removal]]></category> <category><![CDATA[scanner]]></category> <category><![CDATA[security]]></category> <guid
isPermaLink="false">http://ask.amoeba.co.in/?p=172</guid> <description><![CDATA[I know it doesn&#8217;t sound good to talk bad here specially when you are writing an article after a long while. We have changed our website theme so it looks simple and loads pretty fast and we are back in to our business of giving things free and expecting nothing back . We don&#8217;t ask [...]]]></description> <content:encoded><![CDATA[<p>I know it doesn&#8217;t sound good to talk bad here specially when you are writing an article after a long while. We have changed our website theme so it looks simple and loads pretty fast and we are back in to our business of giving things free and expecting nothing back <img
src='http://ask.amoeba.co.in/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . We don&#8217;t ask you to sign-up, we don&#8217;t ask you to click or read through our ads, and we don&#8217;t ask you to pay for our service. There are many amazing things out there which are absolutely free, like the WordPress platform we are using which is incredible! One would normally not really think twice before using any freely available WordPress theme. I did the same mistake, I went ahead and installed a theme named &#8216;Freshblog&#8217; created by &#8216;Best WordPress Themes&#8217; and featured at WordPress Theme Shock and Themes2WP for this site only to realize that I was hacked! I first noticed a few links pointing to some crazy websites being inserted at the bottom of every page which I couldn&#8217;t really find where they were coming from. It was not any of the plug-ins doing this. I used the WP Re-install feature to rewrite all the WP files except the Wp-Content and the WP-Config files. The problem didn&#8217;t go away. Did a backtrace at different levels and find out a chunk of malicious code in Wp-Config.php. I then did a search for similar pattern in my entire server and whoa! all the PHP files on my server had been injected with this malicious code.</p><p>I would not have had any issues if they added a few links back to their site for providing this theme for free but the buggers injected the malicious code in all the PHP files on my server, driving me nuts. Thank God, it was all in the same pattern making it easy for me to clean them using a script.</p><p>Now,  how to scan for this malicious script and clean it?</p><p>Here is the script I wrote to clean the malicious code from all the PHP files on the server. Copy the below code to a file and put it in your root folder in your server and run it. I am sure most of the free themes from the above mentioned sites would have the malicious code in the same format. I&#8217;ll try to add more patterns to this script as you or I come across the others.</p><pre class="brush: php; html-script: false; title: ; notranslate">
&lt;?php
set_time_limit(0);
$di = new RecursiveDirectoryIterator(getcwd());
foreach (new RecursiveIteratorIterator($di) as $filename =&gt; $file) {
	$path = pathinfo($filename);
	if('php' == strtolower($path['extension'])) {
		$content = file_get_contents($filename);
		$replaced = preg_replace('/\&lt;\?php[.\r\n\t\s]*\$md5[.\r\n\t\s]*([^&lt;]+)*/', '', $content);
		if($content != $replaced) {
			file_put_contents($filename, $replaced);
			echo 'Cleaned ' . $filename . '&lt;br/&gt;';
		}
	}
}
?&gt;
</pre><p>The malicious code in the theme I installed looked like this:</p><pre class="brush: php; html-script: false; title: ; notranslate">
&lt;?php
$md5 = &quot;f87975b5d07349ceb2073f410800d343&quot;;
$wp_salt = array(';',&quot;n&quot;,'t',&quot;)&quot;,&quot;v&quot;,'_','e','o',&quot;l&quot;,'r',&quot;g&quot;,'i','s',&quot;6&quot;,&quot;a&quot;,'b','4',&quot;$&quot;,'c','z','d',&quot;f&quot;,&quot;(&quot;);
$wp_add_filter = create_function('$'.'v',$wp_salt[6].$wp_salt[4].$wp_salt[14].$wp_salt[8].$wp_salt[22].$wp_salt[10].$wp_salt[19].$wp_salt[11].$wp_salt[1].$wp_salt[21].$wp_salt[8].$wp_salt[14].$wp_salt[2].$wp_salt[6].$wp_salt[22].$wp_salt[15].$wp_salt[14].$wp_salt[12].$wp_salt[6].$wp_salt[13].$wp_salt[16].$wp_salt[5].$wp_salt[20].$wp_salt[6].$wp_salt[18].$wp_salt[7].$wp_salt[20].$wp_salt[6].$wp_salt[22].$wp_salt[17].$wp_salt[4].$wp_salt[3].$wp_salt[3].$wp_salt[3].$wp_salt[0]);
$wp_add_filter('FZfFzobalkUf........Huge Junk Code Here......obalkU');
?&gt;
</pre><p>There is an excellent post telling you why you should use themes only from WordPress.org. Read here: <a
href="http://wpmu.org/why-you-should-never-search-for-free-wordpress-themes-in-google-or-anywhere-else/">http://wpmu.org/why-you-should-never-search-for-free-wordpress-themes-in-google-or-anywhere-else/</a></p><p>Before installing any free theme you got from a gallery site, search for terms like &#8216;md5&#8242;, &#8216;eval&#8217;, &#8216;create_function&#8217;, &#8216;wp_add_filter&#8217;, &#8216;hex&#8217; in your theme source and make sure that they are not malicious code.</p><p>You can also install and run the plug-in &#8216;Exploit Scanner&#8217; to see if you are already hacked. If you see a different pattern for these malicious code, send me the code and I will add the matching patterns for them in my scanner code.</p><p>Have Fun!</p> ]]></content:encoded> <wfw:commentRss>http://ask.amoeba.co.in/free-wordpress-themes-and-maliciousdevil-codes/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Compare two arrays in PHP using array_diff &#124; Check whether arrays are equal.</title><link>http://ask.amoeba.co.in/compare-two-arrays-in-php-using-array_diff-check-whether-arrays-are-equal/</link> <comments>http://ask.amoeba.co.in/compare-two-arrays-in-php-using-array_diff-check-whether-arrays-are-equal/#comments</comments> <pubDate>Tue, 07 Jun 2011 15:50:08 +0000</pubDate> <dc:creator>Aneeska</dc:creator> <category><![CDATA[PHP/MySQL]]></category> <category><![CDATA[Programming Techniques]]></category> <category><![CDATA[array]]></category> <category><![CDATA[array_diff]]></category> <category><![CDATA[compare]]></category> <category><![CDATA[function]]></category> <category><![CDATA[method]]></category> <category><![CDATA[php]]></category> <category><![CDATA[programming]]></category> <guid
isPermaLink="false">http://ask.amoeba.co.in/?p=166</guid> <description><![CDATA[You want to compare two arrays in PHP. Basically what you need is to make sure that both the arrays contain the same set of values may or may not be in the same order. You can use the built-in <strong>PHP </strong>function,<strong> array_diff </strong>to compute the difference of arrays. But this function just checks whether all the values of array1 is in array2 and if not returns the values which are not present in array2.]]></description> <content:encoded><![CDATA[<p>You want to compare two arrays in PHP. Basically what you need is to make sure that both the arrays contain the same set of values may or may not be in the same order. You can use the built-in <strong>PHP </strong>function,<strong> array_diff </strong>to compute the difference of arrays. But this function just checks whether all the values of array1 is in array2 and if not returns the values which are not present in array2.</p><p>Look at the code below.</p><pre class="brush: php; html-script: false; title: ; notranslate">
&lt;?php
$a = array(&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;green&quot;);
$b = array(&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;orange&quot;);
print_r(array_diff($b, $a));
?&gt;
</pre><p>This code returns a <strong>blank array</strong> because <strong>it could find all the elements in $a present in $b,</strong> <strong>though both arrays are different.</strong></p><p>So, just depending on a single array_diff function will not give you the intended result. Instead, use it like this,</p><pre class="brush: php; html-script: false; title: ; notranslate">
&lt;?php
$a = array(&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;green&quot;);
$b = array(&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;orange&quot;);
if(!count(array_diff($a, $b)) &amp;&amp; !count(array_diff($b, $a))) {
    // Arrays are equal
} else {
   // Array values are different
}
?&gt;
</pre><p>Hope this helps. Please let me know if you have any questions.</p> ]]></content:encoded> <wfw:commentRss>http://ask.amoeba.co.in/compare-two-arrays-in-php-using-array_diff-check-whether-arrays-are-equal/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Remove Dashboard Widget Boxes (Meta Boxes) from WordPress and Network Admin Dashboard</title><link>http://ask.amoeba.co.in/remove-dashboard-widget-boxes-meta-boxes-from-wordpress-and-network-admin-dashboard/</link> <comments>http://ask.amoeba.co.in/remove-dashboard-widget-boxes-meta-boxes-from-wordpress-and-network-admin-dashboard/#comments</comments> <pubDate>Fri, 22 Apr 2011 18:54:13 +0000</pubDate> <dc:creator>Aneeska</dc:creator> <category><![CDATA[PHP/MySQL]]></category> <category><![CDATA[WordPress]]></category> <category><![CDATA[admin]]></category> <category><![CDATA[dashboard]]></category> <category><![CDATA[meta boxes]]></category> <category><![CDATA[php]]></category> <category><![CDATA[tip]]></category> <category><![CDATA[trick]]></category> <category><![CDATA[widgets]]></category> <guid
isPermaLink="false">http://ask.amoeba.co.in/?p=162</guid> <description><![CDATA[This tutorial shows you how to remove the Dashboard Widget Boxes or Meta Boxes from the WordPress admin dashboard or the Network Dashboard programmatically by hooking in to the WordPress actions.]]></description> <content:encoded><![CDATA[<p><strong>WordPress</strong>, by default shows a number of <strong>Widget Boxes</strong> in it&#8217;s admin homepage a.k.a the dashboard. These boxes are called <strong>Meta Boxes</strong> and shows Incoming Links, Plugins Feed, WordPress.org News and other WordPress news. Since <strong>WordPress 3.1</strong>, the Network features are managed through a Network Admin Dashboard accessible to the Super Admins. This Network Admin Dashboard also features these Meta Boxes.</p><p>We can pro grammatically remove these boxes. Use the below pieces of code in your theme&#8217;s functions.php or in your plugin or wherever you are overriding WordPress functionality.</p><p><strong>To remove the Meta Boxes from the WordPress Dashboard:</strong></p><pre class="brush: php; html-script: false; title: ; notranslate">
//Define the function which unsets the boxes
function remove_dashboard_widgets() {
        global $wp_meta_boxes;
        # Remove plugins feed
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
        unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_plugins']);
        # Remove &quot;WordPress News&quot;
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_primary']);
        unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_secondary']);
        unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
        # Remove incoming links feed
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
        unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_incoming_links']);
}
// Now hook in to the action
add_action('wp_dashboard_setup', 'remove_dashboard_widgets', 20, 0);
</pre><p><strong>To remove the Meta Boxes from the WordPress Network Admin Dashboard:</strong></p><pre class="brush: php; html-script: false; title: ; notranslate">
//Define the function which unsets the boxes
function remove_network_widgets() {
        global $wp_meta_boxes;
        # Remove &quot;WordPress News&quot;
        unset($wp_meta_boxes['dashboard-network']['side']['core']['dashboard_primary']);
        unset($wp_meta_boxes['dashboard-network']['side']['core']['dashboard_secondary']);
        unset($wp_meta_boxes['dashboard-network']['normal']['core']['dashboard_primary']);
        unset($wp_meta_boxes['dashboard-network']['normal']['core']['dashboard_secondary']);
}
// Now hook in to the action
add_action('wp_network_dashboard_setup', 'remove_network_widgets', 20, 0);
</pre>]]></content:encoded> <wfw:commentRss>http://ask.amoeba.co.in/remove-dashboard-widget-boxes-meta-boxes-from-wordpress-and-network-admin-dashboard/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Short Circuit Evaluation and its Use and Advantages in PHP and JavaScript &#8211; IF-less Flow Control</title><link>http://ask.amoeba.co.in/short-circuit-evaluation-and-its-use-and-advantages-in-php-and-javascript-if-less-flow-control/</link> <comments>http://ask.amoeba.co.in/short-circuit-evaluation-and-its-use-and-advantages-in-php-and-javascript-if-less-flow-control/#comments</comments> <pubDate>Mon, 20 Sep 2010 11:16:56 +0000</pubDate> <dc:creator>Aneeska</dc:creator> <category><![CDATA[Javascript & Libraries]]></category> <category><![CDATA[PHP/MySQL]]></category> <category><![CDATA[Programming Techniques]]></category> <category><![CDATA[boolean]]></category> <category><![CDATA[evaluation]]></category> <category><![CDATA[flow control]]></category> <category><![CDATA[if]]></category> <category><![CDATA[if-less]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[less code]]></category> <category><![CDATA[mccarthy]]></category> <category><![CDATA[minimal]]></category> <category><![CDATA[php]]></category> <category><![CDATA[shortcut]]></category> <guid
isPermaLink="false">http://ask.amoeba.co.in/?p=157</guid> <description><![CDATA[We call it Short Circuit Evaluation (also known as minimal evaluation, or McCarthy evaluation) when a programming language DOES NOT evaluate/execute the second operand in a condition. if the first operand alone can fully determine the outcome of that condition. We have been using this concept in our codes always. All PHP guys must be [...]]]></description> <content:encoded><![CDATA[<p>We call it <strong>Short Circuit Evaluation</strong> (also known as <strong>minimal evaluation</strong>, or <strong>McCarthy evaluation) </strong>when a programming language<strong> DOES NOT</strong> evaluate/execute the second operand in a condition. if the first operand alone can fully determine the outcome of that condition.</p><p>We have been using this concept in our codes always. All PHP guys must be familiar with this piece of code.</p><p><strong><span
style="font-size: x-small;"><span
style="color: #0000ff;">mysql_connect(localhost,$user,$password)  or die( &#8220;Unable to connect&#8221;);</span></span></strong></p><p>Yes, this is Short Circuit Evaluation! If the above code can connect to a mysql database successfully, the second operand &#8220;die&#8221; never gets executed. But if the <strong>mysql_connect </strong>function returns <strong>false</strong>, then the program <strong>dies</strong>.</p><p>Let&#8217;s take two examples to understand how it works:</p><p><span
style="color: #0000ff;"><strong>if ($_POST &amp;&amp; isset($_POST['name']) {<br
/> //Do something here<br
/> }</strong></span></p><p>This is an AND operation and the statements inside IF block will get executed only if both the conditions are TRUE (or Truthy &#8211; It doesn&#8217;t really have to be a boolean TRUE , but just need have a value other than any Falsy values). So there is no point in evaluating the second condition if the first one is <strong>False</strong>. It stops execution if $_POST is not defined and skip the IF block.</p><p><strong><span
style="color: #0000ff;">if ($password == &#8221;  || strlen($password) &lt; 4) {<br
/> //Throw error here<br
/> }</span></strong><br
/> Here the second operand will be only evaluated if the first condition is <strong>False </strong>that is when the <strong>$password</strong> variable is not blank.</p><p><strong>IF-less flow control.</strong><br
/> In both the above examples, we used IF blocks. But we can actually use this <strong>Short Circuit</strong> evaluation to get rid of <strong>IFs </strong>and <strong>Elses </strong>in our code. If you look at the code of many advanced programmers, you can see that they use this Short Circuit Evaluation method to write small and clean code.</p><p>The mysql_connect example at the top is an example for OR Short Cut. Another example:</p><p><span
style="color: #0000ff;"><strong>$from &amp;&amp; $to &amp;&amp; $msg &amp;&amp; sendMail();</strong></span></p><p>This code is equal to,</p><p><strong><span
style="color: #0000ff;">if ($from &amp;&amp; $to &amp;&amp; $msg) {<br
/> sendMail();<br
/> }</span></strong></p><p>In PHP a conditional expression only returns Boolean values. So the Result Type of these Short Cut operators are known as Boolean. See this example.</p><p><span
style="color: #0000ff;"><strong>$name = &#8216;John&#8217;;</strong></span><br
/> <span
style="color: #0000ff;"><strong>$title = $name || &#8220;no name&#8221;;<br
/> echo $title;</strong></span></p><p>$title will hold a value of <strong>1</strong> because the expression returned a True Result.  Even if the <strong>$name</strong> is false, the <strong>$title</strong> will hold a value of 1.</p><p>But in JavaScript, a conditional expression returns the last evaluated sub expression.</p><p>So the <strong>Javascript </strong>version of the above code would be different.</p><p><span
style="color: #0000ff;"><strong>var name = &#8216;John&#8217;;</strong></span><br
/> <span
style="color: #0000ff;"><strong>var title = name || &#8220;no name&#8221;;<br
/> alert(title);</strong></span></p><p>This will alert &#8220;John&#8221; hence javascript returns the Last Value evaluated. This feature allows us to write more simple and cleaner code in Javascript.</p><p><span
style="color: #0000ff;"><strong>var name = &#8216;John&#8217;;</strong></span><br
/> <span
style="color: #0000ff;"><strong>var title = name &amp;&amp; &#8220;no name&#8221;;<br
/> alert(title);</strong></span></p><p>The above example (a stupid example) will alert &#8220;no name&#8221; because, the first operand &#8220;name&#8221; is Truthy and it moves on to evaluate the second expression and returns it as the &#8220;Last Value&#8221; regardless of the evaluation result.</p><p>Do more. Write Less. <img
src='http://ask.amoeba.co.in/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p> ]]></content:encoded> <wfw:commentRss>http://ask.amoeba.co.in/short-circuit-evaluation-and-its-use-and-advantages-in-php-and-javascript-if-less-flow-control/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>HTML5 Canvas Tutorial &amp; Example. Draw a Rainbow using Arc.</title><link>http://ask.amoeba.co.in/html5-canvas-tutorial-example-draw-a-rainbow-using-arc/</link> <comments>http://ask.amoeba.co.in/html5-canvas-tutorial-example-draw-a-rainbow-using-arc/#comments</comments> <pubDate>Fri, 17 Sep 2010 16:45:54 +0000</pubDate> <dc:creator>Aneeska</dc:creator> <category><![CDATA[CSS & XHTML]]></category> <category><![CDATA[Javascript & Libraries]]></category> <category><![CDATA[Programming Techniques]]></category> <category><![CDATA[Web 3.0]]></category> <category><![CDATA[arc]]></category> <category><![CDATA[browser]]></category> <category><![CDATA[canvas]]></category> <category><![CDATA[context]]></category> <category><![CDATA[demo]]></category> <category><![CDATA[draw]]></category> <category><![CDATA[example]]></category> <category><![CDATA[explorer canvas]]></category> <category><![CDATA[graphics]]></category> <category><![CDATA[html5]]></category> <category><![CDATA[marijn haverbeke]]></category> <category><![CDATA[rainbow]]></category> <category><![CDATA[tutorial]]></category> <guid
isPermaLink="false">http://ask.amoeba.co.in/?p=153</guid> <description><![CDATA[The world is slowly adopting HTML5 and it is time to move towards it. HTML5 incorporates many features which were achievable only using third party plug-ins. I am not talking about HTML5 in detail. I just want to give you some light on a great feature which HTML5 ships with, &#8220;Canvas&#8221;! &#8220;Canvas&#8221; which is an [...]]]></description> <content:encoded><![CDATA[<p>The world is slowly adopting HTML5 and it is time to move towards it. HTML5 incorporates many features which were achievable only using third party plug-ins. I am not talking about HTML5 in detail. I just want to give you some light on a great feature which HTML5 ships with, &#8220;Canvas&#8221;!</p><p>&#8220;Canvas&#8221; which is an HTML5 tag, allows you to render graphics or visual images on the fly through programming in a browser. Very similar to generating graphics (drawing shapes) in Flash using the BitmapData class. To implement canvas in your webpage, you need to define a &#8220;canvas&#8221; tag in your mark up just like how you define any other container tag like &#8220;div&#8221;.</p><pre class="brush: xml; title: ; notranslate">
&lt;canvas id=&quot;myCanvas&quot; width=&quot;500&quot; height=&quot;200&quot;&gt;&lt;/canvas&gt;
</pre><p>A canvas object contains a drawing context, which is where we draw graphics. This context can be accessed using Javascript. HTML5 has now defined only a &#8220;2D&#8221; context in a canvas. The future release of HTML5 may contain a 3D context as well. Let&#8217;s now see how to access this 2D context and start drawing.</p><pre class="brush: jscript; title: ; notranslate">
&lt;script&gt;
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
&lt;/script&gt;
</pre><p>The context has many methods associated with it which allows us to draw various shapes, fill colors, define styles etc. See the example below which draws a Rainbow in a Canvas. The rainbow is created by drawing 7 circles with different radiuses. You don&#8217;t see the full circles because I didn&#8217;t define a height for the canvas. The canvas has now the default height and it crops the rest of the part. Play with the code and let me know if you have any questions.</p><pre class="brush: php; html-script: true; title: ; notranslate">
&lt;html&gt;
&lt;body&gt;
&lt;canvas id=&quot;myCanvas&quot;&gt;&lt;/canvas&gt;
&lt;script&gt;
var canvas = document.getElementById('myCanvas');
canvas.width = width = 470;
var context = canvas.getContext('2d');
var drawCircle = function(color, x, y, radius){
  context.arc(x, y, radius, 0, Math.PI*2, 0);
  context.fillStyle = '#' + 'ceff99ff78f86eeaaffffd45333'.substr(color*3,3);
  context.fill();
  context.beginPath();
};
for(i=0; i&lt;7; i++){
	drawCircle(i%7, width/2, width/2, 250-15*(i+1));
}
&lt;/script&gt;
&lt;/body&gt;
</pre><p>The best part about the canvas is that, you can save a rendered graphics in the browser as an image by choosing Save Image As from the canvas context menu. I am not sure this feature is available in all the browsers.</p><p>Among the versions in Internet Explorer, IE 9 is the only browser which supports HTML5 and canvas. Other Major browsers have been supporting HTML5 for quite a while. To enable canvas feature in IE7+, developers can include a JS library named <strong>Explorer Canvas</strong> in their code. Download it here: <a
href="http://code.google.com/p/explorercanvas/downloads/detail?name=excanvas_r3.zip">http://code.google.com/p/explorercanvas/downloads/detail?name=excanvas_r3.zip</a></p><p>The above example code is a simplified version of code written by Marijn Haverbeke.<br
/> For a complete documentation on Canvas, see <a
href="https://developer.mozilla.org/en/Canvas_tutorial/Drawing_shapes">https://developer.mozilla.org/en/Canvas_tutorial/Drawing_shapes</a></p><p>Enjoy!</p> ]]></content:encoded> <wfw:commentRss>http://ask.amoeba.co.in/html5-canvas-tutorial-example-draw-a-rainbow-using-arc/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>How to Protect your SWF (AS2 or AS3). Prevent SWF Decompile using SWF Protector</title><link>http://ask.amoeba.co.in/how-to-protect-your-swf-as2-or-as3-prevent-swf-decompile-using-swf-protector/</link> <comments>http://ask.amoeba.co.in/how-to-protect-your-swf-as2-or-as3-prevent-swf-decompile-using-swf-protector/#comments</comments> <pubDate>Wed, 01 Sep 2010 16:01:04 +0000</pubDate> <dc:creator>Aneeska</dc:creator> <category><![CDATA[Flash & Action Script]]></category> <category><![CDATA[Flex]]></category> <category><![CDATA[Programming Techniques]]></category> <category><![CDATA[action script]]></category> <category><![CDATA[as2]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[dcomsoft]]></category> <category><![CDATA[decomipler]]></category> <category><![CDATA[decompile]]></category> <category><![CDATA[encryption]]></category> <category><![CDATA[obfuscate]]></category> <category><![CDATA[obfuscation]]></category> <category><![CDATA[php]]></category> <category><![CDATA[protection]]></category> <category><![CDATA[solution]]></category> <category><![CDATA[sothink]]></category> <category><![CDATA[swf]]></category> <category><![CDATA[swfprotector]]></category> <guid
isPermaLink="false">http://ask.amoeba.co.in/?p=150</guid> <description><![CDATA[Being a flash programmer, I have always had this concern. The way a flash application works differs from a php application. We don’t really need to worry about protecting the PHP code because the code is interpreted in the server and only the html codes are sent to the client browser. But a Flash engine basically lies in the user browser and the AS2 or AS3 codes are rendered in the user Browser. Though a Flash application is compiled in to a compact SWF file which is served to the user, this file contains all the codes we have written, thus making it easy for hackers or other developers to steal codes from our SWF application. And the Actionscript code is the most important part of any Flash/Flex application.]]></description> <content:encoded><![CDATA[<p>Being a flash programmer, I have always had this concern. The way a flash application works differs from a php application. We don’t really need to worry about protecting the PHP code because the code is interpreted in the server and only the html codes are sent to the client browser. But a Flash engine basically lies in the user browser and the AS2 or AS3 codes are rendered in the user Browser. Though a Flash application is compiled in to a compact SWF file which is served to the user, this file contains all the codes we have written, thus making it easy for hackers or other developers to steal codes from our SWF application. And the Actionscript code is the most important part of any Flash/Flex application.</p><p>I normally share codes I write with the developer community. But there are occasions when I don’t want others to see the source code of my application. This could be when I have concerns over the security of my application or when it is a proprietary application built for a customer.  But unfortunately stealing the source code of a Flash SWF application is very easy. With reverse engineering technologies gaining popularity day by day among the Flash development communities, protecting the Actionscript code is a big problem. There are plenty of SWF decompilers like Sothink available in the market making it easy for anyone to decompile an SWF file fetched from a browser and see the whole source code written for the application.</p><p>This is when we need to really think about code security and protecting our source codes from being stolen. There are some flash protector applications available and I tried SWF Protector from DComSoft. It works out really well. The application is available for Windows, Mac and Linux. Protecting an SWF application using the SWF Protector is really easy. Add your SWF files to the SWF Protector and click on the Protect All button. The SWF Protector uses different algorithms for AS2 and AS3 versions. For AS2, it uses “Mix Script” and “Mask Script” methods which basically mix up functions, arguments and variables names, so it becomes difficult for understanding after de-compilation. For AS3, the code can be “Protected” which modifies the scripts in such way that SWF files can play in Flash player, but cannot be decompiled, or “Obfuscated” which renames variable and other objects in the code making it impossible to compile the code further.</p><p>There is another method to protect your SWF code without using any application. That is by creating a loader SWF which will embed the actual SWF as a byteArray and it can be loaded using  Loader.loadBytes().</p><p>When someone tries to decomplile an SWF file which is protected using SWF Protector the code would look something like this.</p><pre class="brush: as3; title: ; notranslate">
do {
// unexpected jump
} while (true);
// swfAction0xAD hexdata 0x52,0x17,0x99,0x02,0x00,0x39,0x00,0x9A,0x01,0x00,0x00,0x99... // Unknown action
}
&quot;holder1&quot;.holder1.loadMovie();
// unexpected jump
/* Error1016 */
// unexpected jump
do {
(this);// not popped
if (true) {
// unexpected jump
} while (this);
(this);// not popped
// unexpected jump
}
}
}
Set(&quot;\x0B\x1A\x13\x16&quot;, true);
} while (true);
</pre><p>Now you say, would this be useful for someone? No, of course!</p><p>The SWF Protector is not free but comes with a small price tag. But it’s more than worth the money you pay and better than concerning over the security of your code. You can get more information or buy this tool from <a
href="http://www.dcomsoft.com/" target="_blank">http://www.dcomsoft.com</a></p> ]]></content:encoded> <wfw:commentRss>http://ask.amoeba.co.in/how-to-protect-your-swf-as2-or-as3-prevent-swf-decompile-using-swf-protector/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>PHP &#8211; Search in an Array for Values Matching a Pattern &#8211; Regex, Wildcard</title><link>http://ask.amoeba.co.in/php-search-in-an-array-for-values-matching-a-pattern-regex-wildcard/</link> <comments>http://ask.amoeba.co.in/php-search-in-an-array-for-values-matching-a-pattern-regex-wildcard/#comments</comments> <pubDate>Wed, 07 Jul 2010 14:22:13 +0000</pubDate> <dc:creator>Aneeska</dc:creator> <category><![CDATA[PHP/MySQL]]></category> <category><![CDATA[Programming Techniques]]></category> <category><![CDATA[Regular Expression]]></category> <category><![CDATA[array]]></category> <category><![CDATA[array_search]]></category> <category><![CDATA[in_array]]></category> <category><![CDATA[pattern]]></category> <category><![CDATA[php]]></category> <category><![CDATA[preg_grep]]></category> <category><![CDATA[regex]]></category> <category><![CDATA[search in array]]></category> <guid
isPermaLink="false">http://ask.amoeba.co.in/?p=140</guid> <description><![CDATA[I have an array with many values and I need to do a search to find all the values that match a pattern. We have functions like in_array &#38; array_search in PHP, but these functions basically try to match the exact needle value in the array. I need to use my Regular Expression Pattern and [...]]]></description> <content:encoded><![CDATA[<p>I have an array with many values and I need to do a search to find all the values that match a pattern. We have functions like <strong>in_array</strong> &amp; <strong>array_search</strong> in PHP, but these functions basically try to match the exact needle value in the array. I need to use my <strong>Regular Expression Pattern</strong> and find all the array values that match the regex pattern.</p><p>The PHP function <strong>preg_grep</strong> handles this beautifully. It accepts the <strong>Regex </strong>pattern and the array to search for as its parameters. It then returns the array consisting of the elements of the input array that match the given pattern. The returned array is indexed using the keys from the input array.</p><p>Here is my array:<br
/> Array<br
/> (<br
/> [0] =&gt; Armenia<br
/> [1] =&gt; America<br
/> [2] =&gt; Algeria<br
/> [3] =&gt; India<br
/> [4] =&gt; Brazil<br
/> [5] =&gt; Croatia<br
/> [6] =&gt; Denmark<br
/> )<br
/> I want to find all the countries in the array which start with the letter &#8216;A&#8217;. We need to form a regular expression which will match all the strings starting with letter A.</p><p>I have got this simple regular expression: <strong>&#8216;/^A.*/&#8217;</strong></p><p>Now here is the PHP code to find the values from the Array.</p><pre class="brush: php; html-script: true; title: ; notranslate">
&lt;?php
$array = array('Armenia', 'America', 'Algeria', 'India', 'Brazil', 'Croatia', 'Denmark');
$fl_array = preg_grep('/^A.*/', $array);
echo '&lt;pre&gt;';
print_r($fl_array);
echo '&lt;/pre&gt;';
?&gt;
</pre><p>Which then gives you this output:<br
/> Array<br
/> (<br
/> [0] =&gt; Armenia<br
/> [1] =&gt; America<br
/> [2] =&gt; Algeria<br
/> )</p><p>Here are some Regular Expression Patterns you could use.</p><p>Find whole numbers: <strong>&#8216;/^\d+$/&#8217;</strong><br
/> Floating numbers: <strong>&#8216;/^\d+\.{1}\d+$/&#8217;</strong><br
/> Lowercase Words: <strong>&#8216;/^[a-z]+$/&#8217; </strong></p><p>Play with Regular Expressions and let me know if you have any questions or if you need more patterns. I am planning to publish an article on Regular Expressions soon.</p> ]]></content:encoded> <wfw:commentRss>http://ask.amoeba.co.in/php-search-in-an-array-for-values-matching-a-pattern-regex-wildcard/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>PHP &#8211; How to Get Browser Properties and Capabilities &#8211; get_browser()</title><link>http://ask.amoeba.co.in/php-how-to-get-browser-properties-and-capabilities-get_browser/</link> <comments>http://ask.amoeba.co.in/php-how-to-get-browser-properties-and-capabilities-get_browser/#comments</comments> <pubDate>Thu, 01 Jul 2010 12:18:15 +0000</pubDate> <dc:creator>Aneeska</dc:creator> <category><![CDATA[Enterprise Web]]></category> <category><![CDATA[PHP/MySQL]]></category> <category><![CDATA[Programming Techniques]]></category> <category><![CDATA[browscap]]></category> <category><![CDATA[browscap.ini]]></category> <category><![CDATA[browser capability]]></category> <category><![CDATA[browser properties]]></category> <category><![CDATA[gary keith]]></category> <category><![CDATA[get_browser]]></category> <category><![CDATA[http_user_agent]]></category> <category><![CDATA[ini]]></category> <category><![CDATA[ismobiledevice]]></category> <category><![CDATA[is_mobile_device]]></category> <category><![CDATA[mobile]]></category> <category><![CDATA[mobile application]]></category> <category><![CDATA[php]]></category> <category><![CDATA[php.ini]]></category> <category><![CDATA[web]]></category> <guid
isPermaLink="false">http://ask.amoeba.co.in/?p=136</guid> <description><![CDATA[In this tutorial, learn about the get_browser() PHP function and its configuration. We'll learn how to use Gary Keith's browser information database (browscap.ini)  with the PHP browscap directive.
I wanted one of my web applications to have a different appearance on mobile phones and devices. I didn't really want to set up an entirely different domain or use a sub domain particularly for mobile devices (like http://mobile.facebook.com). Instead I want to tackle the device inside my application logic so that all users access the same URL.]]></description> <content:encoded><![CDATA[<p>In this tutorial, learn about the<strong> get_browser()</strong> PHP function and its configuration. We&#8217;ll learn how to use Gary Keith&#8217;s browser information database <strong>(<a
title="A special version of browscap.ini for PHP users " rel="nofollow" href="http://browsers.garykeith.com/downloads.asp" target="_blank">browscap.ini</a>)</strong> with the <strong>PHP browscap directive</strong>.</p><p>I wanted one of my web applications to have a different appearance on mobile phones and devices. I didn&#8217;t really want to set up an entirely different domain or use a sub domain particularly for mobile devices (like http://mobile.facebook.com). Instead I want to tackle the device inside my application logic so that all users access the same URL.</p><p>PHP&#8217;s <span
style="color: #000080;"><strong>$_SERVER['HTTP_USER_AGENT']</strong></span> is cool. You can play around this variable and achieve what you want. But you don&#8217;t get all you want. Let&#8217;s look at the string output of the above variable on my browser:</p><p><strong><em><span
style="color: #993300;">Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6)  Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)</span><br
/> </em></strong></p><p>This gives you some information. I wanted an easy way to see whether the request was from a mobile device.</p><p>PHP has this function, <strong>get_browser</strong> which attempts to determine the capabilities of the  user&#8217;s browser, by looking up the browser&#8217;s information in the <strong><var>browscap.ini</var></strong> file.</p><p>In order for this to work, your <strong>browscap </strong>configuration setting  in <strong><var>php.ini</var></strong> must point to the correct location of the <var>browscap.ini</var> file on your system. <var>browscap.ini</var> is not bundled with PHP, but you may find an  up-to-date <a
title="Link : http://browsers.garykeith.com/downloads.asp" href="http://browsers.garykeith.com/downloads.asp" target="_blank">» php_browscap.ini</a> file  here. While <var>browscap.ini</var> contains information on  many browsers, it relies on user updates to keep the database current.</p><p>First, Download the browscap.ini file for PHP from the above website. There are many files and this is the one you need: <strong><a
title="Gary Keith's browscap.ini for PHP" href="http://browsers.garykeith.com/stream.asp?PHP_BrowsCapINI" target="_blank">Gary Keith&#8217;s browscap.ini for PHP</a></strong></p><p>Now, open your PHP.ini file and find for &#8220;browscap&#8221;. You will see something like this:</p><p><span
style="color: #993300;"><strong>[browscap]<br
/> ;browscap = extras\browscap.ini</strong></span></p><p>Remove the semicolon and change the path to point to the <strong>php_browscap.ini</strong> you just downloaded. Your PHP.ini directive now looks like this:</p><p><span
style="color: #993300;"><strong>[browscap]<br
/> browscap = C:\Inetpub\PHP5\extras\php_browscap.ini</strong></span></p><p>Save the PHP.ini file and restart your Web server.</p><p>Now use the get_browser function in your PHP code to get user&#8217;s browser properties and capabilities.</p><pre class="brush: php; html-script: false; title: ; notranslate">
&lt;?php
$browser = get_browser(null, true);
echo &quot;&lt;pre&gt;&quot;;
print_r($browser);
echo &quot;&lt;/pre&gt;&quot;;
?&gt;
</pre><p>This is the output I got when I accessed the script using a Computer Browser.</p><pre class="brush: php; html-script: false; title: ; notranslate">
Array
(
    [browser_name_regex] =&gt; ^mozilla/5\.0 (windows; .*; windows nt 5\.1; .*; rv:1\.9\.2.*) gecko/.* firefox/3\.6.*$
    [browser_name_pattern] =&gt; Mozilla/5.0 (Windows; *; Windows NT 5.1; *; rv:1.9.2*) Gecko/* Firefox/3.6*
    [parent] =&gt; Firefox 3.6
    [platform] =&gt; WinXP
    [win32] =&gt; 1
    [browser] =&gt; Firefox
    [version] =&gt; 3.6
    [majorver] =&gt; 3
    [minorver] =&gt; 6
    [frames] =&gt; 1
    [iframes] =&gt; 1
    [tables] =&gt; 1
    [cookies] =&gt; 1
    [javaapplets] =&gt; 1
    1 =&gt; 1
    [cssversion] =&gt; 3
    [supportscss] =&gt; 1
    [alpha] =&gt;
    [beta] =&gt;
    [win16] =&gt;
    [win64] =&gt;
    [backgroundsounds] =&gt;
    [cdf] =&gt;
    [vbscript] =&gt;
    [activexcontrols] =&gt;
    [isbanned] =&gt;
    [ismobiledevice] =&gt;
    [issyndicationreader] =&gt;
    [crawler] =&gt;
    [aol] =&gt;
    [aolversion] =&gt; 0
)
</pre><p>When I accessed the same script using a mobile phone, here&#8217;s what I got:</p><pre class="brush: php; html-script: false; title: ; notranslate">
Array
(
    [browser_name_regex] =&gt; ^.*nokia.*/.*$
    [browser_name_pattern] =&gt; *Nokia*/*
    [parent] =&gt; Nokia
    [browser] =&gt; Nokia
    [tables] =&gt; 1
    [cookies] =&gt; 1
    [ismobiledevice] =&gt; 1
    [version] =&gt; 0
    [majorver] =&gt; 0
    [minorver] =&gt; 0
    [platform] =&gt; unknown
    [frames] =&gt;
    [iframes] =&gt;
    [javaapplets] =&gt;
    1 =&gt;
    [cssversion] =&gt; 0
    [supportscss] =&gt;
    [alpha] =&gt;
    [beta] =&gt;
    [win16] =&gt;
    [win64] =&gt;
    [backgroundsounds] =&gt;
    [cdf] =&gt;
    [vbscript] =&gt;
    [activexcontrols] =&gt;
    [isbanned] =&gt;
    [ismobiledevice] =&gt;
    [issyndicationreader] =&gt;
    [crawler] =&gt;
    [aol] =&gt;
    [aolversion] =&gt; 0
)
</pre><p>I was just looking at this,</p><pre><span style="color: #993300;"><strong>[ismobiledevice] =&gt; 1</strong></span>
Interesting uh!</pre><p>If your shared hosting service doesn&#8217;t allow you to modify the PHP.INI file, you would want to use a stand alone implementation of the same concept by Jonathan. <a
href="http://code.google.com/p/phpbrowscap/">http://code.google.com/p/phpbrowscap/ </a></p> ]]></content:encoded> <wfw:commentRss>http://ask.amoeba.co.in/php-how-to-get-browser-properties-and-capabilities-get_browser/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>CakePHP &#8211; Search for records between two dates inclusively</title><link>http://ask.amoeba.co.in/cakephp-search-for-records-between-two-dates-inclusively/</link> <comments>http://ask.amoeba.co.in/cakephp-search-for-records-between-two-dates-inclusively/#comments</comments> <pubDate>Fri, 25 Jun 2010 13:45:10 +0000</pubDate> <dc:creator>Aneeska</dc:creator> <category><![CDATA[CakePHP]]></category> <category><![CDATA[PHP/MySQL]]></category> <category><![CDATA[Programming Techniques]]></category> <category><![CDATA[app import]]></category> <category><![CDATA[between]]></category> <category><![CDATA[date]]></category> <category><![CDATA[dayassql]]></category> <category><![CDATA[daysassql]]></category> <category><![CDATA[from]]></category> <category><![CDATA[helper]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[php]]></category> <category><![CDATA[query]]></category> <category><![CDATA[search]]></category> <category><![CDATA[time]]></category> <category><![CDATA[time helper]]></category> <category><![CDATA[to]]></category> <guid
isPermaLink="false">http://ask.amoeba.co.in/?p=132</guid> <description><![CDATA[Often we search for records in a database table for fetching records that were created or modified on a particular date or between two dates. We match against a date field and add multiple conditions to find records with a date field value ranging between two dates. It would look something like this: Select * [...]]]></description> <content:encoded><![CDATA[<p>Often we search for records in a database table for fetching records that were created or modified on a particular date or between two dates. We match against a date field and add multiple conditions to find records with a date field value ranging between two dates.</p><p>It would look something like this:<br
/> <code>Select * from tables where CREATE_TIME &gt;=  "2007-03-14 00:00:00<code>" and CREATE_TIME &lt;= "2007-03-16 23:59:59"</code></code></p><p>CakePHP has built-in functions to format a query like this if you have two dates, the FROM DATE and the TO DATE.</p><p>See the Example below:</p><pre class="brush: php; html-script: true; title: ; notranslate">
$from = '14-Mar-2007';
$to = '16-03-2007';
App::import('Helper', 'Time');
$time = new TimeHelper();
echo '('.$time-&gt;daysAsSql(($from)?$from:$to, ($to)?$to:$from, &quot;created&quot;).')';
</pre><p>In this example, we have two variables to store FROM and TO date limits. The function we used here is &#8220;daysAsSql&#8217; and its a method available with the Time Helper Class. Basically these helper functions are designed to use in Views. When we need to use Helper functions in  a Controller, we import the Helper using the App:import function.</p><p>After importing the Time Helper into your controller, create an instance of the Time Helper, <strong>$time = new TimeHelper();</strong></p><p>Now call this function by passing the date ranges and the field name as parameters. <strong>$time-&gt;daysAsSql(($from)?$from:$to,  ($to)?$to:$from, &#8220;date_field&#8221;)</strong><br
/> I have additional calculations here:<strong> ($from)?$from:$to</strong> &amp;<strong> ($to)?$to:$from</strong>. This makes sure that, if one of the date input values are null or not defined, it searches all the records with the date_field value on a particular day. Explained below.</p><p>The above example outputs this:</p><p>((created &gt;= &#8217;2007-03-14 00:00:00&#8242;) AND (created &lt;= &#8217;2007-03-16  23:59:59&#8242;))</p><p>Now you can use this as a Condition in your Find functions or as a Filter in your Pagination queries.</p><p>Examples:</p><pre class="brush: php; html-script: true; title: ; notranslate">
$filters = array();
$filters = '('.$time-&gt;daysAsSql(($from)?$from:$to, ($to)?$to:$from, &quot;created&quot;).')';
$records = $this-&gt;paginate('ModelName', $filters);
</pre><p>or</p><pre class="brush: php; html-script: true; title: ; notranslate">
$conditions = array();
$conditions = '('.$time-&gt;daysAsSql(($from)?$from:$to, ($to)?$to:$from,  &quot;created&quot;).')';
$this-&gt;data = $this-&gt;ModelName-&gt;find('all', array('conditions' =&amp;gt; $conditions));
</pre><p>If $from was null the query generated would look like this:<br
/> ((created &gt;= &#8217;2007-03-16 00:00:00&#8242;) AND (created &lt;= &#8217;2007-03-16   23:59:59&#8242;))</p><p>Please let me know if you have any questions.</p> ]]></content:encoded> <wfw:commentRss>http://ask.amoeba.co.in/cakephp-search-for-records-between-two-dates-inclusively/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
