Code

Code examples for languages such as PHP and JavaScript.

Restricting access to a page based on the referrer.

As you probably already know, PHP has an array called $_SERVER, which contains information such as: The server name. The request method (whether the request is POST, GET or PUT, etc). The IP address of the user. The visitor’s user agent. It has also has an index called HTTP_REFERER, which should give you the address of […]

Read More...

Common measurement conversions in PHP

One of the tricky things about being a web developer is that you are sometimes forced to accommodate visitors that use different measurement systems and formats. Currency symbols, date formats and measurement systems can all differ from one user to the next. The following PHP code snippets will help you to convert between some of […]

Read More...

Slow MySQLi connection on Windows 7.

Not so long ago, I came across a peculiar issue with PHP’s MySQLi extension. For some strange reason, using the string “localhost” in the constructor will sometimes result in an unexpected delay in connectivity. For example, connecting to MySQL will take 1-2 seconds. This is despite the fact that the DBMS is on a local […]

Read More...

Generating Google Maps markers with PHP.

On too many occasions, I’ve come across developers using PHP loops to print out large amounts of JavaScript data. This seems to be particularly common among coders who are trying to generate multiple markers for the Google Maps API. Typically, they will grab a bunch of long / lat values from their database and then […]

Read More...

PHP – Redirecting a form with POST variables.

On more than one occasion, I’ve come across novice PHP developers looking for ways to redirect the user and their submitted form variables to an external website. More often than not, they’ll ask if they can somehow attach POST variables to a header redirect. After this fails and they finally realize that another solution is […]

Read More...