JavaScript (Client/Server)

JavaScript – Executing Multiple window.onload events on a webpage

For executing multiple window.onload add this script to the head head section:

<script language=”javascript” >
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != ‘function’) {
window.onload = func
} else {
window.onload = function() {
if (oldonload) {

Add Function to window.onload event use :



addLoadEvent(function() {
/* code to run on page load *



Magento Load Related, Upsell and crosssell Products by Ajax using dynamic block creation

To load related products using Ajax just pass current product id by Ajax request and create a controller and add the following code to load the related products:

[box] //Register the Current Magento Product
Mage::register(‘product’, Mage::getModel(‘catalog/product’)->load($productId));
$block = Mage::app()->getLayout()->createBlock(‘catalog/product_list_related’)->setTemplate(‘catalog/product/list/related.phtml’);
$message .= $block->toHTml();
return $message;[/box]



Magento URL Rewrites in a Collection

To Enable Url rewrites while using a Magento collection just add this to the end of collection:


which adds URL rewrites to collection flag



Setup Pure-FTPd Daemon in Linux

Install Pure-FTPd
In a Linux shell run the following:
Ubuntu –
apt-get install pure-ftpd-common pure-ftpd

Red Hat, Centos-
yum install pure-ftpd-common pure-ftpd

Now we need to create a new system group for pureftpd:

groupadd ftpgroup

Now we add a user for the group and give that user no permission to a home directory or a shell:

useradd -g ftpgroup -d /dev/null -s /etc ftpuser

Create a new user
Lets create our first FTP user. In this example our user will be “justin”:

pure-pw useradd justin -u ftpuser -g ftpgroup -d /home/pubftp/justin -N 10

In the above command we gave him a limit of 10 MB disk space with the option “-N 10”. Now you have to enter justin’s new password twice.
By default your users will be saved in /etc/pure-ftpd/pureftpd.passwd, but first we have to update the pureftpd Database:

pure-pw mkdb

The “Database” here is simply a binary file but it is ordered and has an index for quick access.
User Information
To get some user details enter the following to get a complete list of all pureftpd users:

pure-pw list

If you want to show information about a specific user:

pure-pw show justin

This will show you detailed information about the user “justin”.
You will notice that the line “Directory: /home/pubftp/justin/./” has a trailing ./ but you shouldn’t worry as this is simply the chroot for the user, which means he can’t go “above” his directory.
Resetting a password
If you forget the password for a user, you can reset it as follows:

pure-pw passwd justin

After a password reset update your database:

pure-pw mkdb

Starting the FTP Server
To test the server let’s start it:

/usr/sbin/pure-ftpd -S,21 -c 30 -C 1 -l puredb:/etc/pureftpd.pdb -x -E -j -R

The shell will open up a new pure-ftpd session and you should be able to connect to your FTP server. Use an FTP client to test whether or not you are able to login with your user details you created.
Once you are happy close the session off:

ctrl z

Configuring Pure-FTPd
Right so by now you have created a user and been able to connect to your FTP server. We now want to setup a few things so that we can run Pure-FTPd as a daemon.
First you need to set Pure-FTPd as a standalone server:

vim /etc/default/pure-ftpd-common

Replace this:


With this:


Now we want to ensure that the standalone server checks our usernames and passwords against the pureftpd database file:

cd /etc/pure-ftpd/conf
vim PureDB

Add the following to that file (if it doesn’t exist):


Now we need to create a symbolic link to the PureDB file:

cd /etc/pure-ftpd/auth
ln -s /etc/pure-ftpd/conf/PureDB 50pure
ls -ls

You should now see a new file “50pure” linking to ../conf/PureDB.
Restart Pure-FTPd:

/etc/init.d/pure-ftpd restart



Check Database size in Mysql

Use this Query to check the Database size in Mysql:

[box] SELECT table_schema “database_name”, sum( data_length + index_length ) / 1024 / 1024 “Data Base Size in MB” FROM information_schema.TABLES GROUP BY table_schema;[/box]


Web Performance Optimization

Browsers with inbuilt Javascript Libraries

Some popular Web browsers like Chrome and Firefox now come bundled with popular libraries such as jQuery, This would offer the caching advantages of Google AJAX library without the initial download request and saves page request time. Using a rel=”loadjQuery-1.7″ attribute on a script link could force the browser to load jQuery from disk rather than downloading from a server.



Stop mod Security From Blocking Google’s Crawler?

If mod security is blocking Google’s crawler for your website you need to add this on top of your rules:

SecRule REQUEST_HEADERS:User-Agent “compatible; Googlebot” “nolog,allow”

That’s it!!



Mod Security Filter for a particular IP

Mod_security can prevent edits  and you will get the error code that is configured within mod_security(while posting data).

Here is a useful way to have mod_security ignore your home / office IP and yet stay active for all other IP addresses:

SecRule REMOTE_ADDR “^127\.0\.0\.1$” phase:1,nolog,allow,ctl:ruleEngine=Off

Simply change the to your home / office IP. If you have more than one IP to add, then simply copy the line and modify it to a second or third IP.



Magento Iphone Theme : Fix Blank Template Error

The following was missing from the top of the page.

Path: app/design/frontend/default/iphone/template/catalog/category

$_helper = $this->helper(‘catalog/output’);
$_product = $this->getProduct();



Magento – Displaying Additional Product Attributes in Catalog List

Firstly I added new method to class Mage_Catalog_Block_Product_View_Attributes:

function setProduct($productId)
if (!Mage::registry('product') && $productId) {
$this->_product = Mage::getModel('catalog/product')->load($productId);

Then I’ve added to /app/design/frontend/*/*/template/catalog/product/list.phtml this:

<table class="info">
$add = new Mage_Catalog_Block_Product_View_Attributes();
$_additional = $add->getAdditionalData();
<?php foreach ($_additional as $_data): ?>
<th><?php echo $_data['label'] ?></th>
<td><?php echo $_data['value'] ?></td>
<?php endforeach; ?>



Magento CONNECT ERROR: Can’t write to file: downloader/.cache/community/

Magento Connect in Magento 1.5 sometimes throws the following error message on attempt to install some extension (we have noticed this on BusinessDecision_Interaktingslider):

CONNECT ERROR: Can't write to file: downloader/.cache/community/BusinessDecision_Interaktingslider-1.0.6/app/code/community/BusinessDecision/Interaktingslider/sql/interaktingslider_setup/mysql4-install-1.0.6.php
You have checked all permissions and sure that the destination is writeable, yet the Magento Connect cannot create this file.
The reason is that function working with archives passing non-ASCII characters in the filename and the filename cannot be created simply due to filesystem restrictions on filenames.
The simple fix can be just patch the _writeFile function indownloader/lib/Mage/Archive/Abstract.php to strip all non-ASCII characters from the filename:
--- Abstract.php
+++ downloader/lib/Mage/Archive/Abstract.php
@@ -43,4 +43,5 @@
     protected function _writeFile($destination, $data)
+        $destination = preg_replace('/[^(\x20-\x7F)]*/','', $destination);
         if(false === file_put_contents($destination, $data)) {
             throw new Mage_Exception("Can't write to file: " . $destination);


Linux, MySql, PHP

Php Mysql installation in a linux server

Use this command on a Red Hat based linux installation to install Php Mysql completely without any issues:

yum install php php-devel php-gd php-imap php-ldap php-mysql php-odbc php-pear php-xml php-xmlrpc curl curl-devel perl-libwww-perl ImageMagick libxml2 libxml2-devel


Linux, MySql

Mysql Socket issue

Sometimes mysql gives an error while starting/opening mysql service for the socket, It happens when your mysql socket get corrupted.
To create a new socket you just need to:
Comment the socket settings in /etc/
Restart mysqld and httpd 🙂



Linux Open Port 80 (HTTP Web Server Port)

Login as root user and type:

# vi /etc/sysconfig/iptables
Append rule as follows:
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
Save and close the file. Restart iptables:
# /etc/init.d/iptables restart



Magento redirect simple product to parent configurable product

Add this code in _initProduct() method in IndexController or ProductController in /app/code/core/Catalog/Product/controllers (Create same file in your local folder), which redirects the simple product to parent configurable product.

$configurable_product = Mage::getModel(‘catalog/product_type_configurable’);
$parentIdArray = $configurable_product->getParentIdsByChild($product->getId());

$product = Mage::getModel(‘catalog/product’)->load($parentIdArray[0]);



Linux, Magento

Adding a Cron Job in Magento

Add the following lines to your module’s config.xml file under etc folder.




                <schedule><cron_expr>*/1 * * * *</cron_expr></schedule>








class Namespace_Yourmodulename_Model_Observer {

public static function methodToCall(){

//Code to Execute

Mage::log(“Cron Executed”);




Note: Magento root file cron.php needs to be added to crontab on web server.