Validating email addresses in PHP with Preg filters, DNS, MX Servers and other checks

UPDATE: It is now easier to send a confirmation email to the provided address to validate it, or to use a framework than a custom script.

There are many tutorials online that show users how to validate an email address, but most do it wrong. This means many websites will reject valid addresses such as customer/[email protected] or abc!def!xyz%[email protected] (yes, they are valid!) with the following expression:

Continue reading “Validating email addresses in PHP with Preg filters, DNS, MX Servers and other checks”

SPF Records for Google Apps Hosted Mail – avoiding rejected emails

Using Google Apps for your domain’s email? Well you definately need to set up some SPF records.
It seems yahoo is extremely strict when it comes to checking for SPF records on a domain, and bounces anything with missing records. Many other email providers like hotmail simply SPAM your message straight away.

Google provide the following resource for this problem: http://www.google.com/support/a/bin/answer.py?hl=en&answer=33786

But, they recommend using

v=spf1 include:aspmx.googlemail.com ~all

That’s all fine and well…but what about the official website? Oddly, they recommend using – instead of ~ (http://www.openspf.org/FAQ/Common_mistakes)

v=spf1 include:aspmx.googlemail.com -all

But seeing as it’s google hosting my mail, I’ve been using ~ successfully for some time now, with no more bounces.

Testing whether you’re configured correctly

Easy, just send an email to [email protected] and you’ll get a bounce right back with the SPF status in (http://www.openspf.org/Tools).

How can I configure my server?

If you have access to all your DNS records for your domain you can add it yourself (for example through WHM or root plesk panel) but on most shared hosts just fire off an email to the support team who will add the record for you.

On Plesk & Cpanel you can add either a SPF record or a TXT record through your DNS editor, making it easy to do this yourself.

Automatic Local, FTP and Email Backups of MySQL Databases with Cron

I’ll start by saying this is not all my own code, it is based on dagon design’s original release in 2007 (Automatic MySql Backup Script) but this version builds on their version 2.1 to add FTP support.

What this script does:

  • Backup all of your MySQL databases on a server individually, then package them into a single tar.
  • Save that tar locally, on a FTP server or even email it to you

What you need:

  • PHP
  • MySQL
  • An account with relevant mysql access (it needs access to the databases you wish to backup)

Download:

Zip containing dbbackup.php & dbbackupconfig.php – dbbackup.zip

What I’ve added:

To upload to a remote FTP server, I added this to the config file:

######################################################################
## FTP Options
######################################################################

// Use FTP Option?
$useftp = true;

// Use passive mode?
$usepassive = true;

// FTP Server Address
$ftp_server = 'host';

// FTP Username & Password
$ftp_user_name = 'username';
$ftp_user_pass = 'password';

and this to the main file below email sending:

// do we ftp the file?
if ($useftp == true) {
$file = $BACKUP_DEST.'/'.$BACKUP_NAME;
$remote_file = $BACKUP_NAME;

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// turn passive mode on?
ftp_pasv($conn_id, $usepassive);

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
 echo "successfully uploaded to ftp: $remotefile\n";
} else {
 echo "There was a problem while uploading $remotefile\n";
}

// close the connection
ftp_close($conn_id);
}

That’s all!

Stripping everything but letters and numbers from a string in PHP with preg_replace

Useful for a number of things including username and anything else you don’t want ANY special chars in, leaving only alphanumeric digits.

<?php
$string = 'us$$er*&^[email protected]@e';
$string = cleanabc123($string);
function cleanabc123($data)
{
$data = preg_replace("/[^a-zA-Z0-9\s]/", "", $data);
return $data;
}
// This will be 'username' now
echo $string;
?>

And if you wanted it to also remove whitespace, you could change the function by removing the \s whitespace character.

$data = preg_replace("/[^a-zA-Z0-9]/", "", $data);

Remove whitespace (spaces) from a string with PHP and preg_replace

Seeing as ereg_replace and that whole family has been depreciated in the newer PHP releases it’s surprising how many sites still show and use it.

This simple function strips out whitespace from a string. Could be useful in taking spaces out of usernames, id’s, etc.

<?php
$string = 'us er na  me';
$string = nowhitespace($string);
function nowhitespace($data) {
return preg_replace('/\s/', '', $data);
}
// This will be 'username' now
echo $string;
?>

You could use this to make all whitespace only 1 space wide by changing the ” in the following line.

return preg_replace('/\s/', ' ', $data);

Last example modified from the PHP manual.

Using PHP to strip a user IP address into subnets and filter out characters

Found it very difficult to find a simple solution to this problem – you want to display the submittor’s IP address on a webpage where it will be publicly available, but obfuscate some characters.

You want 255.255.255.255 to become 255.255.255.x or something, right?

Preg_replace() an IP

So, assuming we have the user’s IP address, for this we could use the following server variable:

// Displays user IP address
$ip =  $_SERVER['REMOTE_ADDR'];

We can manipulate this with preg_replace.

// Displays user IP address with last subnet replaced by 'x'
$ip = preg_replace('~(\d+)\.(\d+)\.(\d+)\.(\d+)~', "$1.$2.$3.x", $ip);

So the code to get and change this IP would be:

// Sets user IP address
$ip =  $_SERVER['REMOTE_ADDR'];
// Sets user IP address with last subnet replaced by 'x'
$ip = preg_replace('~(\d+)\.(\d+)\.(\d+)\.(\d+)~', "$1.$2.$3.x", $ip);
// Displays user IP address as altered.
echo $ip;

Of course, if you’re happy to just cut off the end then you can use the substr function to trim a number of characters off.

I’ll revise this to validate and deal with proxies another time.