Category: PHP

Book Review: Magento Site Performance Optimization

Magento Site Performance Optimization book coverMagento Site Performance Optimization by Mathieu Nayrolles is a handy little primer on the basics of optimizing a server for magento. This little gem covers the basics, doesn’t spend a lot of time on fluff, and moves quickly into optimizing MySQL and covers the basics of using two mysql instances (through replication) one for writing and another for reading.

Chapter 1: Starting with the Right Hardware covers the basics of choosing what hardware is necessary, and how to decide who is a good host. I found the section of choosing a host to be very light because it basically suggests finding a magento recommended host and finding somebody located in the same country. It didn’t really cover much in the way of latency, dns, etc. The section on Content Delivery Networks left a lot to be desired, and really should have looked at some of the avaialble plugins.

Chapter 2: Choosing the Best Web Server is about some of the different options available out there for serving websites on linux. Makes perfect sense to cover the normal Apache and lighttpd. The Apache section discusses the standard .htaccess modifications, compiling Apache with some of the modules, and disabling what you aren’t using. Overall, the Apache section was good for a basic primer. I’m interested in using lightttpd more after reading a bit about it.

Surprisingly, Mathieu also covers Nginx which turns out to be much better for hosting magento once it’s properly tuned. This chapter was one of the most interesting and useful to me. I haven’t ever really looked at the numbers in the way he presented them or decided to try and load test configuration settings as I made each one.

Chapter 3: Tuning, Scaling, and Replicating MySQL covered exactly like the title says. This chapter discussed a bit about some of the different scripts available, but the real gem was the discussion about replication and how to configure it in magento. I didn’t realize I could setup two mysql instances and have magento read and write to different ones.

Chapter 4: Caching Them All starts off with one of the first things I try to do with caching. Turn off most of it, and see how much it changes performance. On nearly every system, I’ve worked on the performance has actually improved because writing to files and reading the files for differences is usually more expensive than just pulling the data again from the database.

Mathieu presents a reall interesting approach, that again I hadn’t really thought of which is using the RAM hold /var/cache which seems like it could be a significant improvement for the caching since most sites are using SCSI drives and not SSD like his examples.

The different caching technologies covered aren’t covered in great detail, and don’t really have that same level of details as Chapter 2 which was pretty disappointing. I had hoped we would spend some more time and energy discussing full page caching, which I find to one of the greatest optimizations to be using full page caching.

The real beauty of Chapter 4 is the section on HipHop Virtual Machine (HHVM) which is something I’ve actually never heard of before. I can’t wait to try it in the next few months and test it on on my site. HHVM is open source and produced by Facebook.

Overall, I found the book to be detailed, easy to follow and full of useful information. Also fantastic is the fact there aren’t twenty pages devoted to installing magento; the book is free of fluff and really introductory details. I also felt that it really left a lot to be desired too. Hopefully, in the next version they can spend a bit more time discussing hosts,content delivery networks and some of the code enhancements that can be done to improve speed.

Reducing WordPress Comment Spam

Self-hosted WordPress sites often attract a lot of comment spam which requires your valuable time or resources to reduce to more manageable levels.

  • We recommend automatically turning off commenting on older posts, because these are the posts that often rank very well in Google which attracts the spammers in the first place. Most blogs choose to turn commenting off on posts that are three months old or older. (We usually do six months.)By turning off commenting on older posts, our spam dropped approximately 25% and it didn’t cause our WordPress site to load any slower.
  • Akismetis possibly the most effective method of reducing spam through a WordPress Plugin. Akismet works by connecting to a hosted web service to determine if comments or pingbacks are spam or legitimate.By using Akismet, we reduced the amount of comment spam by nearly 100%, but Akismet unfortunately isn’t free any more for business users.
  • Disqus is an amazing WordPress Plugin that is used for commenting. Disqus uses a hosted web service to moderate comments and automatically connects to an extensive blacklist and whitelist to reduce the amount of spam comments automatically.
  • Bad Behavior is one other WordPress plugin we use to reduce spam by not allowing the spammers to access our site in the first place. Bad Behavior works by analyzing the way the browser/program/user is using the blog and blocking them, and of course Bad Behavior also has an extensive database of common spammer Ip addresses that it also uses to block.

