PHP Error Handling

Posted on Updated on

PHP Error Handling

The default mistake taking care of in PHP is extremely basic. A mistake message with filename, line number and a message depicting the blunder is sent to the program.

At the point when making scripts and web applications, mistake taking care of is a vital part. On the off chance that your code needs mistake checking code, your project might look exceptionally amateurish and you might be interested in security dangers.

This instructional exercise contains the absolute most regular mistake checking routines in PHP.

We will indicate diverse mistake taking care of routines:

Straightforward “bite the dust()” explanations

Custom blunders and mistake triggers

Blunder reporting

Essential Error Handling: Using the bite the dust() capacity

The main sample demonstrates a straightforward script that opens a content document:

<?php

$file=fopen(“welcome.txt”,”r”);

?>

On the off chance that the record does not exist you may get a blunder like this:

Cautioning : fopen(welcome.txt) [function.fopen] : neglected to open stream :

No such record or registry in C:\webfolder\test.php on line 2

To keep the client from getting a mistake message like the one above, we test whether the record exist before we attempt to get to it:

<?php

if(!file_exists(“welcome.txt”)) {

die(“File not found”);

} else {

$file=fopen(“welcome.txt”,”r”);

}

?>

Presently if the record does not exist you get a mistake like this:

Document not found

The code above is more effective than the prior code, on the grounds that it utilizes a straightforward mistake taking care of instrument to stop the script after the blunder.

In any case, essentially ceasing the script is not generally the right approach to go. How about we investigate elective PHP capacities for taking care of mistakes.

Making a Custom Error Handler

Making a custom mistake handler is very basic. We essentially make an exceptional capacity that can be called when a mistake happens in PHP. This capacity must have the capacity to handle at least two parameters (blunder level and mistake message) however can acknowledge up to five parameters (alternatively: record, line-number, and the mistake connection):

Grammar

error_function(error_level,error_message,

error_file,error_line,error_context)

Parameter Description

error_level Required. Indicates the blunder report level for the client characterized mistake. Must be a quality number. See table beneath for conceivable mistake report levels error_message Required. Indicates the blunder message for the client characterized mistake error_file Optional. Indicates the file name in which the blunder happened error_line Optional. Indicates the line number in which the blunder happened error_context Optional. Indicates a cluster containing each variable, and their qualities, being used when the blunder happened Mistake Report levels.

These mistake report levels are the diverse sorts of blunder the client characterized mistake handler can be utilized for:

Value                             Constant                                                    Description

2                                     E_WARNING                                           Non-lethal run-time mistakes. Execution of the script is                                                                                                               not stopped 8 E_NOTICE Run-time takes note. The                                                                                                                       script discovered something that may be a mistake,                                                                                                                         however could likewise happen while running a script                                                                                                                   typically

256                                 E_USER_ERROR                                 Fatal client produced mistake. This is similar to an                                                                                                                          E_ERROR set by the software engineer utilizing the PHP                                                                                                              capacity trigger_error()

512                                E_USER_WARNING                            Non-deadly client created cautioning. This is similar to an                                                                                                             E_WARNING set by the software engineer utilizing the                                                                                                                 PHP capacity trigger_error()

1024                              E_USER_NOTICE                                User-produced notice. This is similar to an E_NOTICE set                                                                                                            by the developer utilizing the PHP capacity trigger_error                                                                                                               ()

4096                              E_RECOVERABLE_ERROR               Catchable deadly blunder. This is similar to an E_ERROR                                                                                                             yet can be gotten by a client characterized handle (see                                                                                                                    likewise set_error_handler())

8191                                E_ALL                                                       All blunders and notices (E_STRICT turned into a piece                                                                                                                 of E_ALL in PHP 5.4)

Presently lets make a capacity to handle blunders:

capacity customError($errno, $errstr) {

reverberation “<b>Error:</b> [$errno] $errstr<br>”;

reverberation “Finishing Script”;

kick the bucket();

}

The code above is a basic blunder taking care of capacity. When it is activated, it gets the blunder level and a mistake message. It then yields the mistake level and message and ends the script. Since we have made a blunder taking care of capacity we have to choose when it ought to be activated.

