New Resources Posted – PHP Frameworks

There is a new section on the Resources page for Frameworks. This section of the resources page lists popular PHP frameworks like Zend Framework, which is my personal favorite, and CakePHP. The six common PHP frameworks that were added are also listed below:

Do you use a different framework from those listed above? Leave a comment about it and I'll get it added to the list.

PHP Date Tips & Tricks

For those of you using PHP to develop your web pages, I'm sure that you have used the date function at some point. There are so many cases that I have used this to show a date on a page, like using date("Y") in the copyright section of the footer to always reflect the current year. Here are a few tips & tricks to help you save some time.

Formatting

Predefined Constants

This is a trick that I really wish I had known about earlier. PHP 5.1.1 and forward offers date formatting constants. So when I was looking for examples on how to get the date in the correct format when creating my RSS feed, I could have simply used the DATE_RSS constant.

Example

Use:

<? echo date(DATE_RSS); ?>
Output: Thu, 24 Nov 2008 12:29:09 -0500

Instead of:

<? echo date('D, d M Y G:i:s O'); ?>
Output: Thu, 24 Nov 2008 12:29:09 -0500

There are eleven total predefined constants available including DATE_RSS, DATE_ATOM, DATE_COOKIE, and DATE_W3C.

User Defined Constants

Building on this same idea of constants, you could easily create your own date format constants to use. All you would need to do is define a constant with the string value of the date format and then call it within the date function. I have included a simple example below.

Example

<? define(DATE_SCOTT,'m/d/Y'); ?>
<? echo date(DATE_SCOTT); ?>
Output: 11/24/2008

Relative Dates

This trick is one I use when I need to get a date like tomorrow or the day after tomorrow or yesterday. The function strtotime allows you to easily calculate dates like tomorrow and yesterday. You can turn any GNU formatted date string into a PHP date. I have included a couplt of examples below that are based on a publish date of 11-24-2008.

Example

<? echo date('m-d-Y',strtotime('yesterday')); ?>
Output: 11-23-2008

<? echo date('m-d-Y',strtotime('+4 days')); ?>
Output: 11-28-2008

Calculate Age

This trick is a fairly simple and straight forward one. All it does is find an age in years. Simply pass the year(s) you want to compare and it will return the age in year. This could easily be made into a function like getAge($yr1,$yr2);

<? echo floor(abs(strtotime('Y') - strtotime('1985'))/31536000
/*1 year in seconds*/); ?>
Output: 23

phpMyAdmin 3.1.0 Relesased

For those of you that are as hooked on phpMyAdmin as I am, you will be happy to know that there is a new version available. phpMyAdmin 3.1.0 includes a number of improvements and fixes.

The list of improvements includes security updates, language updates, export fixes, and general patches. The release notes highlight a few specific improvements: "this version has a new setup mechanism and supports BLOBstreaming and the Swekey hardware authentication" (release notes).

You can view the release notes for full details concerning the new release and download the new version from sourceforge.

How In-Demand Are Your Skills?

Have you ever wondered if your skills are in demand? With so many different possibilities available for web development, it is tough to know if you should keep mastering a specific technology or start learning a new one. After reading an article about how PHP programming jobs are in high demand, I asked myself the follow-up question: what other skills do I have that are in high demand, and which skills do I have that are not?

The article linked to a great resource from oDesk that lists the current (monthly) trends for many of the development and design skills used for web development. There are 84 different trends with a wealth of useful information about the skill's demand including graphs, stats, and recent job postings. Although these stats are pulled from information specifically on oDesk, it represents demand from all around the world. I have listed links to some of the most in-demand skills right now.

As a side note, this site is a fantastic example of how great the Google Chart API is.

Book Review: Sams Teach Yourself Ajax, JavaScript, and PHP All in One

Sams Teach Yourself books are great resources in a large array of topics. They have released Sams Teach Yourself Ajax, JavaScript, and PHP All in One in paperback version on July 12, 2008. This book is great for webmasters because it includes three of the most popular technologies used on the web.