What is your preferred method for reducing comment spam? We believe that there’s no one technique that will completely eliminate spam, but these techniques together will help you better use your resources.

Akismet (Anti-Spam)

Akismet has worked extremely well for me over the last two years, it’s only inaccurately identified two comments as spam out of almost 20,000 comments. Akismet is pretty much the only plugin used on every WordPress blog, and works for almost everyone although it is no longer free for business use.

There’s been reports over the last year or so of comments missing for some people, but I haven’t ever experienced this or heard from anyone I know of comments disappearing.

Although, extremely good Akismet won’t always solve all spam problems especially on sites with a lot of traffic. I usually recommend using a capatcha with Akismet.

What do you recommend or use?

Zen Coding

When I first heard of Zen Coding on Sitepoint, I figured it was some other gimmick that might save me about 0.01% of my time each year after I spend five or six hours learning the syntax.

Zen Coding works by expanding some abbreviations into properly closed html. The syntax is very simple, and can be easily explained in just a few examples. The syntax is basically CSS selectors which most web designers / web developers should already know.

Overall, I found installing and using Zen Coding to be very worthwhile. I just made sure to change the shortcut key in Notepad++ because Ctrl + E wasn’t doing it for me. 🙂

Ports exist for a lot of different editors, and IDEs, so check it out. What other productivity tools do you use?

Why I Recommend PayPal

I spend a significant amount of my timing deal with ecommerce and different shopping systems. One of my all time favourite solutions is using PayPal and Magento together because they have been designed from the ground up to interact well together.

International Acceptance
PayPal is accepted in many countries and is capable of doing automatic currency conversion.

Integration
PayPal is very easily integrated in all open source shopping carts, and is often freely available requiring very little work for developers.

No Monthly Fees
Many of the other merchant systems require monthly payments even when your business is closed. PayPal charges a per transaction fee which all of the merchants also charge! If you’re accepting debit the transaction fee could be fairly expensive, but usually it is much cheaper for credit card transactions.

Lots of Payment Options
PayPal does e-checks, American Express, Visa, MasterCard, and Discover. PayPal is expected to release applications for smartphones, and the iPad very soon which will allow you to use your device as a wireless terminal.

Secure
You won’t need to spend time worrying about PCI compliance, because the credit card transaction is actually stored and processed on PayPal’s end. Money is usually transferred immediately for credit card transactions.

There are some other shopping solutions available, but don’t expect them to work as well or be as affordable PayPal.

Hope this helps!

WAMP Server Not Accessible on Port 80

WAMP Server generally runs fine without any problems, although sometimes we have to make some configuration changes because of other software monitoring ports. Skype, for example, is commonly using port 80 to communicate and stopping apache from responding.

If you’re using WAMP and apache at the same time, you need to left click the WAMP Manager icon in the system tray and go to apache -> httpd.conf and look for a line that says: Listen 80 to instead use a different port like: Listen 8080.

Hope this helps!

WAMP Server On Windows 7 64bit

Getting WAMP to run properly on Windows 7 isn’t very difficult as long as the permissions are properly set. Also, keep in mind that you may need to modify your windows firewall settings, and bit defender if you have it running.

WAMP needs full administrator privileges which as you may be aware needs to be set in UAC. The easiest way to change this is to go into the C:wamp folder and right click wampmanager.exe and go to Properties and then Compatibility.

Check the “Run as Administrator” checkbox and then press Apply and Okay. You may unfortunately have to confirm each time your machine starts that it should have these privileges.

Magento CheckOut Page Blank

I had a very strange issue with a client’s installation of magento a few nights ago. The checkout page was completely blank and the apache error logs were completely empty.

I tried all the usual fixes of looking at file permissions, updating the magento installation, checking for new modules, etc but none of these solutions solved the problem. Eventually, someone on the forums suggested to me that maybe I should see if Compilation was turned on. I completely forgot that I had turned on System Compilation to increase the performance of the site.

After, i went into ystem → Tools → Compilation and disabled it, the checkout page started working again and customers could continue to order.

PEAR Mail – Checking If Mail Sent

PEAR Mail & mail_mime is absolutely fantastic for sending multi-part mime emails and requires absolutely no effort. Probably, the best part of PEAR Mail & mail_mime is not having to deal with mime boundaries.

