Category Archives: php

Accelerating PHP with the Alternative PHP Cache

I decided to install the open source Alternative PHP Cache on this server in order to improve performance. Interesting exercise. This server runs Debian Linux, and there are several ways to install APC:

1. Install the official package with apt-get install php-apc or similar

2. Install with the PHP Extension Community Library which goes something like:

apt-get install apache2
apt-get install libapache2-mod-php5
apt-get install php-pear
apt-get install php5-dev
apt-get install make
apt-get install apache2-prefork-dev
pecl install apc

The advantage over (1) is that you get the latest stable build, version 3.1.6, instead of the Debian package which is 3.0.19

3. Download the source and do something like this to install.

I started with option (2) though I came to regret it. The first problem is that the pecl installer will build with your currently-installed Apache, and if you later upgrade Apache it might break. Sticking with the official package is safer, even though it is very out of date.

I could live with the idea of re-installing APC every time Apache was updated if necessary, but I had another problem. I was up and running with APC 3.1.6 and pleased with the results, until after a while everything stopped working and my blog became a screen full of messages saying “Unable to allocate memory for pool”.

It looks like this bug, which was said to be fixed in version 3.1.5, but if you look to the end of the comments there is one from today with the same issue, and no suggestions about how to fix it.

The ancient version, on the other hand, has performed perfectly so far.

Another point of interest: I found it challenging to discover the best settings for APC. By default the install does no more than to enable the extension; but the default setting is unlikely to be the best one. The documentation tells you what each setting does, but not how to choose the best values for those settings. Should the cache be the default 32MB, or something much greater? Another thing to note: if you compile with MMAP support, which is the default, the value of apc.shm_segments is ignored, and the value in apc.shm_size will solely determine the size of the cache.

I found this Moodle article on installing APC in Windows helpful. What you do is first to find the file apc.php which the install put somewhere like /usr/share/doc/php-apc – in my case it was also compressed -  and put this on your website, preferably in a password-protected folder. This tells you the status of the cache. The aim is to have the cache just big enough that it does not become full and highly fragmented. Here is what I get after a short run with 128MB, which may be a little too much:


Another tip is to set apc.stat to 0. This means APC will not check for changes in PHP files since they were last compiled and cached. The downside is that every time you change a file you have to restart the web server; but the benefit is better performance, which is the goal after all.

Microsoft WebMatrix released: a simple editor for ASP.NET Razor and more, but who is the target user?

Microsoft has released WebMatrix, a free tool for creating web sites for Microsoft’s web server. It uses the Web Platform Installer and installed smoothly on my Windows 7 64-bit box. What you get is a cleanly-designed tool which lets you start web sites from templates or from standard installs of popular applications including WordPress, Drupal and Moodle.


Yes, you can use PHP and MySQL as well as .NET web applications, though the common factor is that all are configured for IIS, Microsoft’s web server.

With many ISPs already offering instant installs of apps like WordPress, it is more interesting to look at the site templates in WebMatrix, though the selection is smaller.


What is interesting about these is that they create sites based on Razor, an alternative view engine for ASP.NET. Microsoft VP Scott Guthrie describes Razor here. It is odd though: Razor is a feature of ASP.NET MVC 3, currently in release candidate phase, but you cannot create ASP.NET MVC sites in Web Matrix.

Once a site is created, you can modify it in the WebMatrix editor.


You can run the site on IIS Express with one click. WebMatrix will show you all the requests as you run, which could be handy for tracing problems. There is also a database management workspace which uses SQL Server Compact Edition, a reporting workspace which will analyse your site for problems, and the ability to publish a site using  FTP or Microsoft’s Web Deploy.

I like the clean look of WebMatrix, and that it is lightweight and fast; but who is the target user? It appears to be aimed at non-professionals; but this is a techie product that will not appeal to users looking for an easy to use web site builder. There is no visual editor; users are just chucked in at the deep end editing raw HTML and C#. There is not even any intellisense code completion. Clicking Online Help just brings up a Microsoft search form. There is no debugger to speak of; you are expected to upgrade to Visual Studio. Which raises the question, why not just get Visual Web Developer 2010 Express, which is also free, and has a better editor and debugging features? Of course you could use the two together; but Web Matrix is not adding much value. Features like the SEO analysis seem to be be based on the existing Search Engine Optimization Toolkit, which you can install without Web Matrix.

