Category Archives: php

PHP code snippet – Reorder items in a table

This snippet of code reorders items in a table based on the number of steps you want to move an item from its current position. It moves the item of interest to its new position and shifts all other items to their new shifted positions. PLAIN TEXT PHP: function moveItem ($n_steps) { if ($n_steps == [...]

Akismet and PHP for your site

A few sites I administer have recently had the misfortune of having spambots visit their enquiry and contact pages. These pages usually have a contact form, where an enquirer can leave their name, e-mail, and request or comment. When they submit the form, a copy of the message is e-mailed to the site owner. The [...]

Timezones and UTC in PHP

If you are dealing with multiple users in different timezones or simply want to display times in a timezone other than your server's settings, it is best to store timestamps as their UTC (~ GMT) equivalents. When you read those timestamps later, you can convert them to local time. Local time to UTC time PLAIN [...]

Prevent caching of page on client’s end – PHP and HTML solutions

To prevent the caching of a web page on your client's end, use the following snippet of PHP to ensure that the appropriate HTTP headers are sent. PLAIN TEXT PHP: header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Tue, 03 Jul 1979 00:00:00 GMT");   // Date in the past The first header tells the client that the [...]

PHP/CURL

cURL is used to interact with remote URLs without needing a user to initiate the process (e.g. by clicking on a form submit button). cURL is useful for submitting HTTP POST/PUT/DELETE requests when dealing with web services. PHP has inbuilt cURL support since PHP 4.0.2 using cURL's libcurl library. Find out more about PHP/CURL -- [...]

Sending a plain text e-mail

Sending a plain-text e-mail through PHP is a simple process PLAIN TEXT PHP: mail ($to, $subject, $message, $headers); The $to parameter can look like: example@mysite.com example@mysite.com, user@mysite.com Example Sr <example@mysite.com>, Example Jr <user@mysite.com> Common headers to use in your messages: PLAIN TEXT PHP: $headers =  "From: yourname@yoursite.com\r\n              Reply-To: replyhere@yoursite.com\r\n [...]

Creating an XML document with PHP 5′s DOM Functions

PHP 5's DOM Functions (or extension) allows you to create and manipulate XML documents. In fact, it can also be used to create and manipulate any documents adhering to the DOM3 specifications such as HTML and XHTML documents. Creation of a DOM document is a fairly simple affair Instantiate DOMDocument - also specify which version [...]

Smarty tricks

Here's how to include another template file within a template file: PLAIN TEXT PHP: {include file='includes/menu_calendar.inc.tpl'} Tweet

Generate a GUID

PLAIN TEXT PHP: $guid = md5(uniqid(rand(), true)); md5 sanitizes the ID and limits it to 32 characters. uniqid generates a random ID based on your server's time, etc. rand() is prefixed onto the ID - thus enhancing the ID's uniqueness. true tells uniqid() to make use of "more entropy" - again enhancing the ID's uniqueness. [...]

Error reporting through your scripts

Your development environment might be configured to not display errors and log them instead. While this makes good sense in the production environment, while developing you want to be able to see every error and warning in order to pick up potential troublespots. You can override php.ini's error display settings via .htaccess or within your [...]