Php 에러 출력 - php eleo chullyeog

php error 메시지 출력 방법

php 같은 interpreter 언어들은 페이지의 호출과 동시에 해석을 시작하기 때문에 디버깅이 무척 어렵다.

특히 php는 server side 언어라서 모든 처리가 끝난 후 결과만 브라우저에 현시되기 때문에 왜 오류가 나는지, 어디서 오류가 나는지. 오류로 인해 화면이 백지로 나오는 경우도 다반사다.

그럴때는 브라우저에서 오류를 확인하기 위해 다음과 같은 코드를 php 페이지 가장 상단에 삽입하자.

<?php error_reporting(E_ALL); ini_set("display_errors", true); ?>

자세한 내용은 다음 URL에서 확인할 수 있지만 예제 제공되는 함수와 옵션들을 보겠다.

//www.php.net/manual/en/function.error-reporting.php

PHP: error_reporting - Manual

I always code with E_ALL set.After a couple of pages of I made this function to make things a little bit quicker.  Unset values passed by reference won't trigger a notice.

www.php.net

<?php // Turn off all error reporting error_reporting(0); // Report simple running errors error_reporting(E_ERROR | E_WARNING | E_PARSE); // Reporting E_NOTICE can be good too (to report uninitialized // variables or catch variable name misspellings ...) error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); // Report all errors except E_NOTICE error_reporting(E_ALL & ~E_NOTICE); // Report all PHP errors error_reporting(E_ALL); // Report all PHP errors error_reporting(-1); // Same as error_reporting(E_ALL); ini_set('error_reporting', E_ALL); ?>

위의 오류 출력 함수를 이용한 예제를 보겠다.

예제

<?php error_reporting(E_ALL); ini_set("display_errors", true); $mesg = "//muabow.tistory.com"; echo $mes; // 변수명 오류 ?>

결과

오류를 확인할 수 있다.

php 디버깅에 도움이 되었으면 한다.

끝.

PHP로 작업하다 보면 아무런 에러 메시지 없이 동작이 멈추는 경우가 있습니다.

에러 메시지 없이 문제의 원인을 찾는 건 상상할 수 없죠.

그래서 에러 메시지가 출력되도록 설정을 바꿔줘야 합니다.

PHP 설정 파일을 직접 변경해서 에러 메시지를 출력할 수도 있지만, 사실 좀 귀찮은 작업입니다.

아래 코드를 PHP 소스 상단에 추가하면 간단하게 에러 메시지를 출력할 수 있습니다.

1

2

error_reporting(E_ALL);

ini_set('display_errors',1);

   

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  RSS 방지선  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## PHP 에러메시지를 출력하는 방법중 하나는

## php.ini  파일을 수정하는 방법이 있다.

## php.ini 파일을 열어서, display_errors 를 찾아가보자.

## On 이라면, 에러를 표시

## Off 라면, 에러를 표시하지 않게된다.

## 만약 php.ini 파일을 수정했다면,

## 서버를 재시작해주는 것을 잊지말자. 그래야 적용되니깐.

## 아파치라면 서버만 재시작.

## ngnix 라면, php-fpm 재시작.

## 하지만 보통 웹서비스를 하는 입장에서는,

## 에러메시지가 득실득실 나오는 것보단, 아무것도 나오지 않는 것이 더 보기 좋으니깐

## php.ini 에서 설정을 꺼두는게 일반적이라 한다.

## 물론 보안적인 면에서도 꺼두는게 좋다.

## 하지만, 개발하면서 에러메시지는 봐야 하겠고..

## 임시적으로 error_reporting 함수로도 에러메시지 출력이 가능하단다.

error_reporting(E_ALL);

ini_set('display_errors', '1');

## 아래와 같이 에러메시지가 출력되어져 나온다.

웹사이트에 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 ; //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 ; //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)

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

Toplist

최신 우편물

태그