Validating email addresses
Email address validation is a common issue in any language, but it's more common in those oriented to create web pages like PHP.
The standar for email address naming is the RFC 2882, and it's long, long and complicated. Too complicated to fit in a regular expression, so I don't recommend anybody to try a regex implementation of the full standar, it's simply impossible.
The best approach to solve this particular problem is to write a small and understandable regular expression that accepts some invalid email addresses but catches all the valid ones. Is better to let some inexistent emails than don't let an user register with his completely valid and working email.
Double check: one using regex and once passed validate the email address on the only way that you'll now that is valid for sure: send an email containing an activation link.
Since we are going to double check is OK to trust the client and do the first validation using JavaScript. Once passed we'll send an email to that address on the server side, and it won't be confirmed until the activation link is clicked, so as I said before: is OK if you get some invalid email address.
Keeping it simple: I only check for something followed for a @ then another something with a dot and a final something (with any characters and even more dots!).
The above regular expression will:
Match
- user@mail.com
- user+mail@mail.com
- user+mail@mail.co.uk
- mail@mail.commm
- user/mail@gmail.com
- user+mail@.com
- @mail.com
- user@mail
As said, it will match invalid email addresses, just confirm them sending an email, the rest is just meaningless effort to solve a problem that cannot be solved using regular expressions.
Cookies, cURL and PHP without a jar
cURL is one of the best ways of implementing the client side of a RESTful service with PHP, is also really handy to take some data from other websites and do with it whatever you want.
Taking this data can be a problem when you need to autenticate on the third party website using cookies. Rest supports sending and retrieving them, but all the tutorials I've found use txt files to cache those cookies for being able to send them back to the server.
I don't like this approach, so let's see how to grab and store them on-the-fly using a simple regular expresion. First, let's setup a simple authentication page in PHP.
We're going to do at least two connections to the target server, the first one will send the password to the form and grab the cookie, the second one will call again the same server but this time we will send the cookie along our petitions, so we can navigate through private pages like we were a normal user.
mflowDet: mflow widget for WordPress
View download links and changelog info.
Do you know what is mflow? As many people said, in few words, is the son of Twitter and iTunes. Right now it's only available on the UK, but probably will spread across Europe soon. You can read more and try it on it's website.
I just coded a little widget for WordPress that grabs the last flow from your mflow web profile (http://my.mflow.com/username) and adds it to your sidebar.
I used PHP Simple HTML DOM Parser to parse the HTML and played a bit with the data to add the markup and code it.
As long as I am grabbing the data from a server that I don't own, I am using a little cache system. This is not mandatory, but it would be nice to let it work if you install the plugin in your server. The cache is maintained 5 minutes and it's saved on a text file under a folder named "_cache" on the plugin folder. This folder needs to be writable in order to put the data there.
The widged only has one configuration option: your mflow username. Remember that this is case sensitive. If you don't change it you will show my flows.
This was a 15 minutes job, so it's a bit dirty and has no error handling. I'll take a couple of days to clean the code and make it more maintainable, hopefully this won't be really necessary as mflow will probably be delivering it's own widget soon.
This was no more than a proof of concept and a quick test to the parser. This week I will be post a little article explaining how it works.
You can download here the latest version of the widget. The installation is simple: drop the mflowdet folder into your plugins folder and add the widget on the Appearance section of your WordPress admin panel, there you can change the username.
Changelog
1.2 (Download)
- PHP4+ Compatibility added
- Removed PHP Simple HTML DOM Parser
- Uses the mflow API to grab the data
- Cleaner code
- Pre custom HTML built
- Plugin cache has been removed as I am grabbing the data from a web service. I'll add it again if needed.
1.1 (Download)
- Removed classes and objects from the plugin.
- Changed the data array to made it more understandable
- Minor bug fixes
1.0 (Not available)
- First version
Requeriments
- PHP4+
- WordPress 2.7.x or higher
- PHP Simple HTML DOM Parser (included)
Roadmap
- Add an option to output custom HTML
- Add error handling for the web service and the plugin itself
Known Issues
- Fails when the last flow is an album (only occurs when buying and flowing the whole album)