PHP


Tips and Tricks of PHP
By Junaid Shabbir
22-Jun-08
Views: 13480

Tips and Tricks of PHP
 
Suppressing Error Messages in PHP (Page 2 of 7)
In some cases the standard error messages from PHP reveal secure information about the environment. That information could help a malicious user in getting control of your server. Have a look at the following code, which builds a connection with mysql server.
<?
	$server="ns1.myhost.com";
	$username="scott";
	$password="123";

	mysql_connect($server,$username,$password);
?>
 
If the above code is unable to access the specified server, following error message will be sent to the user, revealing the host name



PHP allows to prevent standard error messages by using the '@' symbol in front of functions.

<?
	$server="66.123.45.23";
	$username="scott";
	$password="123";

	$error_message="Site is temporarily down, please check back soon!";
	$link=@mysql_connect($server,$username,$password);	// using @ here suppresses the standard error message

	if(!$link)
		die($error_message);
?>

If the code is unsuccessful, a more decent message is sent to the user


You as a programmer should take care of many other functions of PHP ( especially the functions which request for some resource like file system functions), which can reveal environment related information to malicious users.