Skip Navagation

Full On Design

A Web Development & Technology Blog

 

PHP

PDO (PHP Data Objects) – Starter Guide

It may surprise you to hear, that using the mysql_connect() function in PHP has recently be marked as “old hat” coding because it’s slow in comparison with newer methods. A better alternative is PDO (PHP Data Objects), a lightweight method for accessing databases. Here is a quick overview to help you get started with PDO.

Reasons to use PDO

  • It’s Fast - it talks to the database via a database specific PDO-driver.
  • It’s Object Oriented – The methods within the class are the same for each database driver, so you can easily change your database driver without lots of extra coding.
  • It’s Flexible-  You can easily change between such database drivers as PostgreSQL, MySQL or SQLite by pretty much just changing your construct statement.
  • It’s Safer – PDO encourages you to bind parameters to your SQL query’s, meaning that it’s significantly less likely your website will suffer from a SQL injection based attack.

Read the rest of this entry »

PHP’s Ctype Functions

A few days ago I was researching methods of validating alphanumeric strings and I was shown PHPs Ctype Functions by Sebastian Roming.

In a nutshell they are a group of PHP functions why can be used to check strings to see if they are alphanumeric, numeric etc without using regex (So they are fast!). The key thing to remember is that they return TRUE or FALSE, unlike filter_var which returns FALSE or the string.
Here is the code example:

<?php
// Check if input is alphanumeric (letters and numbers)
ctype_alnum('abcdef1234'); // This returns TRUE
ctype_alnum('£%^&ab2'); // This on the otherhand returns FALSE

// check if input is alpha (letters)
ctype_alpha('dssfsdf'); // returns TRUE
ctype_alpha('12345dssfsdf'); // Returns FALSE

// Check if the input is numeric
ctype_digit('1234'); // TRUE
ctype_digit('1as2d34f'); // FALSE
?>

Read the rest of this entry »

How to validate an email address with PHP

Validating an email address is a great way to reduce spam on your website, but there are several methods to do it. You could use a messy regex approach or alternatively you could also use PHP built in functions, it’s really up to you. Here is the function I use:

<?php
function validEmail($email){
	// Check the formatting is correct
	if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
		return FALSE;
	}

	// Next check the domain is real.
	$domain = explode("@", $email, 2);
	return checkdnsrr($domain[1]); // returns TRUE/FALSE;
}

// Example
validEmail('real@hotmail.com'); // Returns TRUE
validEmail('fake@fakedomain.com'); // Returns FALSE
?>

Free feel to copy/share.

Getting the Weather in PHP

A few months ago I made a small PHP class which can get the weather based on latitude and longitude. Anyway you can now download it and contribute to it over at Google Code.

Here is a code example of how to get the weather:

$w = new Weather('50.799995', '-1.065545'); // Input the Latitude and Longitude
echo $w->getLocation()->getWeather()->sayHuman();
// Ouput~: Portsmouth, England, PO4 8 | Partly Cloudy 4°C, Humidity: 93%, Wind: N at 8 mph

Enjoy!

Twitter OAuth Class 1.0 Released

A few weeks ago I wrote a neat little PHP Class which makes talking to twitter via their API really easy for Socialize This. You can download it and contribute to it, on it’s Google Project homepage. Here are a few quick examples of how to use it:

Update you Timeline (Send a tweet)

$t->updateStatus('Messing about with my Twitter API script');

Get your Mentions

$t->mentions();

Get a users timeline

$t->getTimeline('Rogem002');

Enjoy :)