WebMatrix has been available in beta for six months, but its forum is relatively quiet.

Still, if nothing else Web Matrix is a handy way to take a look at Razor, which deserves attention. Shay Friedman has a technical introduction here.

Guthrie has a detailed look at the WebMatrix beta here.

Single sign-on from Active Directory to Windows Azure: big feature, still challenging

Microsoft has posted a white paper setting out what you need to do in order to have users who are signed on to a local Windows domain seamlessly use an Azure-hosted application, without having to sign in again.

I think this is a huge feature. Maintaining a single user directory is more secure and more robust than efforts to synchronise a local directory with a cloud-hosted directory, and this is a point of friction when it comes to adopting services such as Google Apps or Single sign-on with federated directory services takes that away. As an application developer, you can write code that looks the same as it would for a locally deployed application, but host it on Azure.

There is also a usability issue. Users hate having to sign in multiple times, and hate it even more if they have to maintain separate username/password combinations for different applications (though we all do).

The white paper explains how to use Active Directory Federation Services (ADFS) and Windows Identity Foundation (WIF, part of the .NET Framework) to achieve both single sign-on and access to user data across local network and cloud.


The snag? It is a complex process. The white paper has a walk-through, though to complete it you also need this guide on setting up ADFS and WIF. There are numerous steps, some of which are not obvious. Did you know that “.NET 4.0 has new behavior that, by default, will cause an error condition on a page request that contains a WS-Federation authentication token”?

Of course dealing with complexity is part of the job of a developer or system administrator. Then again, complexity also means more to remember and more to troubleshoot, and less incentive to try it out.

One of the reasons I am enthusiastic about Windows Small Business Server Essentials (codename Aurora) is that it promises to do single sign-on to the cloud in a truly user-friendly manner. According to a briefing I had from SBS technical product manager Michael Leworthy, cloud application vendors will supply “cloud integration modules,” connectors that you install into your SBS to get instant single sign-on integration.

SBS Essentials does run ADFS under the covers, but you will not need a 35-page guide to get it working, or so we are promised. I admit, I have not been able to test this feature yet, and aside from Microsoft’s BPOS/Office 365 I do not know how many online applications will support it.

Still, this is the kind of thing that will get single sign-on with Active Directory widely adopted.

Consider FaceBook Connect. Register your app with Facebook; write a few lines of JavaScript and PHP; and you can achieve the same results: single sign-on and access to user account information. Facebook knows that to get wide adoption for its identity platform it has to be easy to implement.

On Microsoft’s platform, another option is to join your Azure instance to the local domain. This is a feature of Azure Connect, currently in beta.

Are you using ADFS, with Azure or another platform? I would be interested to hear how it is going.

BBC iPlayer goes a little social – but what can it learn from YouTube?

BBC’s iPlayer, which offers but live TV and radio and an on demand catch-up service, has gone live with a new version that adds personalisation as well as links to Facebook and Twitter for limited interaction with your social media contacts.

Presenting a personalised iPlayer web page is more demanding, as every visitor sees a slightly different page. Technical Architect Simon Frost explains that the BBC adopted PHP and the Zend framework in order to enable sharing of components and modules – previously the site was built with Perl and server side includes.

It has been interesting to see the reaction in user comments to the announcement by James Hewines, which are more negative than positive. The gist of the complaints is that many users want the site to get out of the way rather than be a more interactive web destination, and find that the new version adds clutter rather than speeding navigation:

This site should not be fun to use. It should be … a very brief stop between powering the computer up and watching or listening a programme.

This does not necessarily mean the BBC is getting it wrong. It is not really hard to find items, and linking BBC content to social networks does have a certain inevitability. It is a shame though that the most interesting feature described by Anthony Rose back in May seems to be missing – integration with Live Messenger so that you can see your contacts comment on the live content. This is what it was meant to look like:


That said, does it make sense to hook into Live Messenger when Facebook is more at the centre of today’s social networks on the Web? Still, this may appear at some future date.

As it is, we get only limited social interaction through recommendations. No ratings, no public comments or reviews attached to items, nothing that might add real interest to the site.

Contrast this with YouTube. It is a different kind of broadcasting of course, much of it amateur, quantity not quality, no live broadcasts as such, and content that persists (whereas BBC content disappears after a few days). Perhaps because it was born as a social media site, rather than being a traditional broadcaster trying to grok the Web, YouTube does community to a far greater extent than the BBC. Individuals can even create their own “channels” of content, becoming a destination within a destination that appeals to friends or those with the same niche interests.

