Blog

How to configure and setup WHMCS Licensing addon

I get a lot of questions in regards to how to use the WHMCS licensing addon which is why I wrote a post a while back which described how to get the code working. That guide is outdated and did not show any code which is why I want to write a new guide with a little bit of code, but not enough to disclose how the whmcs licensing addon works.

When you purchase the WHMCS licensing addon you will receive several files but you will only have to worry about one for this guide which is the check_sample_code.php file. This is an example by WHMCS to show you how the code should be implemented in your script, plugin or theme. I will now walk through the example file and show you which parts are important and what they do.

inside the function check_license() we have the following code.

$whmcsurl = "http://www.yourdomain.com/whmcs/";
    $licensing_secret_key = "abc123";

These almost explain themselves and WHMCS even has some commenting to explain how to modify them. The first variable $whmcsurl should be equal to your whmcs installation and the other variable $licensing_secret_key is your secret key that you use when creating the product that you will be licensing.

those are all the changes you should make to the check_license() function. You could basically copy the check license function (all of it) and paste it into your script, plugin, or theme. The header file is always a good place to either have the code or include the file which contains the code for the function check_license(). Check php’s include function for more information on how to do this.

Now, let’s go past the check_license() function and move down to $licensekey, this is very important and should always be equal to the license you generate in WHMCS. This is the key used to check if a license is valid or not. Next is the local key ($localkey), don’t worry about this, its a local key used for logging and you could change it up if you would like, but i wouldn’t touch it.

This line actually calls the function that will talk to your WHMCS and check if the license is valid or not.

$results = check_license($licensekey,$localkey);

You need to include this in your php file after the $licensekey and $localkey are defined. If you do not include this function then your script won’t check if the license is valid.

Now the results of the function check_license() is stored in the variable $results as an array. The status of the license will be stored in this key.

$results["status"]

We can now check if the license was valid using the included else if statement WHMCS uses.

if ($results["status"]=="Active") {
    # Allow Script to Run
    if ($results["localkey"]) {
        # Save Updated Local Key to DB or File
        $localkeydata = $results["localkey"];
    }
} elseif ($results["status"]=="Invalid") {
    # Show Invalid Message
} elseif ($results["status"]=="Expired") {
    # Show Expired Message
} elseif ($results["status"]=="Suspended") {
    # Show Suspended Message
}

This is very simple but can get confusing for some so I will make a quick example of my own script which I want to use the license check on.

So here is my script which I want to license.


plus

It simply takes two numbers and adds them. Now I want to show my script only if the license is valid, so I can add an if statement to make this check. I also need to include the check_license() function which I added in a file called licensefunction.php.


plus

Now the script will only show if there is a valid license otherwise the page will be blank, but I want to show an error if the license is invalid so that the user doesn’t get confused.


plus

This will echo the error if the license was not valid, I could of course add different errors for an invalid, suspended, or expired license but this will be ok for my script, the user will know something is wrong.

For larger scripts I could terminate the loading of a page by simply adding a die() function at the top.


plus
?>

Now the script will stop loading at the die() function if the license is not active, so none of the code below the die statement will be presented. If the license is active the die function will never be called and the script will continue to run normally.

To make your licensed script, plugin, or theme even more secure I would recommend using something like IonCube loader which WHMCS uses on their own encrypted files.

Improve the search function in Magento

Earlier I wrote about how you can change Magento so that all the products that are sold out show up last. Here is another thing that can be improved in Magento. The search function shows by default the most relevant results last. And if you have multiple search terms it tries to find a match one at a time. We can change this fairly easily with a small bit of code.

Change the file /catalogsearch/form.mini.phtml in your theme

In that file is a html form. Add the following code anywhere between the form tags


This changes the search so that our most relevant results show first.

Then make a copy of app/code/core/Mage/CatalogSearch/Model/resource/Fulltext.php

And place it here app/code/local/Mage/CatalogSearch/Model/resource/Fulltext.php

On line 356 is the following code