Set Error Handler

The default mistake handler for PHP is the implicit blunder handler. We are going to make the capacity over the default blunder handler for the term of the script. It is conceivable to change the mistake handler to apply for just a few blunders, that way the script can deal with distinctive blunders in diverse ways.

On the other hand, in this illustration we are going to utilize our custom blunder handler for all mistakes:

set_error_handler(“customError”);

Since we need our custom capacity to handle all blunders, the set_error_handler() just required one parameter, a second parameter could be added to indicate a mistake level.

Case

Trying so as to test the blunder handler to yield variable that does not exist:

<?php

/blunder handler capacity

capacity customError($errno, $errstr) {

reverberation “<b>Error:</b> [$errno] $errstr”;

}

/set blunder handler

set_error_handler(“customError”);

/trigger blunder

echo($test);

?>

The yield of the code above ought to be something like this:

Blunder: [8] Undefined variable: test

Trigger an Error

In a script where clients can enter information it is helpful to trigger mistakes when an illicit data happens. In PHP, this is finished by the trigger_error() capacity.

Illustration

In this illustration a mistake happens if the “test” variable is greater than “1”:

<?php

$test=2;

on the off chance that ($test>1) {

trigger_error(“Value must be 1 or beneath”);

}

?>

The yield of the code above ought to be something like this:

Notice: Value must be 1 or beneath in C:\webfolder\test.php on line 6

A mistake can be activated anyplace you wish in a script, and by including a second parameter, you can indicate what blunder level is activated.

Conceivable mistake sorts:

E_USER_ERROR – Fatal client produced run-time mistake. Blunders that can not be recuperated from. Execution of the script is stopped E_USER_WARNING – Non-lethal client produced run-time cautioning. Execution of the script is not stopped E_USER_NOTICE – Default. Client produced run-time notice. The script discovered something that may be a mistake, yet could likewise happen while running a script typically.

Sample

In this sample an E_USER_WARNING happens if the “test” variable is greater than “1”.

In the event that an E_USER_WARNING happens we will utilize our custom blunder handler and end the script:

<?php

/mistake handler capacity

capacity customError($errno, $errstr) {

reverberation “<b>Error:</b> [$errno] $errstr<br>”;

reverberation “Finishing Script”;

bite the dust();

}

/set blunder handler

set_error_handler(“customError”,E_USER_WARNING);

/trigger blunder

$test=2;

on the off chance that ($test>1) {

trigger_error(“Value must be 1 or below”,E_USER_WARNING);

}

?>

The yield of the code above ought to be something like this:

Blunder: [512] Value must be 1 or underneath

Finishing Script

Since we have figured out how to make our own particular mistakes and how to trigger them, lets investigate blunder logging.

Blunder Logging

As a matter of course, PHP sends a mistake log to the server’s logging framework or a document, contingent upon how the error_log design is set in the php.ini record. By utilizing the error_log() capacity you can send blunder logs to a predetermined document or a remote destination.

Sending blunder messages to yourself by email can be a decent method for getting advised of particular mistakes.

Send an Error Message by E-Mail

In the sample underneath we will send an email with a mistake message and end the script, if a particular blunder happens:

<?php

/mistake handler capacity

capacity customError($errno, $errstr) {

reverberation “<b>Error:</b> [$errno] $errstr<br>”;

reverberation “Website admin has been advised”;

error_log(“Error: [$errno] $errstr”,1,

“someone@example.com”,”From: webmaster@example.com”);

}

/set mistake handler

set_error_handler(“customError”,E_USER_WARNING);

/trigger mistake

$test=2;

on the off chance that ($test>1) {

trigger_error(“Value must be 1 or below”,E_USER_WARNING);

}

?>

The yield of the code above ought to be something like this:

Mistake: [512] Value must be 1 or beneath

Website admin has been told Furthermore, the mail got from the code above resembles this:

Mistake: [512] Value must be 1 or beneath

This ought not be utilized with all mistakes. Customary blunders ought to be signed on the server utilizing the default PHP logging framework.

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s