Like all the rest of the "Sams Teach Yourself" line of books, this one is easy to follow, easy to understand, and takes you from a beginner to an intermediate and beyond. There are a number of visual aids including screenshots and diagrams to help you understand what you have read. Another great help is the amount of code examples used throughout the book; there are examples for most of the topics covered.

The book starts with a general overview at the beginning and works towards a more detailed explanation at the end. In chapter one, you will learn the basics of the web, HTML, CSS, and Ajax. By the end of chapter nine, you will understand JavaScript, using it to manipulate the DOM and add usability to your site. By chapter thirteen you will be using Ajax and by the end of chapter eighteen, you will be using Object Oriented PHP to make your site dynamic. Once you have taken all of that in, you can learn about XML responses, web services (REST and SOAP), and gain a basic understanding about a few popular Ajax tools and libraries that are available.

An added feature with this book is beyond the pages of helpful information. There is a CD provided with the book that includes all of the software needed to get a basic web server up and running. It includes XAMPP, jEdit, Prototype, Scriptaculous, Rico, XOAD, searchable PDF tutorials, and source code for the examples in the book. Most of these are freely available for download on the web, but it is nice to have them all in one place, especially for a beginner that doesn't know what to look for.

This book is well worth your time to read, especially if you are a beginner looking to get not only introduced to these web technologies, but also get started using them. You can pick up your copy at Amazon.com and get started building dynamic websites using Ajax, JavaScript, and PHP.

ZendCon 08 Slides Available On Zend Developer Zone

Zend Technologies held their fourth ZendCon conference, ZendCon 08, in Santa Clara, CA from September 15-18. In case you weren't able to attend (like me) or just want to see them again, the presentations from the conference are available online. You can view the ZendCon 08 Slides from the Zend Developer Zone website.

There are 60 different presentations available currently with a possibility of more on the way. They cover a wide range of topics including MySQL (Jay Pipes), Facebook development (Brett Harris), PECL (Elizabeth M Smith), scaling PHP applications (Shahar Evron), and many more. The presentations are broken down by the day they were presented and show you the speaker and session name. Here are a few of the presentations that I'll be checking out this weekend.

300+ PHP Presentations Online

The PHP Presentation System hosts over three hundred presentations, covering a slew of different topics including performance, testing, debugging, XML, and security. The presentations are from speakers at a variety of conferences. You will see (and hear if interested) presentations from speakers like Derick Rethans, Tobias Schlitt, Rasmus Lerdorf and many many more. These presentations have been at conferences all around the world and contain a great deal of information. Most presentations are between 25 and 40 slides. You will have to sift through some information in the presentations because some build on each other.

The presentations are best viewed in Firefox and are not available for use with IE. If you try to use IE you will get a pretty funny error code; "IE is not supported - please use Firefox, Safari, Konqueror or just about anything else" (view this presentation in IE). There are presentations dated as far back as 2002 and as recent as August 2008. Take a look around and see what's there.

My Top 10 Presentations

  1. Input Filter
  2. Webservices with PHP
  3. XML and PHP
  4. Myth Busting
  5. PHP Best Practices
  6. Web 2.0 and PHP 5
  7. XSS
  8. Help, I Found a Bug in my Code
  9. Hack Day
  10. Designing Good Databases

phpMyAdmin 2.11.8.1 Released Yesterday

.
phpMyAdmin

phpMyAdmin 2.11.8.1 was released yesterday. The main reason for the updated release is to patch bugs and security issues. There were some XSS problems reported by Aung Khant from the YGN Ethical Hacker Group. The two main fixes are both XSS related, one having to do with frames and the other has to do with a lack of HTML escaping. You can read more about these by visiting the site which contains the news release about these bug/security fixes from phpMyAdmin. These vulnerabilities were considered very serious and have now been patched with this new release. Be sure to upgrade your copy to 2.11.8.1. You can download the new release from SourceForge.

Unable to Use mysqldump on a Shared Host? Here’s an Alternative

Hard Drive Backup