$likeCond = '(' . join(' OR ', $like) . ')';

Change it to the following

$likeCond = '(' . join(' AND ', $like) . ')';

Then on line 378 find

$where .= ($where ? ' OR ' : '') . $likeCond;

And change it to

$where .= ($where ? ' AND ' : '') . $likeCond;

That’s it, now your search results should be much more relevant! Let me know if you have any questions.

Show products no longer in stock last in Magento

One big problem with Magento is that when products become out of stock they still show up very high in the product listings and this can get very annoying for some users if there are several products out of stock. This very easy code fix will automatically make products that are out of stock show up last in all your categories and search results.

Go to the following folder in Magento where you will find List.php

/app/code/core/Mage/Catalog/Block/Product/

If you don’t want to change the core files, which is always a bad habbit then copy the file to /app/code/local/Mage/Catalog/Block/Product/

Find the following around line 86.

$this->_productCollection = $layer->getProductCollection();

And change it to

$this->_productCollection = $layer->getProductCollection()->joinField('inventory_in_stock', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id','is_in_stock>=0', 'left')->setOrder('inventory_in_stock','desc');

There you go, now it’s done, that’s all you have to do to make it work. Feel free to ask me any questions you may have.

Creating a Tag Cloud with PHP

I have previously uploaded this code to SourceCodeDB.com
http://sourcecodedb.com/Create-a-tag-cloud-with-php.html

I am using and modifying code originally from Steven York, he has written most of the code in this example. I had to modify pieces of his code due to the fact that the tags stored on sourcecodedb.com are strings like this “C#, loop, code” instead of storing each tag as its own word. Below is the code I am using with plenty of comments to explain what I am doing.


            if($res)
            {
                //build an output array, with the tag-name and the number of results
                $output[$i]['tag'] = $name;
                $output[$i]['num'] = $res['totalnum'];
            }
            }
            $i++;

        }  

        /*this is just calling another function that does a similar SQL statement, but returns how many pieces of content I have*/
        $total_tuts = 50;  //I set this to 50 in the example, we could use a sql query to get the actual nummber of codes

        //ugh, XHTML in PHP?  Slap my hands - this isnt best practice, but I was obviously feeling lazy
        $html = '
    '; //iterate through each item in the $output array (created above) foreach($output as $tag) { //get the number-of-tag-occurances as a percentage of the overall number $ratio = (100 / $total_tuts) * $tag['num']; //round the number to the nearest 10 $ratio = round($ratio,-1); /*append that classname onto the list-item, so if the result was 20%, it comes out as cloud-20*/ $html.= '
  • '.$tag['tag'].'
  • '; } //close the UL $html.= '
'; return $html; } $gettags = mysql_query("SELECT Tags FROM codetable WHERE Moderated='1' AND Published='1'"); //We get the tags which are written like: php, code, at, sourcecodedb while ($thetags = mysql_fetch_array($gettags)) { if ($thetags['Tags'] != null) { // We dont want any empty tags of course $newtag[] = $thetags['Tags']; // Each group of tags is put in the array } } foreach ($newtag as $thetag) { $tags[] = explode(", ", $thetag); // Each group is seperated into individual tags and put into a new array } function array_flatten_recursive($array) { if (!$array) return false; $flat = array(); $RII = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); foreach ($RII as $value) $flat[] = $value; return $flat; } $finaltag = array_flatten_recursive($tags); //We flatten the array, dont want it to be multidimensional $finaltag2 = array_unique($finaltag); //We remove any duplicate tags we have in our array echo createTagCloud($finaltag2); //Then the modified createTagCloud function is called ?>

To show the tag cloud in the footer or anywhere else on your php page simply include the file with the code above into your file using PHP’s include functions.

Feel free to comment if you have any questions!

How to make a good Facebook Timeline cover / picture

Download: Facebook Timeline Cover PSD

