Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Environment-Based Error Configuration | PHP Error Types and Reporting
Practice
Projects
Quizzes & Challenges
Quizzer
Challenges
/
PHP Error Handling

bookEnvironment-Based Error Configuration

Configuring error reporting based on your environment is a critical part of secure and maintainable PHP development. During development, you want to see all errors, warnings, and notices right away so you can fix them quickly. However, in a production environment, exposing detailed error messages to users is a security risk. Attackers could use this information to exploit vulnerabilities, and users may be confused or alarmed by technical details. Instead, production servers should log errors for later review by developers, while displaying only generic messages to users. This approach helps you catch problems early during development, while keeping your live site secure and professional.

index.php

index.php

copy
12345678910111213141516171819
<?php // Define environment: 'development' or 'production' define('ENVIRONMENT', getenv('APP_ENV') ?: 'development'); if (ENVIRONMENT === 'development') { // Show all errors in development error_reporting(E_ALL); ini_set('display_errors', '1'); ini_set('log_errors', '0'); } else { // Hide errors from users and log them in production error_reporting(E_ALL); ini_set('display_errors', '0'); ini_set('log_errors', '1'); ini_set('error_log', __DIR__ . '/php-error.log'); } // Example error for demonstration echo $undefined_variable;

In the code above, the application checks an environment variable or constant to determine if it is running in development or production. In development mode, all errors are displayed directly in the browser, making it easier to spot and fix issues as you build your application. Logging is turned off to avoid cluttering log files with frequent changes. In production mode, error messages are hidden from users by disabling display_errors, but all errors are still reported and written to a secure log file using log_errors and error_log. This ensures that sensitive information about your application or server is never shown to visitors, while giving you the information you need as a developer to investigate problems. Following these best practices helps protect your application from attackers and provides a smoother experience for your users.

question mark

Why is it important to hide detailed error messages from users in a production environment?

Select all correct answers

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookEnvironment-Based Error Configuration

Stryg for at vise menuen

Configuring error reporting based on your environment is a critical part of secure and maintainable PHP development. During development, you want to see all errors, warnings, and notices right away so you can fix them quickly. However, in a production environment, exposing detailed error messages to users is a security risk. Attackers could use this information to exploit vulnerabilities, and users may be confused or alarmed by technical details. Instead, production servers should log errors for later review by developers, while displaying only generic messages to users. This approach helps you catch problems early during development, while keeping your live site secure and professional.

index.php

index.php

copy
12345678910111213141516171819
<?php // Define environment: 'development' or 'production' define('ENVIRONMENT', getenv('APP_ENV') ?: 'development'); if (ENVIRONMENT === 'development') { // Show all errors in development error_reporting(E_ALL); ini_set('display_errors', '1'); ini_set('log_errors', '0'); } else { // Hide errors from users and log them in production error_reporting(E_ALL); ini_set('display_errors', '0'); ini_set('log_errors', '1'); ini_set('error_log', __DIR__ . '/php-error.log'); } // Example error for demonstration echo $undefined_variable;

In the code above, the application checks an environment variable or constant to determine if it is running in development or production. In development mode, all errors are displayed directly in the browser, making it easier to spot and fix issues as you build your application. Logging is turned off to avoid cluttering log files with frequent changes. In production mode, error messages are hidden from users by disabling display_errors, but all errors are still reported and written to a secure log file using log_errors and error_log. This ensures that sensitive information about your application or server is never shown to visitors, while giving you the information you need as a developer to investigate problems. Following these best practices helps protect your application from attackers and provides a smoother experience for your users.

question mark

Why is it important to hide detailed error messages from users in a production environment?

Select all correct answers

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
some-alt