Categories
PHP Programming

Fwds.Me: A Simple URL Shortener

Awhile ago on the internet, there was a big debate going on about the value of URL shortening services like Bit.ly.  I was following the debate with some interested when I decided to create my own URL shortener.  It does only one thing, and that is shorten URLs.  Fwds.Me is still young, so the possibility of getting REALLY short URLs is very real.  If you are looking for a simple url shortener, then look no further than Fwds.Me.

Go To Fwds.Me

Categories
Wordpress Development

How to Enable Multisite in WordPress 3

One of the most touted new features in WordPress 3 is the integration of WordPress MU into the main branch of WordPress.  What this allows you to do is run multiple WordPress sites or blogs, while only needing one install.  The only issue is that this functionality is not enabled by default.

To enable multisite in WordPress 3, you need to add the following line somewhere in wp-config.php. [Note:  Make sure you disable all of your plugins first.] 

define('WP_ALLOW_MULTISITE', true);

After you set WP_ALLOW_MULTISITE to true, you’ll get a new menu item called “Network” under the tools menu.


From there, you can experiment setting up new sites.

Categories
PHP Programming

Validate Email Addresses With PHP

If you’re a web programmer, there will come a time when you need to validate an email address. It’s going to happen, so just accept it. In newer versions of PHP, there is built in functionality for this. However, for those of us not lucky enough to be running the latest and greatest version, we can use regular expressions.

The following PHP function will validate email addresses using regular expressions. True is returned on success, and false is returned otherwise.

1
2
3
function validate_email($email) {
return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email);
}
Categories
PHP Programming

PHP Validate Email

Every so often (ok, a lot more than that), you need to validate an email address. The obvious solution is to use regular expressions, however PHP provides a better method using the filter_var() function.

To validate an email address using PHP, simply do the following:

1
2
3
4
5
6
$email = "jack@re-cycledair.wploadtest.xyz";
if(filter_var($email, FILTER_VALIDATE_EMAIL) == TRUE) {
     echo "Valid Email.";
} else {
     echo "Email is not valid.";
}

Note: This only works for PHP >= 5.2

Categories
PHP Programming

PHP Function Of The Day: ob_end_clean()

Sometimes you get in to a situation where you are working in an environment that drops everything into an output buffer before it spits it out the browser. In most languages this is called “output buffering”. The problem with having EVERYTHING buffered is being able to do special stuff like dynamic XML documents.

My solution to this little pickle was to just drop everything from the current output buffer and then kill the process after I’m done with it. To do this, just execute “ob_end_clean()” right before the functions/objects/code you need to execute. What “ob_end_clean()” does is drops all information that is currently stored in the output buffer, and then stops the buffering anything after it.

One “gotch ya” moment I had using this function was that it doesn’t end ALL cases of output buffering. If for some strange reason there is nested buffering going on, you’ll need to call the function as many times as it takes to get to the top of the call stack.

http://us2.php.net/manual/en/function.ob-end-clean.php

Categories
PHP Programming

Converting MySQL dateTime to RFC-822 (RSS pubDate) in PHP

I had the need to convert a MySQL datetime time stamp into a format accepted by the RSS 2.0 specification. To get the first part, you can do a SQL query like the following:

1
SELECT DATE_FORMAT(dateTimeColumn, '%a, %d %b %Y %T') AS rssPubDate FROM yourTable

After that, you need to get the timezone, which can be accomplished by using this:

1
$timeZone = date('T');

So lets say you store the rssPubDate from MySQL in a variable called $rssPubDate, all you need to do is $rssPubDate .= ” {$timeZone}”;

That’s it! You now have a RFC-822 compliant time stamp.

Categories
PHP Programming

Showing All Errors in PHP

When I was first starting to create web sites with PHP, I struggled with getting error reporting turned on.  Turns out, that you can have it enable at the server, or in the document, or in both.  For development, it’s probably a good idea to have this at the top of script:

error_reporting(E_ALL);
ini_set('display_errors', '1');

This will allow php to display all errors, notices, and warnings.  Simple things like developing with error reporting full on like this help prevent security breaches and unknown application behavior.

Categories
PHP Programming

PHP Type Evaluation

Aside from grad school drama, I learned something new the other day about PHP.  In PHP, there are two ways to look for equality: “==” and “===”.

$a = “1”;
$b = 1;
if($a == $b) { echo “True”; } else { echo “False”; }
if($a === $b) { echo “True”; } else { echo “False”; }

The first if statement would evaluate to true because it only evaluates the value.  The second if statement would evaluate to false, because it evaluates both value AND type.