Archive for category PHP

Notice: Undefined index: message in /home/example.php on line 9

If  you’re getting this message on PHP this is due to your PHP error reporting settings not being set correctly.

You can suppress the Notice warnings by changing the error reporting settings on PHP.ini (permanent change) or adding an extra line to your PHP script.

Changing PHP.ini:

Changing your PHP.ini makes the change permanent and available to every PHP script you run.

  1. Edit your PHP.ini file
  2. Locate the line that has error_reporting without the ; in the beginning (ex: error_reporting = E_ALL)
  3. Change this to error_reporting = E_ALL & ~E_NOTICE
  4. Save PHP.ini

Adding line to your script:

Just add the following line to the beginning of your script:

error_reporting (E_ALL ^ E_NOTICE);

Explanation:

error_reporting = E_ALL & ~E_NOTICE tells the system to show all errors and warnings except those for notices.

  • email
  • Add to favorites
  • Facebook
  • Twitter
  • MySpace
  • del.icio.us
  • LinkedIn
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • Yahoo! Buzz
  • IndianPad

No Comments

Backup and Restore a web site in CPANEL

It’s a good practice to make backups prior to submit any changes to your site.

Time and again people skip the backup to find themselves with problems and not having an easy and quick way to undo their changes. This may not seem a big problem, but if the changes were done to the web site of your business and now the web site is not working probably, this means you’re losing money!

Backing up a site is easy and even more if your web server uses CPANEL.

Consider the following steps to BACKUP your site:

  • To Backup the files of your site:
    1. On CPANEL select option Backups
    2. Under Home Directory select option Download a home directory Backup
    3. Select Save File
  • To Backup the Databases of your site:
    1. On CPANEL select option Backups
    2. Under Download a MySQL Database Backup select the Database you wish to Backup
    3. Select Save File
    4. Repeat Steps 2 and 3 for any other Database you wish to backup

Consider the following steps to RESTORE your site:

  • To Restore the files of your site:
    1. On CPANEL select option Backups
    2. Under Restore a Home Directory Backup select Browse and locate the backup you want to restore from
    3. Select Upload
  • To Restore the Databases of your site:
    1. On CPANEL select option Backups
    2. Under Restore a MySQL Database select Browse and locate the backup you want to restore from.
    3. Select Upload
    4. Repeat Steps 2 and 3 for any other Database you wish to restore
  • email
  • Add to favorites
  • Facebook
  • Twitter
  • MySpace
  • del.icio.us
  • LinkedIn
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • Yahoo! Buzz
  • IndianPad

No Comments

How to validate an email address format

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.


  • email
  • Add to favorites
  • Facebook
  • Twitter
  • MySpace
  • del.icio.us
  • LinkedIn
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • Yahoo! Buzz
  • IndianPad

No Comments

PHP security considerations

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:

  1. Turn OFF the register_globals option
  2. Always declare variables in advance
  3. Check input type, length and format
  4. Use mysql_real_escape_string() before passing values to a MySQL query
  5. Use htmlentities() to convert characters to html entities before passing values to a MySQL query
  6. 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.


  • email
  • Add to favorites
  • Facebook
  • Twitter
  • MySpace
  • del.icio.us
  • LinkedIn
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • Yahoo! Buzz
  • IndianPad

No Comments

How to identify a MySQL query problem

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).

  • email
  • Add to favorites
  • Facebook
  • Twitter
  • MySpace
  • del.icio.us
  • LinkedIn
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • Yahoo! Buzz
  • IndianPad

No Comments

PHP Session not being passed from a web site to WordPress

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:

  1. Login to WordPress Admin area
  2. Go to SettingsGeneral
  3. Add “www.” to the WordPress address (URL) and Blog address (URL).
  • email
  • Add to favorites
  • Facebook
  • Twitter
  • MySpace
  • del.icio.us
  • LinkedIn
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • Yahoo! Buzz
  • IndianPad

11 Comments