The BBC’s exploration of social media is just too timid to make an impact. I interviewed Rose back in 2008 and he made a telling comment about how the BBC would remain a place safe from the unpleasantness of one-star ratings and nasty comments:

Rose says that upcoming iPlayer features, such as ratings and discussions, will be restricted to networks of friends, rather than made public. "Rating works really well in YouTube where you’ve got a million videos. In iPlayer, if you rate Parliament channel as zero stars, are you saying that Parliament is rubbish, or that you just don’t want to watch Parliament? Rating in the context of the BBC is very useful, but only when you’ve got a friends network."

Unfortunately safe also means dull, and I doubt the social aspect of iPlayer will make much impact as currently implemented.

I still love iPlayer. I have less time to watch than I would like; but I clicked the HD channel and was soon watching a stunning programme about scaling ancient buildings, in this case Durham Cathedral. Played full-screen, the photography was beautiful and the content both entertaining and educational, the BBC at its best.



Note that there was no need to involve the desktop player to enjoy this; it worked fine from the web; and I suspect that the download player gets relatively little use compared to the iPlayer site – the BBC won’t discuss this, saying “We cannot report on playback of downloads due to technical and data privacy restrictions”, which makes little sense to me.

Perhaps the comments are right – the BBC should concentrate on its content, and leave the social stuff to others who do it better.

XML literals come to PHP via Facebook: XHP

Facebook engineer Marcel Laverdet has written up the XHP project on which he has been working at Facebook, and which he says is “quickly becoming a cornerstone of front-end PHP development at Facebook”. XHP enables XML fragments to be valid PHP expressions. In addition, most HTML elements have been pre-defined as variables. The project is hosted on github and you can read a quick summary here. PHP inventor Rasmus Lerdorf has had a quick look and says:

The main interest, at least to me, is that because PHP now understands the XML it is outputting, filtering can be done in a context-sensitive manner. The input filtering built into PHP can not know which context a string is going to be used in. If you use a string inside an on-handler or a style attribute, for example, you need radically different filtering from it being used as regular XML PCDATA in the html body. Some will say this form is more readable as well, but that isn’t something that concerns me very much.

Lerdorf goes on to express concern about performance, but says that in combination with APC caching it is much better, and with HipHop compilation to native code becomes “a viable approach”.

One of the benefits here is that getting XML markup out of quoted strings means that it can be checked as it is parsed, so that errors are caught earlier. There are parallels with Microsoft’s work on Linq (Language Integrated Query) which pulls database queries into the language, and XML literals in Visual Basic 9.0. Ideally, developers should not have to hide code such as SQL, XML or Javascript within strings that are not checked for valid syntax until runtime.

This might also re-ignite the debate about whether XML literals should be in C#, now also gaining more attention because of the popularity of ASP.NET MVC.

Is Zend really the PHP company?

I’m at Yahoo! Hack day in London – not hacking, but here for sessions on topics such as YUI (Yahoo! User Interface Library) and PHP.

I had a brief chat with Rasmus Lerdorf who is speaking later. I asked him about Zend, which presents itself as the PHP company (that is actually the slogan on its web site). Is it really?

Lerdorf says Zend has no special status. While acknowledging its contribution, he says there are 1300 PHP committers, and only 6 work for Zend. He emphasises that PHP is a community project and that decisions are made by consensus, influenced by who is actually willing to write the code, not by Zend or any company.

I also asked about PDT (PHP Development Tools), the Eclipse-based open source IDE. Lerdorf says there are lots of PHP IDEs, and people who use generic editors for PHP, and none has any more status than any other; he doesn’t use PDT.

From my perspective as press, there are only two organizations who ever encourage me to write about PHP. One is Zend; the other is Microsoft, keen to establish Windows as a credible PHP platform (Lerdorf says PHP on Windows has made enormous progress in the last couple of years). Zend does seem to do more than any other company to promote PHP for commercial and corporate development.

Lerdorf is not surprised. We’re developers, he says, we don’t do PR.

Zend’s effort is broadly beneficial to the PHP community – provided that it does not give a false impression of who owns PHP.

Microsoft Expression Web causes PHP error

