Better Error Handling #19
Replies: 5 comments 5 replies
-
Config.phpThe script is currently set to output an error message if it can't connect to the database. This could potentially reveal sensitive information about your database or server configuration. It would be better to log the error message and show a generic error page to the user. Current approach
The problem with this approach is that it directly outputs the error message to the user. If there's a problem with the database connection, the error message could contain information about your database or server configuration, which could be helpful to an attacker. Revise Error HandlingIn this updated code:
Remember, it's important to test your error handling to ensure that it works correctly and does not reveal any sensitive information ;) |
Beta Was this translation helpful? Give feedback.
-
Database Queries assumed successfulRight now, it's assumed that all database queries will succeed. However, there could be issues with the queries, such as syntax errors or issues with the data being inserted or updated. You should check the result of each database operation and handle any errors. The PDO The PHP Data Objects (PDO) extension provides the
|
Beta Was this translation helpful? Give feedback.
-
File includesThe include statement is used to include other PHP files. If these files can't be found or can't be read for some reason, this will cause a warning. Consider include_once or require_once instead, which will cause a fatal error if the file can't be included. This is generally better because it prevents the script from continuing to execute with missing code.
Explanation of include, include_once, require and require_onceWhen you use include, if the file can't be found or read for some reason, PHP will generate a warning, but it will continue to execute the rest of the script. This can lead to unexpected behavior if the rest of the script depends on the included file. The include_once statement is similar to include, but it will check if the file has already been included, and if so, it will not include it again. This can be useful to prevent problems with function redefinitions, variable value reassignments, etc. The require statement is also similar to include, but it behaves differently when the file can't be found or read. Instead of just generating a warning like include, require will generate a fatal error and stop the script. This can be better than include if the rest of the script can't run correctly without the included file. The require_once statement is a combination of require and include_once: it will generate a fatal error if the file can't be found or read, and it will check if the file has already been included, and if so, it won't include it again. |
Beta Was this translation helpful? Give feedback.
-
User InputThe search term provided by the user is directly used in your SQL queries. While using prepared statements helps to prevent SQL injection, validating and sanitising this input to prevent other types of issues. The PHP Issue
Migration
PHP provides several functions that can be used to validate and sanitise input. One of these is the
In this example, |
Beta Was this translation helpful? Give feedback.
-
File and Directory PermissionsWhile less important for Doogle, this proved to be an ongoing issue with Valhalla2 which allowed users to upload files into the web root. In PHP, file and directory permissions can affect whether a script can read from or write to a file or directory. If the script needs to access a file or directory that it doesn't have permission to access, this will cause an error. Before trying to read from or write to a file, you can use the
Remember to replace Keep in mind that file and directory permissions can depend on the user that your web server runs as. For example, on a Unix-like system, if your web server runs as the "www-data" user, then that user needs to have read and/or write access to the file or directory. |
Beta Was this translation helpful? Give feedback.
-
A discussion around ways to improve error handling within Doogle
Beta Was this translation helpful? Give feedback.
All reactions