Detecting if the cookies are enabled with PHP

For my current project I need a detection if the cookies are enabled or not in the user’s browser.

The easiest way to do this is by using this code:

setcookie('test', 1, time()+3600);
if(count($_COOKIE) > 0){
    echo "Cookies are yummy!";
} else {
    echo "You didn't bring any cookies here. We are hungry!";

The CakePHP way is almost similar:

$this->Cookie->write('test', 1);
if(count($_COOKIE) > 0){
    echo "Cookies are yummy!";
} else {
    echo "You didn't bring any cookies here. We are hungry!";

Ok, it’s not rocket science, but it helps 🙂

How to secure selectively admin location

Securing the admin part of the site is always tricky, especially for open source software CMS. A very good practice is to protect your admin directory with an extra .htaccess, because most of the damages has been done from exploits of the applications rather than brute force cracking.

There are many articles how to set up .htaccess authentication on Apache web server, but here I will explain how to set an extra password request only for Internet users while the users from Local Network should spare the extra password.

The scenario:
When the user in in the local network the administration part need to be accessible with the CMS default authentication, while if the user access the Administration from Internet, an extra password prompt will be shown.

1. Create your password file

# htpasswd -c /your_secret_location/.htpassword user
New password:
Re-type new password:
Adding password for user user

If you writing in an existing file don’t use the option -c

2. Setting the .htaccess
Depending of the server set up you can do this in your httpd.conf, or in apache2 style – in sites-available directory. So, let’s say we have a file in /etc/apache/sites-available called which holds the record of the domain

<VirtualHost *:80>
        <Location /admin>
                 AuthType Basic
                 AuthName intranet
                 Satisfy any
                 Allow from 192.168
                 Order allow,deny
                 AuthUserFile /your_secret_location/.htpasswd
                 Require valid-user

Here is the tricky part: The directive “Satisfy any”. By default the directive is set to all so it’s like AND:
If (you are in local net AND you are valid user) {access the location}

while Satisfy any is like OR
If (you are in local net OR you are valid user) {access the location}

More on this topic: Satisfy directive

Adding notification when new order arrives. Magento

It’s very common practice the administrators to get notification mail when a new order is placed on the shop. Here is how to activate this feature in Magento for free (there is paid Admin Notification Module as well):

Log in into admin area, then go to System->Configuration menu. Scroll down to Sales section and click on the Sales Emails and select the first section Orders. Enter your admin e-mail in the field “Send Order Email Copy To”.

And here comes the tricky one 🙂 Select the option “Separate email” from the “Send Order Email Copy Method” field. I don’t know why, but if it’s selected “Bcc”, the e-mail to the admin wasn’t send.

It’s possible that this is fixed in the versions above 1.2.x, but I haven’t tested.

hope this helps someone.

Web based CakePHP Project generation tool

Yesterday I found a tool which could be handy for rookie CakePHP developers. Atlas is a web code generator based in CakePHP. As a glance it’s a web alternative of CakePHP bake console which is a nice idea indeed.

My first impression was that tool is like diamond in the mud and I was wondering how I didn’t hear about this tool till now, but after installing I realized that it’s not so good as it looked like.


The first problem occurs in the generation of my first project – I’ve got a error message in Spanish which I couldn’t understand. I had to use Google translate for it, but after the translation, the message still doesn’t say much – “Failed to generate project skeleton”.

After debugging the code figuring out why this error means, I found that the application uses backslash as directory separator (\) ?!? and of course this lead to directories and files with strange names like /var/www/ctest\app\controllers directories where ctest\app\controllers was the full name of the directory :). I am on Ubuntu Desktop machine where backslash is not a directory separator.

Pros and Cons about this tool:


  • Very nice interface – really big point for Atlas
  • It should work properly on windows environment and based on the features list and screenshots it quite intuitive.
  • I like web based development everything is in your web browser, so another big plus.


  • Not working as expected – the backslash problem described above
  • If you are not Spanish speaking person – you are in the middle of nowhere when you see error like this: Error: no se ha podido generar el esqueleto del proyecto
  • No documentation at all. I had to look in the code in order to get idea why I cannot create a project – Not clear labels (or at least no hints). Probably a hint like: Directory (enter a full path to the empty directory under htdocs) or similar could help a lot.

The conclusion:
Atlas looking like promising tool, still it’s far from the final version, but it’s worth to try it in Windows environment. 🙂 I definitely will follow that project in the future, so hopefully I could use it one day 🙂

Magento Beginner’s Guide. PacktPub. Book

Magento Beginner's guideFew weeks ago I’ve been contacted from Packt Publishing in order to review their new book Magento Beginners Guide and because I had experience with that e-commerce platform, I decided to accept the offer.


Magento Beginners Guide is a new book that covers the process of building an online store using the Magento e-commerce solution. It’s written by William Rice and is focused on the main key features of Magento to setup a unique on-line store and customize its appearance with the help of examples.

This book is for anyone who wants to create an online store using Magento. If you are a non-technical person and are discouraged by the complexity of this powerful e-commerce application, this book is ideal for you.

About Magento

Magento is the world’s most evolved e-commerce solution which runs on the Apache/MySQL/PHP platform. From one installation, you can control multiple multilingual storefronts, all sharing customer and product information. Magento’s templates, themes and extensions gives the possibility to create an unique and powerful solution for e-commerce.

In detail

The book provides a step-by-step approach to building a simple and effective on-line store. It covers the key features of Magento that will help you get your store up and running. It guides you through the installation, configuration, adding categories and product attributes, filling your store with products from various types, payment configuration, maintaining relationships with your customers, and fulfilling orders.

Readers will be able to present and sell products in groups, sets, they can offer discounts based on quantities along with accepting various payments such as PayPal, CC, Checks, Bank transfers, Payment on delivery and many other payment gateways. Along with connecting to shippers such as UPS, FedEx, and USPS they will learn to apply tax rules to different shipping addresses and different types of products.

More on what you will learn from the book

  • Install and configure Magento and add products in the store;
  • Create categories and attributes to build your catalog of products;
  • Enhance your products with descriptions, images, and inventory information;
  • Create and apply tax rules to different product types and different shipment addresses;
  • Present and sell products in groups and sets;
  • Display products related to the one that is being viewed by a customer;
  • Offer your customer choices for a product’s size, color, or other attribute and give discounts based on quantities;
  • Accept payments using Paypal, credit cards, and checks/money orders and offer a variety of shipping options;
  • Create your own, customized shipping rates and connect to shippers such as UPS, FedEx, and USPS;

The beginner’s guide approach

  • Clear step-by-step instructions for the most useful tasks
  • Learn by doing – start working wight away
  • Leave out the boring bits
  • Inspiring, realistic examples give you ideas for your own work
  • Tasks and challenges to encourage experimentation

The publisher kindly provide two chapters from the book, so you can get a clue of the style in it.

How to buy that book?

Magento beginner's guideIf you like the book you can order it from here:
Magento: Beginner’s Guide
€27.89 save 10%

How to use aggregation SQL functions with CakePHP

Have you ever wondering how to find MAX, MIN or AVG of a database table column in CakePHP way? Well I haven’t since yesterday. The solution is quite simple and obvious.

Lets say you want to show the average views per post (in the example we have the views field in every post).
The SQL should be something like this:

SELECT AVG(views) FROM posts

Instead of using query() function you can use the CakePHP way:

$this->Post->find('first', array('fields'=>array('AVG(views) as avg_views')));

Hope this will help someone.

New plug-in for spam protection

Today I found quite nice plug-in for WordPress pretending that it will block at least 90% of the spam attempts. Quite ambitious indeed, but the time will show.

The general idea is that this plug-in adds a stylesheet to your blog’s html source code. When a browser loads that stylesheet a cookie is dropped. If that user then leaves a comment the cookie is checked. If it doesn’t exist the comment is marked as spam.

Installation is just plug-activate-play. Let’s see what will happen.

The plug-in details page: Cookies for Comments
The authors page: Donncha O Caoimh

JSON and CakePHP

Last few articles in this blog are only for stupid problems and unfortunately this one will be the same.

I had to use JSON to update some chained select boxes. I am using one jQuery plugin for this. The problem is that if you code, most likely your debug setting in the core.php file is set to 1 (or 2). This way the developer can see errors from CakePHP.

When the level is set to 1, at the end of the page, there is an comment, displaying the processing time for the page, but when you use JSON, this comment causes troubles (probably, because JSON doesn’t accept this type of comments) and the JS fail to process the response.

In order to check JSON, I needed to disable set debug level to 0 and after this to reset it again to 1 (for debugging purposes in other parts of the application).

So, the conclusion: bear in mind, that JSON doesn’t like HTML comments.

Hope this helps.