I ran into a strange and surprising PHP error today. I’m working on a little PHP application which has a login page. The login script calls session_start() to start or resume a PHP session. It was working OK so I decided to decorate the page a little (I was working in Eclipse). I like to try a variety of tools, so I ran up Microsoft’s Expression Web, added an image, then re-ran the script to see how it looked.

The answer was not good, because I now had an error:

Warning: session_start(): Cannot send session cookie – headers already sent

I puzzled over this for some time. The error was in line 0 of my login page. I couldn’t see anything that was different from before, except the static image that meant nothing to PHP.

Eventually I worked it out. Eclipse (running on Windows) created the PHP files using ANSI. On saving, Expression Web silently changed them to UTF-8. That in itself was no bad thing – it’s usually a better choice – though I reckon it should ask. The bigger problem was that Expression also added a BOM (byte order mark) to the beginning of the file. This is actually optional for UTF-8, and most non-Windows editors do not add it. It happens to flummox PHP, which interprets them who-knows-how and sends some output to the browser, preventing session_start from working.

This is particularly painful to debug since most editors do not display the BOM; they simply use it to confirm the character set in use. So you can have file A which works, and file B which does not, and they are character-by-character identical.

One way to see and remove the BOM is to open it with, which does not understand it at all:

I guess both Expression and PHP could do better here. The bit that puzzles me is that I can’t be the first to run into this. Doesn’t Microsoft know that its UTF-8 BOM breaks PHP files, at least on the two versions I tried (XAMPP on Windows and PHP 5.2.1 on Linux)? I can’t even see a preference in Expression that would prevent it being written. And if you remove it, and then re-edit in Expression, it carefully writes it back. Unlike Adobe’s Dreamweaver, which leaves well alone.

PS if you want to know all about BOMs, see here.

Update: See comments – apparently this was fixed in Expression Web 2.0. Tina Clarke discusses the problem here. linking with Facebook, Amazon

I’m at the Dreamforce conference in San Francisco, where Marc Benioff, CEO of, and co-founder Parker Harris, are presenting new features in the platform.

The first is a built-in ability to publish your data as a public web site. The service is currently in “developer preview” and set for full release in 2009. Even in preview, it’s priced per page view on your site. For example, if you have the low-end Group Edition, you get 50,000 page views free; but if you exceed that limit, you pay $1000 per month for up to 1,000,000 further page views. It would be unfortunate if you had 50,001 page views one month.

The second announcement relates to Facebook integration. This is a set of tools and services that lets you use Facebook APIs within a application, and create Facebook applications that use data. Sheryl Sandberg, Facebook COO, says this is “Enterprise meets social”. The problem: Facebook is consumer-focused, more play than work. Sandberg says this deal will launch Facebook into the Enterprise. This will be an interesting one to watch.