I have used many shared hosting providers throughout my years as a webmaster. Shared hosts have many great points, namely, price, and then they have their major drawbacks. For me, one of the major drawbacks is the limited use of the machine on which your site is hosted.

For instance, I love phpMyAdmin for maintaining and managing my databases, but I am not a fan of the need to log in to backup the databases. The easiest way around that of course would be to execute the mysqldump command that comes bundled with MySQL. Unfortunately, most shared hosts, all that I have dealt with, disable the ability to use exec() or system() with PHP to execute the MySQL native command. If these were available, creating a cron job on a simple PHP file that ran mysqldump would have worked perfectly. However, this wasn't the case. After looking around for a long time at a possible way around this, I came up empty handed. Maybe I didn't look hard enough, or looked in all the wrong places, but I was left with nothing. I needed to back up my databases at regular intervals and found it frustrating to log into phpMyAdmin to do this each time. So I did what I should have done to begin with... wrote my own back-up tool.

It is pretty simple and self-explanatory. I did not create it as a function to be called, although you are more than welcome to do so. The main advantage to calling it, I think, would be to pass variables. For my needs, I simply set it up in the directory. I wanted it to run and back up whichever database needed to be. The script simply backs up the database specified in an sql file located in the same directory as the script itself. The file is named in this format, "DB_Backup_YourDatabase_YYYY-MM-DD.sql"; this could be easily modified to save anywhere you have permissions to save. The code is completely free and you can do what you would like with it. If you are feeling generous, throw a shout out my way or maybe even donate, but neither is not required.

This script was written and used on a server running PHP Version 4.4.2 and MySQL Version 4.1.22.

View Code | Download File

Commonly Used Regular Expressions

Here is a function that I have developed in PHP for checking to see if data is in a valid format. These are some of the common validations I have needed while programming various projects and websites. Regular expressions are some of the most powerful validation checks available in PHP, however they are not always easy to understand.

This function is used by passing 2 arguments: the type of validation check you want to be performed, and the data you want validated. The function itself uses a simple switch statement (a quicker and cleaner if/then statement). The function simply returns a boolean True or False.


function isValid($type,$var) {
$valid = false;
switch ($type) {
case "IP":
if (ereg("^([0-9]{1,3}\.){3}[0-9]{1,3}$",$var)) {
$valid = true;
}
break;
case "Email":
if (ereg("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$",$var)) {
$valid = true;
}
break;
case "URL":
if (ereg("^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu)$",$var)) {
$valid = true;
}
break;
case "SSN":
if (ereg("^[0-9]{3}[- ][0-9]{2}[- ][0-9]{4}|[0-9]{9}$",$var)) {
$valid = true;
}
break;
case "CC":
if (ereg("^([0-9]{4}[- ]){3}[0-9]{4}|[0-9]{16}$",$var)) {
$valid = true;
}
break;
case "ISBN":
if (ereg("^[0-9]{9}[[0-9]|X|x]$",$var)) {
$valid = true;
}
break;
case "Date":
if (ereg("^([0-9][0-2]|[0-9])\/([0-2][0-9]|3[01]|[0-9])\/[0-9]{4}|([0-9][0-2]|[0-9])-([0-2][0-9]|3[01]|[0-9])-[0-9]{4}$",$var)) {
$valid = true;
}
break;
case "Zip":
if (ereg("^[0-9]{5}(-[0-9]{4})?$",$var)) {
$valid = true;
}
break;
case "Phone":
if (ereg("^((\([0-9]{3}\) ?)|([0-9]{3}-))?[0-9]{3}-[0-9]{4}$",$var)) {
$valid = true;
}
break;
case "HexColor":
if (ereg("^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$",$var)) {
$valid = true;
}
break;
case "User":
if (ereg("^[a-zA-Z0-9_]{3,16}$",$var)) {
$valid = true;
}
break;
}
return $valid;
}

Example:

$email_address = $_POST["email"];
if (isValid("Email",$email_address)) {
echo "Valid Email Address;
} else {
echo "Invalid Email Address;
}

For more information, help, and examples visit RegExLib.com or check out the Mastering Regular Expressions book.