PHP

[PHP] 에러 메세지 출력 또는 숨기기 설정 방법

PHP 에러 메세지 출력 또는 숨기기


웹사이트에 PHP 에러 메세지가 보이지 않을 경우 아래 대처 방법으로 에러 메세지를 표시할 수 있도록 전환할 수 있습니다. (반대의 경우에도 해당)

주의 : PHP 에러 메세지는 개발 중이 아닐 때는 보안상 가급적 보이지 않도록 하는 것이 좋습니다.

 

먼저 한 페이지에서만 임시적으로 에러 메세지를 보이게 하는 방법입니다.

아래 코드 PHP 코드 상단에 붙여넣어보세요.

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
?>

이 코드가 적용된 페이지에서는 PHP 오류 코드 및 메세지가 나타날 것입니다.

반대로 오류 메세지를 끄고 싶다면 아래와 같이 지정하거나, 위 코드 부분을 지워주시면 됩니다.

<?php
ini_set('display_errors', '0');
?>

 

에러 메세지를 영구적으로 보이거나 보이지 않게 하려면 서버 내의 php.ini 파일을 수정해야 합니다.

서버 접근 권한이 있을 경우 php.ini 파일을 수정해주세요. 아래는 Linux 서버 기준으로 설명합니다.

# vim /etc/php.ini

php.ini 파일이 열리면 아래 부분을 찾아주세요.

먼저 error_reporting 옵션은 어떤 종류의 에러 메세지를 표시할 것인지에 대한 설정입니다. 각 에러 표시 항목을 ‘&’ 로 구분하고 ‘~’ 기호를 앞에 붙이면 관련된 에러 메세지는 표시하지 않게 됩니다. 기본 값은 ‘E_ALL & ~E_DEPRECATED & ~E_STRICT’ 입니다.

; Error Level Constants:
; E_ALL             - All errors and warnings (includes E_STRICT as of PHP 5.4.0)
; E_ERROR           - fatal run-time errors
; E_RECOVERABLE_ERROR  - almost fatal run-time errors
; E_WARNING         - run-time warnings (non-fatal errors)
; E_PARSE           - compile-time parse errors
; E_NOTICE          - run-time notices (these are warnings which often result
;                     from a bug in your code, but it's possible that it was
;                     intentional (e.g., using an uninitialized variable and
;                     relying on the fact it is automatically initialized to an
;                     empty string)
; E_STRICT          - run-time notices, enable to have PHP suggest changes
;                     to your code which will ensure the best interoperability
;                     and forward compatibility of your code
; E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
;                     initial startup
; E_COMPILE_ERROR   - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR      - user-generated error message
; E_USER_WARNING    - user-generated warning message
; E_USER_NOTICE     - user-generated notice message
; E_DEPRECATED      - warn about code that will not work in future versions
;                     of PHP
; E_USER_DEPRECATED - user-generated deprecation warnings
;
; Common Values:
;   E_ALL (Show all errors, warnings and notices including coding standards.)
;   E_ALL & ~E_NOTICE  (Show all errors, except for notices)
;   E_ALL & ~E_NOTICE & ~E_STRICT  (Show all errors, except for notices and coding standards warnings.)
;   E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)
; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
; http://php.net/error-reporting
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

 

다음으로 에러 메세지를 켜고 끌 수 있는 옵션입니다.

; This directive controls whether or not and where PHP will output errors,
; notices and warnings too. Error output is very useful during development, but
; it could be very dangerous in production environments. Depending on the code
; which is triggering the error, sensitive information could potentially leak
; out of your application such as database usernames and passwords or worse.
; For production environments, we recommend logging errors rather than
; sending them to STDOUT.
; Possible Values:
;   Off = Do not display any errors
;   stderr = Display errors to STDERR (affects only CGI/CLI binaries!)
;   On or stdout = Display errors to STDOUT
; Default Value: On
; Development Value: On
; Production Value: Off
; http://php.net/display-errors
display_errors = Off

display_errors의 값이 On이면 에러를 표시, Off면 에러를 표시하지 않게 됩니다. 물론 이 값이 Off로 되어있어도 PHP 코드 내에 ini_set으로 에러 메세지 표시를 하게 되면 에러 메세지가 나오게 됩니다.

적용이 완료되면 파일을 저장하고, PHP-FPM 서비스를 재시작해야 합니다.

# service php-fpm restart (또는 systemctl restart php-fpm)

 

이제 에러 메세지가 정상적으로 표시될 것입니다.

JooTC

안녕하세요. 테크놀로지에 관심이 많은 블로거입니다.

Recent Posts

Zalgo 텍스트와 이를 방지하는 방법

인터넷 커뮤니티 사이트에서 게시글이나 댓글에 간혹 장난을 목적으로 작성된 특이한 글자를 볼 수 있습니다. 위…

1개월 ago

리눅스 kill, killall 명령어 – 특정 프로세스 종료하기

리눅스 명령어 - kill, killall 리눅스 kill 명령어는 특정 프로세스를 종료해주는 명령어입니다. 백그라운드에서 실행되고 있는…

1개월 ago

JavaScript typeof null이 ‘object’인 이유

JavaScript는 역사가 긴 스크립트 프로그래밍 언어입니다. 세월이 흐르면서 많은 자바스크립트 표준이 만들어졌고, 현재는 많은 문법적…

1개월 ago

Mocha Error: Resolution method is overspecified. 해결 방법

NodeJS 테스트 프레임워크인 Mocha는 비동기 테스트를 지원합니다. 간혹 특정 테스트 스크립트를 작성하고 실행하면 아래와 같이…

1개월 ago

윈도우 11 설치 시 Microsoft 계정 로그인 없이 로컬 계정 만들기

언제부턴가 윈도우 11을 처음 설치할 때 마이크로소프트(Microsoft) 계정 로그인을 강제로 요구하게 되었습니다. 물론 마이크로소프트 계정이…

6개월 ago

에어팟 프로 2 케이스 스피커 소리를 완전히 끄는 방법

애플 에어팟 프로 2 (AirPods Pro 2) 케이스에는 스피커가 내장되어 있습니다. 그런데 간혹 아무 것도…

9개월 ago