PHP
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 »
Posted July 1st, 2011
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 »
Posted March 5th, 2011
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.
Posted March 5th, 2011
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!
Posted February 27th, 2011
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
Posted November 5th, 2010