How To Archives

Date and time page last updated

A PHP one-liner to keep the date/time the page was last updated:

page last updated <?php echo date(“F d Y H:i:s”, getlastmod() ); ?>

Note: uses the server timezone by default. getlastmod() returns a timestamp and if you are on a shared server (virtual hosting) and cannot change the timezone, let’s say the timezone is off by […]

How to use MySQL REPLACE function

Here’s a MySQL query fuction that’s easy to forget about: REPLACE. Let’s say I have a bunch of records in a field with http://www and want to quickly change all to http://. Here’s the syntax to update:

UPDATE tdurl_1 SET URL = REPLACE(URL, ‘http://www.tdurl.com/’,’http://tdurl.com/’);

How to build a how many days in the future script with timezone offset

This morning I was wondering what the date would be an arbitrary number of days in the future, like if somebody says: get back to me in a few months. I decided to search Google to see if there was a simple, web-based form that I could enter in the number of days in the […]

How to randomize, return and remove numbers from a pool

This morning in the IRC chat (irc.scriptschool.com #scriptschool) a random visitor named Rnd-Amarion stopped by with the following how-to PHP question:
I am about to learn PHP, and want to program a simple web page with a long string of random numbers. I have the random numbers allready. When someone queries the web site, it retrieves […]

Tips for speeding up your PHP scripts

Jaslabs shares six key points on improving the speed of your dynamic PHP scripts including: object code caching, template system, distributed object caching system, turning off some global PHP variable settings, output compression and database query optimization.

How to fix reBlog 2.0b2 URL sanitizing bug

Behavior: Links in Reblog cannot be ‘archived’ or ‘published’
Problem: Reblog wasn’t sanitizing the ‘link’ field in the MySQL table properly, allowing injection of unescaped single quotes (’) from RSS feeds into the database. The query would fail and thus would never process properly.
File: Controller.class.php in /refeed/library/RF
Version: $Revision: 1.40 (reBlog version 2.0b2)
Status: Bug reported with code […]

Dealing with the browser buttons in AJAX

Developing PHP the AJAX Way [article]:
A major challenge of Asynchronous JavaScript and XML (Ajax)-driven Web sites is the lack of a Back button. We will use JavaScript to create a history stack for the Ajax photo gallery
Good article on how to deal with the browser Back, Forward and Reload button.

Showing a user’s profile picture one time on a page

This is more of a proof of concept, but is in response to a post at Performancing where the concern is the same profile picture showing up more than one time on the front page:
What bothers me most, is how it looks when one author (me) might publish like 5 times in a row…
Let’s say […]

Debugging numerical sorting with strings

Ahh, good to be back after the holidays. Took a little time off from most the blogs including this one for those who are new readers.
In the meantime a good question came in on the sorting post back in October from Beregszászi Mihály, who wrote:

I have got a problem:
rsort($numbers);
Print:
9
8
7
70
6
69
68
67
This is my problem! My […]

How to not show the same thing twice when randomizing

Last night I was reviewing a new site that randomizes pages and I kept seeing the same pages coming up repeatedly. This is a rather common problem with many basic randomizers that do not keep track of what has already been shown.

This begs the question: how do you setup even distribution of a randomized […]