PHP stands for PHP HyperText Processor (it’s what’s called a recursive acronym). PHP is an open source scripting language widely used for server-side web development.

Install the PHP SQLSRV Driver on Linux

Rejoice! Microsoft actually does provide a PHP Linux driver for Microsoft SQL Server. The bad news is, it can be a real pain to get it working.  I went through this experience tonight while setting up a utility I wrote for a client. I ran into two separate issues. This article provides some assistance with the issues I ran into when I tried to Install the PHP SQLSRV driver on Linux.

How to Install PHP SQLSRV Driver on Linux

The instructions in the repository’s readme.md are actually pretty good. Check out the official Github repository for the driver for the latest version and installation information. I recommend you follow their instructions first. When you hit a roadblock, come back to this article for help.

PECL Building with Wrong PHP Version

The final step in the installation is installing the base driver and the PDO variant withpecl install. PECL runs a build process on your machine and it uses a tool called phpize to execute the build. The version of this tool needs to match your PHP version. Otherwise, the build will succeed, but the extension file it generates won’t be compatible with the version of PHP you’re running. You’ll know pretty quickly if this is the case if you get “can’t load extension” errors after you build and execute PHP.

To solve the problem, install the version of phpize that matches your PHP version. For example, if you are running PHP 7.1, you would run:

sudo apt-get install phpize7.1

Microsoft ODBC Client is Missing

After I fixed the problem above, I ran my PHP script and the database connection failed. The PDO SQL Server driver reported an error message that it could not execute because it required the Microsoft ODBC Client. Installing the client is actually part of the steps in the instructions provided on Github. But for me, the installation failed and I didn’t notice. I went back and repeated this step and found that there was a missing dependency that would not install. Once I manually installed it and re-ran the client installation steps, then the PDO driver started working.

The Github repository below contains the official drivers from Microsoft. Check out the repository for the latest version information and installation instructions.

Subnet Calculator in 9 Lines of PHP

I’ve been spending a lot of time studying for my Microsoft 70-642 exam, an important part of which is subnetting.As an intellectual exercise I wrote a subnet calculator. Enjoy:

1
2
3
4
5
6
7
8
9
 function subnet($hosts)
 {
	$bits       = decbin($hosts) + 2; // Add 2 for Network ID and Broadcast
	$hostBits   = strlen($bits); // find how many bits it takes to represent it
	$cidr       = 32 - $hostBits; // Find slash-notation
	$binaryMask = str_repeat('1', $cidr) . str_repeat('0', $hostBits); // Subnet in binary
	$subnet     = implode('.', array_map('bindec', str_split($binaryMask, 8))); // Subnet in dotted-decimal
	return array('hosts' => $hosts, 'hostBits' => $hostBits, 'cidr' => '/' . $cidr, 'binaryMask' => $binaryMask, 'subnet' => $subnet);
 }

Use a Frame-Busting Redirect To Authorize Facebook Applications

Here’s a trick I picked up during development of my new Facebook application, My Wishlist. I picked this one up at Stack Overflow.

When you begin developing for Facebook, one of the first trick’s that you’ll learn is how to check for a Facebook session and how to redirect the user to the Facebook login page if they aren’t logged in or haven’t authorized your application.  Usually that code looks something like this:

<?php
$fb = new Facebook(array(
    'appId' =&gt; 'XXXXXXXXXXXXXXXXX',
    'secret' =&gt; 'XXXXXXXXXXXXXXXXXXX',
    'cookie' =&gt; true
));
 
$session = $fb-&gt;getSession();
 
if($session)
{
    // TODO Show your application's canvas.
}
else
{
    // Redirect the user:
    header('Location: ' . $fb-&gt;getLoginUrl(array(
        'next'   =&gt; $_SERVER['PHP_SELF'],
        'canvas' =&gt; 1,
        'display' =&gt; 'page'
    )));
}

