September PHP-Scripts Blog Archives

Turck MMCache and PHP encoder

I’m not sure how current or updated this is because I found some broken project links (all links below work) but these projects seem promising and worth closer inspection, especially if one is looking to make their PHP scripts run faster.
Turck MMcache:
… a free open source PHP accelerator, optimizer, encoder and dynamic content cache for […]

Changing databases inside code

To change MySQL databases within code use the mysql_select_db() function like this:

<?php
 mysql_select_db(‘db_2′, $mysql_link);
$query = “SELECT username from user where userid = ‘$user_id’”;
$result = mysql_query($query,$mysql_link);
        if(mysql_num_rows($result)) {
                $row = mysql_fetch_row($result);
                $username = $row[0];
        }
mysql_select_db(‘db_1′, $mysql_link);
?>

This code […]

Editing and customizing error messages in Wordpress: wp-db.php

Wordpress has a cool plugin system that allows for various template changes. Sometimes, though, some defaults are planted in the program that require hacking the code to remove. By creating your own 404.php template and storing in the template are you can have it match the design of the rest of your blog, but what […]

PHP Security Guide

The PHP Security Guide discusses and explains in depth PHP security issues, including many code examples:
If you do not design your application with security in mind, you are doomed to be constantly addressing new security vulnerabilities. Careful programming cannot make up for a poor design.
Even a little security philosophy in there. Definitely worth bookmarking.

Parsing names and numbers out of text

At one of our sites we needed to parse out some names and numbers from a log file. The strings also contained comment text and the order would be mixed format. These types of regular expression parsing can be tricky.
Let’s take a look at the two possible formats of the strings:
FORMAT #1:
[00:02:18][jvastine]7.52..I agree with […]

Using header to redirect the browser

The PHP header function allows redirection of the browser, among other cool things.
Example redirecting the browser to php-scripts.com homepage:

<?php
header(”Location: http://www.php-scripts.com/”);
?>

Using floor and time to calculate number of days elapsed from UNIX timestamp

Finding out the number of days elapsed from a UNIX timestamp is actually pretty straightforward. If you only know two dates then first you’ll need to convert the date to a UNIX timestamps and then subtract the most recent timestamp from the older timestamp like this:

<?php
$old_timestamp = 1102971600;
$elapsed_seconds = time() - $old_timestamp;
print ‘Seconds elapsed since […]

How to use preg_replace to replace phrase between question marks

<?php
$string = “?This is a test that should be in single quotes quotes?”;
$text = preg_replace(”/(\?([^\?]+)\?)/”,”‘\\2′”,$string);
?>

regular expression produces:
This is a test that should be in single quotes’

How to make an anniversary counter announcement script using ordinal numbers

Today being it is our 16th wedding anniversary I decided it would be appropriate to build a small script to keep track and post a message automatically on our special day together. I wanted this program to remember what year of our anniversary it was without me having to remind it: a great use for […]

4 PHP debugging tips and tricks

So you are getting a weird parser error at a line of code that doesn’t have any errors? Here are some tips and tricks I’ve compiled over the 6+ years I’ve been doing PHP scripting for tracing various PHP errors in source code:
1. Start commenting out code near the parser error line number. Start by […]