![]() Join Up! 96813 members and counting! |
|
|||
Cross-platform and Portable Development With PHP
Building an E-Commerce Website On Windows and Unix/Linux: Cross-platform and Portable Development With PHP
Mike Banahan
Synopsis
We have recently built and delivered an e-commerce project which runs
both on Linux and Windows, talking optionally to either Microsoft SQL
Server or MySQL. The software was built with PHP4 and is now deployed
on a Windows NT server under IIS and SQL server. It runs equally happily
(and somewhat faster) on Linux and MySQL.
We thought there might be some people who would like to know a little about
how it was done.
Background
The project objectives themselves were not out of the ordinary. We had to build
a site for industrial use which would allow a database of products, prices
and discount matrices to be accessed by registered users. The users should be
able to place orders, review order status and carry out typical
shopping-basket operations of the kind you would see on a many on-line ordering
sites. Although the site wouldn't be allowed to connect to the live enterprise
resource planning system (security concerns were amongst some of the reasons)
it would receive regular stock updates and should try to provide
a good indication of current stock levels.
The particular project objectives aren't all that important. The technically
interesting aspects are the cross-platform solution that evolved.
Our customer was keen that any solution should be able to run equally well
on Linux or Windows NT and that it should be independent of the database
product as far as possible. Various commercial constraints meant that it
would inevitably be deployed on NT/IIS with the data stored in a SQL Server
database. A particular concern of the implementation was that the database
would NOT be local to the web server, but instead accessed physically
remotely via the corporate internal WAN. The WAN is known to have limited
bandwidth and to get busy from time to time.
Flexibility and portability being important matters, we concluded that a
solution based on either Perl or PHP would be the best bet and elected to
use PHP since it was easier to convince some sceptics that PHP was a mature
product in the NT environment. The situation was politically sensitive (spot
the understatement) and whilst we would have been happy with
either choice, the art of the possible pointed towards PHP. (Our developers
still prefer Perl by the way).
Cross-Platform Issues
We already had PHP and MySQL happily running on Linux. The PHP download for
NT was no problem and after telling the SQL Server installation that it
had to be prepared to speak TCP/IP to clients (an option buried amongst
the numerous dialog boxes), it was responding to
local requests from PHP too. We persuaded the PHP code to talk across our network to MySQL
without the slightest difficulty. The reverse was another matter.
Building a version of PHP that would run on Linux and talk to SQL Server
was not proving to be easy. After quite a lot of digging around the PHP
website and reading what various people had to say in mailing lists,
we'd found that a) SQL Server talks the same protocol as Sybase (no surprise, since SQL Server is/was based on a Sybase core) and that b) we would need
some special libraries to make it work.
Those libraries are not part of standard PHP distributions for Linux/Unix.
We chose to use the freetds library.
It took a lot of cursing and sweating to get PHP working with freetds -
we'd have been well advised to have spent our time looking around for
the guidance that is actually out there, but sadly hadn't found it first time around.
At least one useful starting
point is:
Michael Peceny's site and of course, after all the struggling, others came to
light as well. You do need to be happy compiling applications from
source to make it work. If there are any packaged installations of PHP
and
freetds, then we didn't find them.
Eventually PHP4 running on Linux was talking to SQL Server running on NT.
Being used to the mature and powerful Perl
DBI:: interface for
accessing databases, PHP's insistence on using a different set of function
calls per database seemed terribly retrograde.
Whilst there are probably thousands of similar solutions to what we did
(and most of them probably better),
we built our own extremely simple interface so that the bulk of the
software wouldn't have to call functions conditionally depending on the
target database.
On each target we include a tiny file which specifies the database in use, the
hostname, username and password. Here is an edited version:
A common set of interface functions is then
included, as shown below:
Having built our interface we could get on with bulding the application. A couple of auxiliary PHP
programs were writte so that we could always drop all the tables in the database and re-create them
empty - these are run using PHP stand-alone instead of as a CGI script - and we found no serious
problems other than normal application development concerns.
Most of the development was done using
Linux tools with the NT virtual server directory mounted onto the Linux system (using smbmount) so
that the one set of files, located on the NT system, were simultaneously being used to run the Linux
and NT versions of the site. Switching our configuration files to tell each server which database to
use allowed us to ensure that the software operated properly with both SQL Server and MySQL.
Of course we found incompatibilities between the dialects of SQL that the two databases use.
MySQL considers every data type to be a string (or at least works as if it does) so that something like:
insert into blah values("abc", "123")
works even if the second column was declared to be a number. SQL Server won't stand for that and you need
to be type-conscious.
A special function had to be written to deal with string concatenation in a
consistent manner (see
DBconcat above) because we needed it often enough. A small number
of places in the software discovered irritating inconsistencies which had be dealt with.
Here is
a very frustrating example of serious database-dependency:
Other niggles we encountered were:
The goal of building code which would run without caring which database product it was accessing simply
couldn't be done cleanly enough for our taste. Extending it to even more databases would not be
a pleasant prospect.
Performance
We'd love to say that we have done a lot of performance testing, but we haven't. Apart from noticing
that MySQL is a good deal nippier than SQL Server at most of the tasks, database performance
hasn't been a big concern.
Our biggest performance problem was having to to deal (on the deployed system) with a WAN separating the
web front-end from the database. Customers searching for particular types of component could easily
generate queries that returned record sets with thousands of entries and the first version of the
software was appallingly slow when that happened. By paging through record sets using MySQL's
limit
and SQL Server's equivalent of it we got up to an acceptable level of performance and that was enough.
Conclusions
Cross-platform development with PHP proved to be entirely possible. Cross-database development (as well)
is trickier but by no means impossible. Our customer is now planning to deploy the same code on Linux
where it is organisationally acceptable and not constrained by the Microsoft-only corporate
`strategy'. PHP has caused us very little heartache. Our concerns about the insistence of running the
database via a low bandwidth connection from the web server proved to be valid,
but a workaround has been found. Our client has taken significant order volumes using this system and
has now commissioned several extensions.
Overall, a success.
This article was republished with permission. The original is at http://development.gbdirect.co.uk/phpwin.html. Mike Banahan has been involved in the Unix and Open Standards area since 1977. He has been an active member of the C and C++ standards committees and currently works on consulting and training projects through his company GBdirect. The company switched to using exclusively Open Source products in 1978, except where demanded by clients. |