I often see incorrect code for handling whether the message successfully sent or not. To check if the message is sent we should do something like the following.

$isSent = $mail->send(...);
if (PEAR::isError($isSent)) {
// error handling goes here
} else {
// successfully sent code goes here.
}

If we simply use the following code, we’ll always have a true condition because $isSent won’t be set to null.

$isSent = $mail->send(...);
if ($isSent) {
// successfully sent code goes here.
//Unfortunately this will always be called
} else {
//code here will never actually be executed.
}

Hope this helps!

Installing PEAR on BlueHost

PEAR as I’m sure you’re aware stands for “PHP Extension and Application Repository” and is one of the leading frameworks for reusable components.

Installing BlueHost can be easily be accomplished using the Fantastico located inside your Cpanel access. The configuration of PEAR can be quite a challenge though!

The PEAR installation as I’m sure you’ve seen already is extremely simple. Configuration will require modifying your php.ini file. If you don’t have a php.ini file in /public_html you will need to follow the following steps.

  1. Log into Cpanel. Click PHP Config
  2. Click Install PHP.INI Master File

Now that you have a php.ini file that we can modify you will need to find the line include_path. In my php.ini file it is line 503. Change the include_path to the following making not that you will need to replace the # number with your potential number, and {{YOURBLUEHOSTUSERNAME}} should be your bluehost username without the domain name.

If you don’t know these details you can find them under the main page of Cpanel under Stats, Home Directory.


include_path = ".:/usr/lib64/php:/usr/lib/php:/home#/{{YOURBLUEHOSTUSERNAME}}/php"

Some of the core PEAR packages aren’t always installed. For example, I’ve had to go back into the PEAR Config form and search for Mail and install it.

Hope this helps!

Add A Facebook Like Button

A while ago, Facebook created the Facebook Like Button that works by using an iframe of FBML. The Like button allows you to easily get some free social media coverage with consumers.

Many websites and blogs are making use of the Facebook Like button and any other ways possible of sharing on social media networks because the cost is so minimal and publicity is so important to any business.

Adding the button is extremely simple, although it does require some editing of .phtml files, an ftp client, or experience with VI or EMACS if you have SSH access.

Step 1: Open up app/design/frontend/default/{{YOURTHEMENAMEHERE}}/template/catalog/product/view.phtml

Step 2: Search for the following code.

getReviewsSummaryHtml($_product, false, true)?>
getChildHtml('alert_urls') ?>

Step 3: Paste this code between the two lines of above code.

<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo $_product->getProductUrl(); ?>&amp;layout=button_count&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>

Step 4: Open up your website and view a product. If you can’t see the Facebook Like button you may need to clear the Magento Cache.

Hope this helps and good luck with your business!

How to Tell What Version of Magento

The easiest way of telling what version of Magento you are running is by viewing file app/Mage.php and looking for a function called getVersionInfo().

The function should look something like the following and be fairly self explanatory to most developers.


public static function getVersionInfo()
{
return array(
'major' => '1',
'minor' => '5',
'revision' => '0',
'patch' => '1',
'stability' => '',
'number' => '',
);
}

Hope this helps!

Magento An Introduction

What is Magento?
Magento is an open source eCommerce platform with two versions: Magento Community Editon, and Magento Enterprise Edition. Magento is developed by Magento Inc and is built upon PHP, and the Zend Framework.

The Magento Community Edition uses the Open Software License (OSL) v3.0 which is sufficient for most online stores.

Why use Magento?
Magento is extremely flexible, and requires very minimal customization to have a fully functioning store. In addition, Magento has incredible search engine optimization built into from the start which allow you to quickly start marketing to web users and get noticed by search engines. For a list of all of the awesome features, you should check out the official Magento Features.

Why do I recommend Magento?
I recommend Magento because of the ease of customization, amazing flexibility, and the quickly growing library of extensions which are often free or very low cost. We offer services for Magento, and aren’t afraid to help customers migrate from oscommerce or some other open source system.

What do I need for Magento?
For Magento to run you need MySQL, PHP, and good quality web hosting. We don’t recommend shared hosting to any of our clients, and think you shouldn’t use it either!

