Determine whether PHP is being run via HTTP or CLI

PHP can be run as a webserver module, as a CGI application or from the command line as a CLI script. There is both a function and a PHP constant which allows you to determine whether PHP is being run via HTTP or CLI. This post looks at the function and constant and the possible values, as determined in the PHP 5.2.5 source code.

Categories PHP

PHP Register Long Arrays HTTP_POST_VARS

I have been in the process of moving my websites from a dedicated machine running Gentoo Linux to a VPS (Virtual Private Server) running CentOS 5. One of the ecommerce websites uses an old install of osCommerce which expects register globals and register_long_arrays on, otherwise things won’t work very well.

While testing the site out on the new server, I couldn’t work out why some of the form posts were working and some weren’t. For example, on some pages I could add items to the shopping cart but on others I couldn’t. I also couldn’t click the form button to proceed to th checkout or update my basket: whenever I tried it kept showing me the same basket page again.

Categories PHP

Replacing relative URLs with absolute URLs in PHP

I’ve finally set up an RSS feed for this site and one of the things I needed to sort out was the use of URLs and image tags. All the anchor tags in this site are relative, and so are all the image sources. This isn’t going to work in an RSS feed because it would mean none of the links would work, and the images would all be broken. Fortunately this is easy to fix with the use of PHP’s string replacement functions.

Categories PHP

PHP Magic Constants

There are several PHP "magic constants" (or "magical contants") which can be useful for a variety of reasons. These magic constants aren’t actually constants at all, but effectively behave like them, although the values change depending on the context.

Categories PHP

Correcting the PHP timezone

The end of daylight savings in New Zealand has been changed from Sunday March 16th (today) to Sunday April 6th. Last week I looked out how to check the daylight savings dates will be applied correctly on Linux, but now that it’s actually the day itself it turns out that while the operating system is using the correct date, PHP is not due to PHP5 maintainin its own internal timezone database. This post looks at how to temporarily correct the solution until it’s fixed in the distro’s PHP builds.

Categories PHP