Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Some question about 'error_reporting(E_ALL)' in php

Posted on 2016-11-21
5
Medium Priority
?
129 Views
Last Modified: 2016-11-22
Hello;

I have few questions about error_reporting(E_ALL);:

1-Where we must put this php function in a script,at the starting of codes?

2-If i check my page without this function and see that there is no error,does it need to add?

3-If the page which has this function has error,does it return the error in the same page?

4-If i have several script php in a page does it need that i put it for each script?

Thanks
0
Comment
Question by:MOSTAGHASSI
  • 3
  • 2
5 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 41896353
I recommend putting error_reporting(E_ALL) at the very start, such as the top of a common script that is loaded first, in every PHP page.  The error_reporting() setting can be considered permanent throughout the entire HTTP request -- it will affect all PHP scripts that are part of the same request (but not AJAX scripts or other asynchronous scripts, these need their own error_reporting() settings).

You can also change the PHP.ini error_reporting parameters.  The typical (default) installation is E_ALL ~ E_NOTICE, meaning that you will not get any of the Notice messages.  This is a simply terrible setting because you may miss such dangerous things as relying on an undefined variable.  Here is the trade-off between writing your own error_reporting() statement and updating PHP.ini.  If you get used to relying on php.ini, and you move to a new server, you will not get any notice about the changed error_reporting() level!  So it is more convenient to just update php.ini and forget about it, but IMHO it may be risky.

The error reports are created in the PHP script  that encounters the error.  They can be displayed to the browser with something like this:
ini_set('display_errors', TRUE);

Open in new window

and they can be logged to the server with something like this:
ini_set('error_log', 'error_log');
ini_set('log_errors', TRUE);

Open in new window

and you can use both settings to display and log errors.

You probably do not need to put error_reporting(E_ALL) in every script, but I would still do it as a matter of "good habits."

When you have a site up and running, you probably do not want to send error messages to the browser display, just to the error_log file.  This script will find all of the error_log files.  You can put it in the web root and it will traverse the directory tree, looking in every sub-directory.  You probably want to password-protect something like this.
<?php // demo/find_error_log.php
/**
 * Put this script in the web root directory
 *
 * Traverse all directories down the web tree
 * Show and optionally delete the error log files
 *
 * http://php.net/manual/en/class.recursivedirectoryiterator.php#85805
 */
ob_start();
error_reporting( E_ALL );
ini_set( 'display_errors', TRUE );
ini_set( 'log_errors',     TRUE );


// START IN THE CURRENT DIRECTORY
$path = realpath(getcwd());
$plen = strlen($path);

// THE ERROR LOG FILE NAME
$signal = strrev(ini_get('error_log'));

// IF THERE IS A POST-METHOD REQUEST TO DELETE THIS ERROR LOG
if (!empty($_POST['log']))
{
    // MAKE SURE WE ONLY UNLINK THE ERROR LOG FILE
    $test = strrev($_POST['log']);
    if (strpos($test, $signal) === 0)
    {
        @unlink($path . $_POST['log']);
        echo '<h3>' . $_POST['log'] . ' Discarded</h3>';
    }
}


// COLLECT THE DIRECTORY INFORMATION OBJECTS
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);


// ITERATE OVER THE OBJECTS
foreach($objs as $name => $obj)
{
    // PROCESS THE ERROR LOG ONLY
    $test = strrev($name);
    if (strpos($test, $signal) === 0)
    {
        // CREATE A DELETE BUTTON FOR THIS ERROR LOG
        $name = substr($name, $plen);
        $form = <<<EOD
<form method="post" style="margin:0; padding:0; display:inline;!important">
<b>$name</b>
<input type="submit" value="Discard?" />
<input type="hidden" name="log" value="$name" />
</form>
EOD;
        echo $form;

        // SHOW THE CONTENTS OF THIS ERROR LOG
        echo '<pre>';
        print_r(file_get_contents($path . $name));
        echo PHP_EOL . '********** EOF **********';
        echo '</pre>' . PHP_EOL;
    }
}


// IF THERE ARE NO ERROR LOG(S)
$out = ob_get_contents();
if (empty($out)) echo '<h3>Good News! No error_log found.</h3>';


// SHOW THE GIT BRANCH
$root = '.git/HEAD';
$text = @file_get_contents($root);
if ($text)
{
    $text = explode(DIRECTORY_SEPARATOR, $text);
    $text = array_slice($text, 2);
    $name = implode(DIRECTORY_SEPARATOR, $text);
    echo PHP_EOL . "On Git branch: $name" . PHP_EOL;
}
else
{
    echo PHP_EOL . "On Git branch: UNKNOWN" . PHP_EOL;
}

echo '<a href="' . $_SERVER['REQUEST_URI'] . '">Run Again</a>?' . PHP_EOL;

// SCRIPT TERMINATION WILL FLUSH THE OUTPUT BUFFER TO THE CLIENT BROWSER

Open in new window

HTH, and please post back if you still have questions, ~Ray
0
 

Author Comment

by:MOSTAGHASSI
ID: 41896906
Hi Ray,thanks for your answer i have these questions:

-The function  ini_set()  is in php.ini or we must add to our script on every page?Please send an example.

-On my linux sever i have a dir by name of log that every day create a new file regarding the warning and errors,so does this file report errors, automatically or it depend setting  the error_reporting(E_ALL) in our script or both?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41897324
PHP ini_set() is documented here:
http://php.net/manual/en/function.ini-set.php
http://php.net/manual/en/configuration.changes.php

Here is an example that reports all errors, and logs errors, and displays errors.
<?php // demo/find_error_log.php

error_reporting( E_ALL );
ini_set( 'display_errors', TRUE );
ini_set( 'log_errors',     TRUE );

Open in new window


I don't understand the dir by name of log part of the question, sorry.
0
 

Author Comment

by:MOSTAGHASSI
ID: 41897947
Sorry that my question was not clear, it is:

-On my linux sever there is  a directory that its name is logs and every day create a new file regarding the warning and errors of php(by reading these files i have corrected my codes),so does this file report errors as automatically or it depend setting  the error_reporting(E_ALL) in our script ?My mean is this that linux report php errors and warning as defulat?
At this time i have only the error_reporting(E_ALL) in my pages,
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41898024
If you're able to see the PHP errors in the daily file in logs directory it sounds to me like you're in great shape!

In my installation, when there is a PHP error, warning, or notice, the message is written into a file named error_log in the same directory with the script that triggered the error.  So I have to look in several places to find all of my error logs.  That's what the find_error_log.php script above is for.  You may not have to do that if your error logging is set up differently.

You can make a test at any time you want - just install this script and run it.  Then you can look at the error logs and see that the message is getting put into the right places for your needs.  The trigger_error() function does the same thing as any other PHP error.
<?php trigger_error('A deliberately triggered warning', E_USER_WARNING);

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question