r/PHPhelp • • Dec 15 '22

mysqli_connect security

I am wondering if this function is vulnerable to timing attacks and whether or not I need to add additional code for added security or does mysqli_connect work in a way that would prevent a timing attack? I assume it does but was wondering if someone can verify if this is a safe way to authenticate someones credentials?

$connection = mysqli_connect($servername, $username, $password, $database_name);

if (mysqli_connect_errno()) {

echo "Failed to connect" . mysqli_connect_error(); }

else {

echo "connected";

mysqli_close($connection); }

?>

2 Upvotes

2 comments sorted by

View all comments

2

u/MateusAzevedo Dec 15 '22 edited Dec 15 '22

MySQL functions in PHP are just a wrapper for the mysqlnd C library. If something is implemented for timing attacks, it's likely to be on the underlying code.

In any case, you code example is unsafe for another reason: one shouldn't just print errors unconditionally.

This article shows the correct way of connecting to MySQL, including the proper error reporting mode.