Archive for category Tech – Web Development
How to validate an email address format
Posted by Rui Miguel Feio in PHP on July 2, 2009
There are several ways of validating an email address format.
Normally, I do it the following way:
1. I first define the regular expression for the email format:
define (“FORMAT”,”^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$”);
2. To validate the email address provided is correct:
if ( !eregi (FORMAT, $email)) echo “The email provided has an invalid format”;
Regular expressions can be a pain to understand and if it’s hard for you then I would recommend you read “Sams Teach Yourself Regular Expressions in 10 Minutes” from Ben Forta.
If however you are simply interested in some practical examples that you could use then I recommend “Regular Expressions Cookbook” from Jan Goyvaerts and Steven Levithan.
PHP security considerations
Posted by Rui Miguel Feio in PHP on July 1, 2009
When developing a script wether if it’s in PHP or any other language you should always consider the security aspect. I’m sure the last thing you want is to get your script (and ultimately your data) hacked.
To make sure this does not happen there are a few steps you should pay attention at:
- Turn OFF the register_globals option
- Always declare variables in advance
- Check input type, length and format
- Use mysql_real_escape_string() before passing values to a MySQL query
- Use htmlentities() to convert characters to html entities before passing values to a MySQL query
- Turn OFF the error reporting and display
Here are some ways on how to achieve these:
1. To turn OFF the register_global option:
Edit your php.ini and turn it OFF or if you don’t have access to it add the following to the .htaccess file in your server:
php_flag register_globals 0
2. Always declare variables in advance:
Define default values to all the variables you’re going to use in the beginning of your script.
3. Check input type, length and format:
Always make sure that the value of the variable is exactly what the script should expect.
Checking type (example):
if ($settype($var, ‘integer’)) exit(“$var is an invalid value”);
Checking length (example):
if(strlen($var) > 20) exit (“$var has to have a maximum of 20 characters”);
Checking format (example):
$format = “^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$”;
if(!eregi($format, $email)) exit(“$email is not a valid email address”);
4. Use mysql_real_escape_string() before passing values to a MySQL query
Escape the content of your variables before passing them to a MySQL Query to sanitize the values passed to your data base:
$escaped = mysql_real_escape_string($var);
$action = “SELECT * FROM table WHERE field = ‘$escaped’ “;
5. Use htmlentities() to convert characters to html entities before passing values to a MySQL query
Another way to sanitize the values passed to your data base is by using the htmlentities() function. This converts special characters to their corresponding HTML code:
$convert= htmlentities($var);
$action = “SELECT * FROM table WHERE field = ‘$convert’ “;
6. Turn OFF the error reporting and display
You should never allow the system to show the error messages in your live production server because these messages can provide precious information about your system.
You can either set both error_reporting and display_errors to 0 in php.ini or when you execute the scripts with error_reporting(0) and display_errors(0).
If you would like to know more about PHP Security I recommend the book “Pro PHP Security” from Chris Snyder.
How to identify a MySQL query problem
Posted by Rui Miguel Feio in MySQL, PHP on July 1, 2009
Sometimes you happen to have a problem with one of your MySQL queries and you simply can’t seem to understand why.
Well, one thing that helps is to use the mysql_error() function.
This function returns the message of the MySQL error you’re getting.
Example:
$action = “INSERT INTO table (name, address, dob) VALUES (‘$name’, ‘$address’, ‘$dob’)”;
$query = mysql_query($action) or die(mysql_error());
If we happen to get a problem with our MySQL error the script execution will stop (by using the die() function) and the MySQL error message will be returned (by using the mysql_error() function).
PHP Session not being passed from a web site to WordPress
Posted by Rui Miguel Feio in PHP, WordPress on June 30, 2009
Recently I had this case where a PHP session ($_SESSION) was not being passed from a web site to WordPress.
Basically, WordPress was installed and configured to be part of a web site and there was the need to use Sessions to control the login/logout process of the site.
Although the session_start() had been defined to WordPress, the latter did not recognize the session values and therefore simply ignored if a user was logged on.
After some research and some brainstorming with some great experts on the field, I realized that the problem was in the WordPress URL itself.
The problem was that although the domain was the same, WordPress was suppressing the “www” from the URL which caused the $_SESSION values not to be recognized.
Example:
Site URL: http://www.this-is-an-example.com
WP URL: http://this-is-an-example.com (missing the “www.”)
Solution:
By adding the “www.” to the WordPress domain name the problem was solved:
- Login to WordPress Admin area
- Go to Settings – General
- Add “www.” to the WordPress address (URL) and Blog address (URL).
Work with records older then x days
Posted by Rui Miguel Feio in MySQL on June 29, 2009
For the sake of this example, let’s assume that:
- we want to work with records older then 60 days
- the table name is ‘table’ (how original!)
- the table field with dates is called date_field
DELETE FROM table WHERE date_field < DATE_SUB(CURDATE(),INTERVAL 60 DAY)
This MySQL command deletes all table rows whose date is older then 60 days from current date – CURDATE() -.
SELECT something FROM table WHERE date_field< DATE_SUB(CURDATE(),INTERVAL 60 DAY)
This MySQL command selects all table rows whose date is older then 60 days from current date – CURDATE() -.
Explanation:
DATE_SUB(date,INTERVAL expr unit) -> Subtracts two dates
CURDATE() -> Returns the current date
unit can be:
- MICROSECOND
- SECOND
- MINUTE
- HOUR
- DAY
- WEEK
- MONTH
- QUARTER
- YEAR
- SECOND_MICROSECOND
- MINUTE_MICROSECOND
- MINUTE_SECOND
- HOUR_MICROSECOND
- HOUR_SECOND
- HOUR_MINUTE
- DAY_MICROSECOND
- DAY_SECOND
- DAY_MINUTE
- DAY_HOUR
- YEAR_MONTH
Follow Me!