If you’re developing an Iframe-based Facebook application (soon to be the only option since FBML has been deprecated), you’ve got a real problem: The redirect will happen within your application’s IFrame, with the actual login page content hidden within the frame. You’ll recognize the problem because it will look a little something like this:

This is what happens when you redirect to the login page within a Canvas-based Facebook application.

This is what happens when you redirect to the login page within a Canvas-based Facebook application.

The solution to this problem is to use what I call a Frame-Busting Redirect using JavaScript:

<?php
$fb = new Facebook(array(
    'appId' =&gt; 'XXXXXXXXXXXXXXXXX',
    'secret' =&gt; 'XXXXXXXXXXXXXXXXXXX',
    'cookie' =&gt; true
));
 
$session = $fb-&gt;getSession();
 
if($session)
{
    // TODO Show your application's canvas.
}
else
{
    // Redirect the user:
    echo "<script>\n";
    printf("top.location.href= \"%s\";\n", $_SERVER['PHP_SELF']);
    echo '</script>';
}

Making the PHP Curl Extension Work on Windows

Earlier tonight I was setting up some development tools on my new laptop and ran across a problem that I had never seen before.  When I began testing an application that uses the Facebook API (which depends on the CURL library), PHP insisted that the CURL extension was not loaded.  I checked and double-checked that my php.ini file was correct (it was) and that the rest of my extensions were loading (they were).

Finally after looking at the installation documentation for the CURL module I realized my mistake: I had forgotten to put PHP on the system path. According to php.net:

In order to enable this module on a Windows environment, libeay32.dll and ssleay32.dll must be present in your PATH. You don’t need libcurl.dll from the cURL site.

In other words, you either need to adjust your PATH environment variable to contain C:\PHP or you need to copy these two files from that directory to a directory that is on the path, such as C:\windows\system32. Either solution should resolve the problem.

6 Reasons I Chose A2Hosting

FTC Disclaimer: I’m a satisfied, four-year customer of A2Hosting and a proud member of their Affiliate Program. I’ll make a buck or two if  you purchase hosting via my site,  so if you have a moral objection to helping me pay my monthly Netflix subscription, then by all means don’t click my links. Either way, check out A2Hosting.

Anti-Disclaimer: My blog has had a long and proud tradition of sounding off about products that suck. Just this once I decided to get behind a product that I’m truly proud of and tell you all why I recommend A2Hosting’s Shared Hosting package above the competitors.

Introduction

If one goes searching this vast Internet of ours for recommendations about cheap web hosting, one will be sure to drown in oceans of fake ratings and user reviews, not to mention affiliate links from folks who have never actually sampled a company’s services or those of any of their competitors.

I’ve dealt with several cheap, shared web hosts from the largely unknown Global Internet Solutions (name intentionally left unlinked for your protection) to Danika Patrick’s host-of-choice GoDaddy.  But when I have the choice, I host with A2Hosting.com.  This personal choice and my recommendation to my readers comes not from the promise of affiliate payments, but from four years of positive experiences with this company. Below are 6 reasons why I stand behind A2Hosting.

6. A2Hosting Watches & Listens

Up until just a few months ago I was running a now-defunct personal project called Sudoku Madness.  This marginally-popular website was hosted on A2Hosting and hosted hundreds of thousands of unique Sudoku puzzles in a PostgreSQL database. My site became a small hit (especially with my sister’s seventh-grade math class), which turned out to be a problem for other users who shared my server:  my scripts were opening a new database connections on each request and not returning them for use by other users.  A2Hosting didn’t knock me offline as they had every right to do, but they immediately notified me that my PHP was running amock and proposed a fix.

A2Hosting solved a problem of my own creation and suggested a solution that kept me and their other customers online and happy. Plus they kept me earning literally tens of cents per week in affiliate income from that project! (Look for my money-making infomercial coming soon to QVC!)

5. A2Hosting Supports PostgreSQL

