one of the project required me to encrypt DB connection string (username & password).
Usually I use .htaccess to restrict user to access from web. But this pro
if the extension is available on your server you can use mcrypt Store your username and password crypted in a file outside the web root and read/write it with mcrypt
For exemple :
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key = "This is a very secret key";
$text = "myusername";
echo $text . "\n";
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
echo $crypttext . "\n";
End of course mcrypt_decrypt let you get the original string.