-2

Summary

I have school management system which is used in different institute. if any person see error on any page than it click on report button than all the errors store in table where i can see what kind of error comes against institute I want to store all the php errors warning etc in variable but its show empty.

If variable store errors and warning than it will show like this. enter image description here

Problem

No error store in global array.

Code

ini_get(0);
set_error_handler("errorHandler");
register_shutdown_function("shutdownHandler");

function errorHandler($error_level, $error_message, $error_file, 
$error_line, $error_context)
{

  $error = " Msg:" . $error_message . " file:" . $error_file . " ln:" . 
  $error_line;

  switch ($error_level) {
  case E_ERROR:
  case E_CORE_ERROR:
  case E_COMPILE_ERROR:
  case E_PARSE:
    mylog($error, "fatal");
    break;
  case E_USER_ERROR:
  case E_RECOVERABLE_ERROR:
    mylog($error, "error");
    break;
  case E_WARNING:
  case E_CORE_WARNING:
  case E_COMPILE_WARNING:
  case E_USER_WARNING:
    mylog($error, "warn");
    break;
  case E_NOTICE:
  case E_USER_NOTICE:
    mylog($error, "info");
    break;
  case E_STRICT:
    mylog($error, "debug");
    break;
   default:
    mylog($error, "warn");

  }
}

Global Array

$custom_error show empty.

global $custom_error;
$custom_error =  array();

function mylog($error, $errlvl)
{
  // error_log($error);  
  // echo '<p><b>'.$error.'<b></p>';
  $custom_error  = $error
}
print_r($custom_error); //Show empty
if($custom_error){
  echo '<button> Report </button>';
 }
Adam
  • 109
  • 9
  • 1
    What about the error logs of the server? – B001ᛦ Mar 26 '19 at 09:55
  • I don't want use the error log because i have school management system which is used in different institute. if any person see any kind of error than it click on report button than all the errors store in table where i can see what kind of error comes in different institute. – Adam Mar 26 '19 at 09:59
  • 3
    Why would you need a user to click "Report" to tell you about a PHP error that should be getting logged? – Jonnix Mar 26 '19 at 10:02
  • I mean, feel free to aggregate your logs anywhere you like, lots of people do, but this idea of getting your users to manually report it is weird. I've seen similar kinds of things when you want users to report non-programming errors e.g. UI issues, spellings, off functionality etc, and to allow screenshots to be sent with comments, but that doesn't seem to be what you're describing. – Jonnix Mar 26 '19 at 10:07
  • 1
    1. Put them directly into the database. Escalate critical errors via e-mail. 2. I'd use a static class as storage. 3. No one will click on that button – kuh-chan Mar 26 '19 at 10:07
  • You may want to read up on [variables scope](https://www.php.net/manual/en/language.variables.scope.php) to solve the issue in the code you've shown. Also, think about using an IDE with syntax highlighting, it shows you some obvious coding/syntax-errors right away (like e.g. not closing with `;` in `$custom_error = $error`). – lovelace Mar 26 '19 at 10:11
  • 1
    A `Report` button can be useful if a page of your website/app does not the intended behavior without throwing any error/exception. For your case, as some of us said, you have already a file log, or you can store them in a database. – Kévin Bibollet Mar 26 '19 at 10:13
  • @lovelace page not found. Its my mistake not adding semicolon. Problem is why $custom_error show empty. – Adam Mar 26 '19 at 10:15
  • it shows empty because the variable inside the function is local to the function, i.e. not 'visible' to code outside the function. If you'd like it to become visible outside the scope of the function, you need to declare it global inside the function. – lovelace Mar 26 '19 at 10:17

1 Answers1

0

First, try to not use globals in your code, it is bad practice. To answer your question: to populate your array and access it from outside the function, using the code you've provided, you should declare the array $custom_error as global inside the function. That way, code that sits in the global scope has access to the array.

<?php
function mylog($error, $errlvl)
{
    global $custom_error;
    $custom_error[]  = [$error, $errlvl];
}
mylog('error1', 1);
mylog('error2', 2);

echo '<pre>';
var_dump($custom_error);
echo '</pre>';

Output:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(6) "error1"
    [1]=>
    int(1)
  }
  [1]=>
  array(2) {
    [0]=>
    string(6) "error2"
    [1]=>
    int(2)
  }
}
lovelace
  • 1,181
  • 1
  • 5
  • 10
  • You need to give a bit more feedback than _"in my case it does not work"_... do you get any error messages? What have you done to debug your code? – lovelace Mar 26 '19 at 12:47
  • 3 errors came in my php file not any single error store in $custom_error variable – Adam Mar 26 '19 at 12:58
  • I also try to save error in $_SESSION['CUSTOM_ERROR'] but it store only last one. When i create session array its show empty. No error message store in $_SESSION['CUSTOM_ERROR'] – Adam Mar 26 '19 at 13:05