Third, there are new tools linking with Amazon’s S3 and EC2. Tools for S3 wrap Amazon’s API with Apex code (Apex is the language of so you can easily add unlimited storage to your application. Tools for EC2 delivers pre-built Amazon Virtual Machines (AMIs) that have libraries for accessing data and applications. The first AMI is for PHP, and simplifies the business of building a PHP application that extends a solution.

Interesting that is providing two new ways to build public web sites that link to – one on its own platform, the other using PHP and in future Ruby, Java (I presume) etc.

It’s worth noting that you could already do this by using the SOAP API for, and there are already wrappers for languages including PHP. This is mainly about simplifying what you could already do.

More information is at

When will PHP Developer Tools be mainstream at Eclipse?

I’ve been doing a little PHP work and enjoying it; I like PHP 5.x much better than earlier versions. My PHP development setup is based on Eclipse and the PHP Developer Tools project, or PDT, and one thing I noticed when I set this up is that it is awkward to use PDT with Eclipse 3.4, or Ganymede. I ran into problems again when I updated my Ganymede Eclipse to the latest releases, this time on Windows as it happens. PDT stopped working, and I had to download a newer “integration build” of PDT as well as an update to the Eclipse Dynamic Languages Toolkit (DLTK), using a manual download and import process instead of the built-in Eclipse online update. I also had to remove the Ruby Development Tools as these relied on an earlier version of the DLTK; there might be a way round this but my priority was to get PDT working.

I’m getting this pain because I want to use PDT 2.0 and Eclipse3.4, instead of the older PDT 1.0.3 which has an all-in-one download based on Eclipse 3.3. “All-in-one” means that you download a bundle which includes both Eclipse and PDT, and treat it as a separate standalone IDE. The question though: why wasn’t the PDT properly integrated with Ganymede, which brings together multiple Eclipse projects with the promise that they will all work together?

I looked in the Eclipse PDT newsgroup and found some discussion on the subject. Apparently the PDT team felt it was just too difficult to manage the dependencies. More depressing is that apparently the team feels the same way about Galileo, the follow-up to Ganymede expected in June 2009. It means that the PDT stays outside the mainstream of Eclipse projects, reducing its visibility.

Conspiracy theorists might surmise that major PDT contributors like Zend, which has its own commercial IDE called Zend Studio which uses both Eclipse and PDT, might enjoy keeping the free version low-profile. That (or some other reason) might also explain why Zend Studio uses a 1.x version of PDT along with Eclipse 3.4, which is not meant to work. It turns out that Zend Studio uses PDT 1.0.5, whereas the latest public download (unless you go directly to the source) is 1.0.3. If 1.05 works fine with Eclipse 3.4, why isn’t the public all-in-one based on this combination?

I like the PDT, and my patched together Ganymede + PDT 2.x works very well. Debugging seems more stable since I updated it. Personally I’d like to see PDT get more prominence within the Eclipse community, and for it to be packaged as part of Galileo rather than being left on the sidelines.

Technorati tags: , , , ,

Future of Web Apps 2008 Day One: Web is DVD, desktop VHS

I’m at London’s dreary Excel centre for Carson’s Future of Web Apps conference, just before the opening of day two. Yesterday was a mixed bag; good when speakers talk technical; bad when they descend into marketing. The origins of the conference are as a start-up incubator; developers and entrepreneurs getting together to see what’s new and make contacts. It still has some of that flavour, but it has grown beyond that because web apps are a mainstream topic and Carson attracts generally excellent speakers. There is a good crowd here; I’m not sure if every last ticket sold, but it is pretty much packed out, though the dark economic mood is dampening spirits.

Digg’s Kevin Rose spoke briefly about his site’s new recommendation engine, which has been active since July or so. The idea is that Digg learns a user’s profile by examining clicks and votes, using it to customize what the user sees. He spoke about a forthcoming feature, where third-party sites will be able to call the Digg recommendation engine to get profile information that it can then use to customize its own site.

An interesting idea; though it raises several questions. How does it work – would logging out of Digg be sufficient to disable it? Will users opt-out or opt-in? How much of this kind of customization do we want anyway?

This whole theme of contextualization is a big one here; it ties in closely with social networking, and Google’s OpenSocial API is getting quite a bit of attention.

Blaine Cook (ex Twitter now Yahoo, Ruby guy and inventor of OAuth) gave a though-provoking session on scalability along with Joe Stump from Digg (and a PHP guy). They took the line that languages don’t matter – partly a reflection on Twitter’s scaling problems and whether it was Ruby’s fault. Other factors make language efficiency unimportant, they said, such as disk I/O and network speed; and the secret of scaling is multiple and redundant cheap boxes and apps which are segmented so that no one box  is a bottleneck. The case was overstated but the main points strike me as sound.

I’m wondering how many of the developers here are actually having to deal with these kinds of scalability problems. Many web apps get only light use; the problems for everyday developers are different.

I attended a session entitled "The future of Enterprise Web Apps" by Googler Kevin Marks. It turned out to be a plug for the OpenSocial API; not what I was expecting.

Francisco Tolmasky of evangelised his Objective-J and Cappucino JavaScript framework, based loosely on Apple’s Cocoa framework. Hmm, bit like SproutCore.

I give Tolmasky credit for the most striking analogy of the day. The Web is DVD is says, and the desktop VHS. Adobe’s AIR is a combo player. He is talking about transition and leaving us in no doubt about what he sees is the future of the desktop.

Best sessions of the day (that I attended) were Blaine Cook on Jabber and its XMPP protocol, and David Recordon from SixApart on the evolving Internet "open stack". In this he includes:

  • OpenID + hCard for identity
  • XRDS-Simple for discovery (
  • OAuth for authentication
  • ATOM and POCO  ( or PorC) – Portable contacts)
  • OpenSocial

I put these two sessions together because they both addressed the "Web as platform" topic that is really the heart of why we are here. Spotting which APIs and protocols will win is tricky; but if consensus is reached on some or all of these, they will impact all web developers and bring new coherence to what we are doing.

I’ll be covering today on Twitter again – see here if you want to follow.