To make a Facebook Timeline picture you really don’t need a lot, just a good image that you can upload on your timeline page, however if you would like to get a bit more creative and create something more like these inspiring Timeline pictures: http://www.hongkiat.com/blog/creative-facebook-timeline-covers/

I have attached a psd below to help out with the profile picture and let you create your design around it. The size of the Facebook Timeline Cover is 851px by 315px, this is the best size and will keep Facebook from resizing the photo at all. If you have Photoshop I would definitely suggest using that but if you are looking for a free alternative there is always GIMP.

Here is a sample of how I have the PSD laid out, my complete facebook page is also included so you can see how your image will look with the Facebook layout.

Download: Facebook Timeline Cover PSD

Learning Cinema 4D

I have always been a bit interested in 3d modeling and designing and decided to give Cinema 4D a shot after seeing what Andreas Wannerstedt created with it and some help with Adobe After Effects. I have no plans to make anything as advanced as what he did but playing around with the physics and such is a lot of fun and very interesting. Here is the link to Andreas’s video on Vimeo called Genesis: http://vimeo.com/33294114

So what I have done looks nothing like that and I have only been playing around with the program for about 5 hours but this is what I have come up with so far.

My name in 3D with particles that create and destroy it.

http://www.youtube.com/watch?v=S2aNQDS-Y-c&feature=g-upl&context=G26664fcAUAAAAAAABAA

And then I made several blocks, I believe it comes out to 125 blocks in total and changed the resolution a bit.

http://www.youtube.com/watch?v=Hpx7VwoSBSU&feature=g-upl&context=G2529b24AUAAAAAAAAAA

Hopefully I will be able to create a nice looking video one day but it will be a little while until I am ready.

 

Time to get started with Zend Framework

I have been developing with PHP for a while now and as I am learning more and more about OOP with C# I got interested in more OOP based programming with PHP. I picked up a few books on more advanced PHP programming which explains how to use classes and the like with PHP and it seems fairly similar to C# in its most basic form at least. While doing more research on  PHP I stumbled across Zend Framework, I had heard about it before and I sort of knew what it was but I had never considered using it. But upon further reading of the documentation and what others had to say it really got me interested. So since I was learning more advanced PHP i figured why not throw in something like Zend Framework as well? I have not gotten very far but I am making and testing out some initial applications but progress is slow since I am also doing OOP C# and Discrete Mathematics at the same time.

Learning more about Zend got me interested in possibly switching to something better than dreamweaver for my PHP coding, yes I know it’s bad but I never felt that I needed anything better. At the moment I have started using NetBeans which seems pretty nice. I have yet to take a look at other IDE’s like something Eclipse based. But switching to NetBeans also got me thinking that I should probably clean up my work environment and get a bit more organized with my projects. Currently my projects are almost all always hosted, yes even during development, I like hosting the actual site in the main directory and then in another private directory I will have a duplicate of the site and database that I work on. I of course keep backups of my projects on external drives I have laying around. Anyways since I already have PHP on my Macs I figured I should just keep most of my projects in my NetBeans project folder and work on them locally, should be easier and faster right? :) We will see how it works out.

Other than that there is not much going on. Progress is steadily being made with BilligaApan.se, the swedish webshop me and a friend started about 6 months ago. I have a meeting with GoPlus coming up which seems interesting, just got to find time for it. I should also be heading over to PingDom soon, seems like a real interesting company to visit. I also met a new company I hadn’t heard about before called Diadrom which as far as I could understand the software to control various systems of cars and trucks and probably more, this might be a nice company where I could potentially take my “Ex-work” for college.

Another cool company I spoke to was Student Competitions, check them out and let me know what you think http://studentcompetitions.com

Spina – Premium WordPress Admin Template

Recently stumbled across this super sleek admin theme for WordPress. I can see lots of uses for this. Personally it’s great for my own company blog, where I can make it more stylish and sleek to show off to clients and friends. You basically end up hiding the whole WordPress part and making your site really unique. This is also the perfect theme for a web host who wants to provide wordpress with a custom look for their users.

Get The Theme Here For Only $18