Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Some question about 'error_reporting(E_ALL)' in php

Posted on 2016-11-21
5
Medium Priority
?
124 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

715 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