How to Add CSS & JavaScript to a Magento CMS Page

Magento is an insanely flexible system that has a lot of potential, but of course with this flexibility comes a lot of learning.

Adding a JavaScript file and a CSS file to a specific CMS page isn’t very difficult. We should always consider adding the JavaScript file or CSS file to the theme if we’re going to use it on more than one specific page. Continue reading

Displaying The Newsletter On a CMS Page/Block

I really like magento, and a lot of the built in features although it can be difficult sometimes to figure out how to use things. Displaying the newsletter in a CMS Page / Block is pretty easy.

Just simply paste the following code into the design block, and away you go!

{{block type="newsletter/subscribe" template="newsletter/subscribe.phtml"}}

Hope this helps!

Professional PHP Design Patterns

Professional PHP Design Patterns written by Aaron Saray, coming in at around 250 pages is a very short and concise book. Unfortunately, the book doesn’t cover all of common design patterns but does do an adequate job of covering about 17 different patterns.

Each of the 17 patterns covered include fairly easy to understand examples, solutions, and a few pages of very easy to understand text regarding when to use the pattern and best practices for using the pattern. The pattern examples are clearly not just able to be dropped into your project, but each example could be easily adapted for most situations. Saray, has tried to be consistant and use a CD Store as an example and dsicusses how the code for its ecommerce solution would change as the company grows and offers different products.

I’m a little disappointed that there’s a few patterns that aren’t covered that I feel are so important: Chain Of Responsibility, and bridge pattern. Patterns are covered for 125 of the pages, and I really felt the book should be much larger to really cover more of the patterns.

The 20th chapter of the book is titled “Requirements Analysis” and covers an example situation that occurs before software development should happen. Saray throws out a few tips that I would consider best practices, and a generally good method of determing the requirements although I really felt that the example that takes over the next few chapters could have been much much better. Chapter 21 is an excellent addition to the book, because Saray takes the time and explains how he would do things by using UML diagrams and a lot of text before the programming happens. Saray really uses chapter 21 to mention all of the patterns he will use before he starts developing the application in chapter 22 and 23.

I wouldn’t pay full price for the book, and thankfully was able to find the book for a deep discount at a store in Buffalo. Over all, I like the book and felt I learned some pretty valuable things but I felt that in a few ways more time could have been spent on the patterns.

Magento Add/Edit Footer Links

Editing the footer links in magento is actually pretty easy. By default, there should be a static block called footer_links.

The static block starts off with at least these two links: About Us, Customer Service. Editing is as simple as signing into the admin, and Clicking CMS->Static Blocks->footer_links and making your changes.

Good luck!

Magento – Stuck in Maintenance Mode

After upgrade magento to 1.5.0.1, my client’s website was stuck in magento maintenance mode. I wasn’t able to easily find a way to fix it by looking in the normal database tables, but did locate a file called “maintenance.flag” in the magento root.

So, to fix maintenance mode look for the maintenance flag and remove it and your shop will be back online!

PHP MS SQL Truncated VarChar(255)

I don’t often have fields much longer than 256 char, but a web application I am working on makes use of varchar 512. I’m not really able to change the field to text as the PHP Docs suggest. MS SQL doesn’t allow the equal operator, and grouping on text fields, and it’s also very slow compared to a varchar which can make advanced queries very difficult. “Due to a limitation in the underlying API used by PHP (MS DBLib C API), the length of VARCHAR fields is limited to 255”

Basically, it appears that PHP is using some old C API from Microsoft and to get around this need to convert from varchar to text. Text by default, allows about 8,000 characters but changing this fairly simple in the php.ini


mssql.textlimit = 65536
mssql.textsize = 65536

Of course, you can also set it dynamically using ini_set.

ini_set("mssql.textlimit", '65536' );
ini_set("mssql.textsize", '65536' );

Hope this helps.

Resolving Session Not Saving

Tonight, I’ve encountered a very strange situation where the session data was empty after each page reload. Have you ever had an issue with the session data not being set after a page refresh and the session has been for sure been started using session_start() ?

I modified the php.ini file located on the server for the line containing session.save_path to a temporary directory on the server.

session.save_path = /tmp

After making the change, sessions were now available and working again.