Use/Enable a payment method for back-end/admin-end orders only


In Magento, somtimes we need to enable the payment-method only for back-end orders.payment-methods
For this you can create new module in local and overwrite the payment method model in your local folder.
Following are the steps:
Note: You have enabled the method in the configuration for single/differnt store-views.
Create a new module and extend it from the class Mage_Payment_Model_Method_MethodName.
Change the variables:
protected $_canUseInternal = true;  // use in admin
protected $_canUseCheckout = false;  // use in frontend onepage checkout
protected $_canUseForMultishipping = false; // use in frontend multishippping checkout
$_canUseInternal  this variable responsible to show/hide payment method(s) for back-end orders.
$_canUseCheckout this varible is responsible to show/hide payment method(s) for frontend orders in case of onepage checkout.
$_canUseForMultishipping is responsible to show/hide payment method(s) for frontend orders in case of multishippping checkout.

Parallel Download Link


Parallel vs Serial – fighting continues.

Note that for smaller files it doesn’t matter much. But in case of bigger files it really does.

Case 1: 4113-thumb
Lets say we have download links for songs. So the user may want to download one by one, as in this way single file download is faster and user can start listening while other are getting download.

Case 2:
Now suppose our download links provide zipped reports. And those reports will be analyzed later. Another example can be your site provide online courses. When a user going through online tutorial he may download all the resources and assignments. In these cases user may want to download all the files parallel.

Generally we use this type of code to implement download functionality.

header('Content-Type: audio/mpeg3');
header('Content-disposition: attachment; filename=' . $filename);
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);

Now if you access more than one download link, you may find that when first download is on the process others are not started. After completion of first download second one gets started and so on..

To make all download parallel use this simple trick –

session_write_close();

at the top of the method.

What it does?

According to PHP’s session handling, using the default session handler, when a PHP script opens a session it locks it. Subsequent scripts that need to access, it have to wait until the first script is finished with it and unlocks it (which happens automatically at shutdown, or by session_write_close() ). So here session_write_close() simply unlocks the session.

Hope this will help 🙂

Identify your slow Running QUERY?


We all know, it not good to keep queries which take long time to run and fetch result. But, one point also comes in mind what is the quick way to identify the Slow Running Query.

One of the good thing is MYSQL provide one way from which we can easily identify the slow running queries, and also store that slow running query in log.

“MYSQL doesn’t store SLOW running queries by default.”

images (2)
To Enable Log for Slow Queries:

Edit my.cnf (configuration file) of mysql and uncomment these line under [mysqld] block:

long_query_time         = 1  // in seconds
log_slow_queries       = /var/log/mysql/mysql-slow.log

The default long_query_time is 0, but we’ve set it to 1 here to make MySQL log all queries that take longer than 1 second to execute.

Feel free to change this value, as well as the location for the log file. You can then use the
mysqldumpslow command-line tool, included with MySQL, to get a summary of the slow-query-log file.

When you’re done, you need to restart the MySQL server for the changes in the my.cnf file to take effect

/etc/init.d/mysql restart
OR
sudo service mysql restart

And its done!

Enjoy Coding! 🙂

 

ApaChE Tips


We all know while development, sometimes we need to setup different projects in our system. For which we have to do settings (like virtual host, host setup,…) to run it in our local environment.

Here, I am sharing some tips for Apache that might be helpful for doing setup of different projects.

Setup a Virtual Domain

NameVirtualHost *
<VirtualHost *>
DocumentRoot /web/example.com/www
ServerName www.example.com
ServerAlias example.com CustomLog /web/example.com/logs/access.log combined ErrorLog /web/example.com/logs/error.log
</VirtualHost> 

Include another conf file

Include /etc/apache/virtual-hosts/*.conf

Hide Apache Version Info

ServerSignature Off
ServerTokens Prod

Only allow Access from a specific IP

Order Deny,Allow
Deny from all
Allow from 127.0.0.1

Only allow access from your subnet

Order Deny,Allow
Deny from all
Allow from 176.16.0.0/16

Add a directory index

DirectoryIndex index.cfm index.cfm

Turn OFF directory browsing

Options -Indexes

Turn ON directory browsing

<Location /images>
  Options +Indexes
</Location>

Enjoy Coding! 🙂

How ‘==’ and ‘===’ different from EaCh OtHeR?


It quite easy. It’s very general we use these operators in our code in many places while matching some important conditions, or in some business logics, password comparisons, etc. But, sometime this make a big problem if we do wrong choice between operator ‘==’ or ‘===’

If your data is from a secure location or already validated, you may not need the strict identical === operator, but if your data is from an outside source (HTTP GET/POST or user entered), you may want to be more precise depending on the situation

Basic point:
1.) == (Equal) for Loose Operations.
2.) === (Identical) for Exact Matches.

Let’s get more into this.

For Loosely Equal (==) in PHP

    Null Usage:                              String Comparisons to 0:
    “” == null     => True                “abc” == 0     => True
1 == null     => False                “str” == 1     => False
null == null     => True             “str” == “0”     => False
null == 0        => True              “strA” == “strB”     => False
null == “0”    => False
null == false    => True
Type juggling converts strings to numbers and viceversa:
    3 == 4          => False
3″ == 3.0     => True
1 == “1”         => True

For Identical Equal (===) in PHP
It removes the Loose evaluations. Identical operator is great whenever you expect both sides of the comparison to have the same type and it will help keep you out of trouble. The output will be more likely what you expect.

Comparisons to Zero                 String Comparisons
0 === “”         => False                “abc” === 0         => False
“0” === false     => False             “abc” === 1         => False
“0” === 0     => False                   “abc” === “0”         => False
0 === false     => False                “abcA” === “abcB”    => False
Null Usage                                   Numeric Comparisons
“” === null     => False                3 === 4         => False
1 === null     => False                 “3” === 3.0     => False
null === null     => True              1 === “1”     => False
null === null     => True
null === 0     => False
null === “0”     => False
null === false     => False

It’s very basic tip. But, if not taken care properly will take you to big mistake. So, need to take care while choosing between loosely equal and identical.

Enjoy Coding! 🙂

Reference:http://www.codeproject.com/Tips/819645/Pitfalls-of-PHPs-Comparison-Operators-When-to-Us

Setting Default controller for Different Environments in Zend


Sometimes, we create different environment in which we define some default module, controller and action.
So, here the point we need to remember is:

If you do not have modular structure then setting folowing to application.ini should work

resources.frontController.defaultControllerName = “site”
resources.frontController.defaultAction = “action”
resources.frontController.defaultModule = “module”

If you have modular structure then add following

resources.frontController.defaultControllerName = “site”
resources.frontController.defaultAction = “actionName”
resources.frontController.defaultModule = “moduleName”
resources.frontController.params.prefixDefaultModule = “1” // this is imp.
resources.frontController.moduleDirectory = APPLICATION_PATH “/modules”

resources.frontController.params.prefixDefaultModule = “1”
This allows you to re-purpose an existing module to be the default module for an application.
🙂