Preventing Access to the Database

There are a couple of ways to reduce the chance that a malicious user can access your database. First, if there's a problem connecting to the database, the default MySQL error code reveals the location of the database—in other words, the IP address of the host. You should suppress that information.

To prevent the standard error message from PHP, add the Error Control Operator, which is the at sign (@), to the front of the database function call. You'll experience a more closed-lipped or dubious error message in Example 15-10 before calling die to stop all processing.

Example 15-10. Suppressing the standard database error message <?php require_once('db_login.php');

$error = "Site down for maintenance, please check back.";

$db_link = @mysql_connect($db_host, $db_username, $db_password) or die($error);

@mysql_select_db($db_database, $db_link) or die($error); ?>

Without the at sign (@) before the function calls, you'll see Figure 15-12.

._.

■J Mozilla Firefow ! !

File Edit View Go Bookmarks Tools Help

Warning: mysq^connectO' Unknown MySQL Server Host 'secretserver. example, com' (1) in /hDmc/www/hlird/ciTDr handling, php on line 5 Site down for maintenance, please check back.

done ^

Figure 15-12. The database server's location is revealed in the error message

Figure 15-12. The database server's location is revealed in the error message

From a security standpoint, notice how little the error message in Figure 15-13 reveals to a potential attacker about the environment.

^^^^^lozilla Firefox

-ln|x|

File Edit View Go Bookmarks lools help

Site down for maintenance, please check back.

Done

■A

1

Figure 15-13. We no longer give out more information than is necessary

Figure 15-13. We no longer give out more information than is necessary

While this may seem like a minor point, minimizing the information available to hackers makes getting in much harder for them, providing you with more security.

Was this article helpful?

0 0

Post a comment