Many shared web hosts support only the typical LAMP development stack; but in addition to the traditional Mysql offering, A2Hosting also supports PostgreSQL.  Attempting to debate the pros and cons of the two platforms is both out of this article’s scope and risks inciting a religious war, but suffice it to say that Oracle’s purchase of Sun Microsystems leaves MySQL’s future uncertain so a choice in open database platforms can’t be a bad thing.

I chose to use PostgreSQL on several of my projects, most notably on my Sudoku Madness site.  That particular database housed hundreds of thousands of records and used home-brewed PGSQL procedures to speed up searches and puzzle creation. The site ran for several years largely unchanged, and speed and storage never became a factor.

My one complaint: A2Hosting’s automatic backups include your MySQL databases, but at least as of a year ago, not your PostgreSQL databases. To the best of my knowledge, you’ll need to set up your own backup schedule if you choose PostgreSQL (a good idea regardless).

4. A2Hosting’s Server Rewind Feature

Whether you only host your own projects or resell hosting to clients, the day can and will come when you’ll need a backup. There is nothing more embarrassing and damaging to your credibility than losing a client’s site or database and not having a recent backup.  Nobody is perfect. That’s why pencils have erasers and decent web hosts provide backups.

I’ve tested A2Hosting’s backup system on numerous occasions.  The first was when my account was migrated to a new server and one of my databases was missed during the restoration (again, darn that lack of PostgreSQL backups!). With the help of A2Hosting’s support department I was able to recover one of my manually created SQL dump files from the Server Rewind app and quickly get my site up and running again.

More recently a site which I inherited from another developer was hacked.  The code for this site only worked when a certain directory under the web root had full write access by the Apache process and the client refused to pay for a rewrite.  Suffice it to say, they eventually got theirs.  I was able to recover both their site and their back-end database  using Server Rewind, available through my management panel.

3. A2Hosting Supports Subversion

The day comes when a developer outgrows manually updating their websites via FTP.  Fortunately A2Hosting supports Subversion over SSL, which provides you with secure version control of your code.  They also offer CVS and Git hosting if Subversion isn’t your thing.

I personally use A2Hosting’s Subversion hosting for all of my projects that aren’t based on another project (WordPress, etc), even development projects that aren’t hosted on A2Hosting.  After committing my code to Subversion I SSH into my server and run a script that pulls the latest changes from the repository. Using version control to manage your sites saves you time and bandwidth by only uploading changed files, and it gives you the power to quickly undo a bad update.

2. A2Hosting Has Never Eaten My Domains

Granted, I’ve never given them the option. I’ve previously lost domains to other shared web hosts who offered me cheap domain registration but refused to release my domains after I became disgusted with their service and wanted to move.

On the other side of the coin, I’ve transferred a number of domains to A2Hosting and they’ve always offered assistance in this frustrating process when they can.

1. A2Hosting’s Support is Unmatched

I can’t begin to explain how helpful A2Hosting has been to me over the past few years.  They’ve always responded quickly to my support requests no matter how inane, and they’ve saved my ass from embarrassment and lost clients on a number of occasions.

When I developed my first site using PostgreSQL, I uploaded my scripts to A2Hosting only to find that they had not enabled the pdo_pgsql extension which my code was completely dependant upon.  Their support people had the issue resolved within hours, which I consider more than acceptable for a shared host, when other hosts have rejected my requests across the board for common PHP extensions in the past.

More recently I had an issue with mail from my domains being rejected.  This is the sort of issue which truly tests a company’s metal because there are so many possible points of failure. Many companies will play the blame game for days or even weeks with email issues, insisting that the problem is “on the other end.” A2Hosting has never been afraid to dive in, and resolved this issue by the end of the day.

Summary

Though A2Hosting remains competitive in price, speed and options, their edge comes from their fanatical support.  Though I award them zero points for originality by naming their support wing the Guru Crew, those are the folks who have made me a committed and loyal customer of A2Hosting. Choosing a web host is a lot like choosing car insurance:  you can pay your premiums for years and have no concept of a company’s quality.  You find out when it’s too late, and that’s when A2Hosting truly shines.