W3docs

A Comprehensive Guide on mysqli_ssl_set Function in PHP

When it comes to working with MySQL databases in PHP, the mysqli extension provides a variety of functions to perform various operations. One such function is

When working with MySQL databases in PHP, the mysqli extension provides a variety of functions to perform database operations. One such function is mysqli_ssl_set, which configures SSL/TLS parameters to establish a secure connection to a MySQL server.

In this guide, we will explain how mysqli_ssl_set works, its parameters, and how to implement it correctly in your PHP projects.

What is mysqli_ssl_set Function?

The mysqli_ssl_set function is a built-in PHP function that configures SSL/TLS options for a MySQL connection object. It is used to ensure secure data transmission between your PHP script and the MySQL server.

The function takes six parameters. The first argument is the MySQL connection object returned by mysqli_init(). The remaining arguments specify the paths to the SSL key, certificate, CA certificate, CA certificate path, and the cipher suite.

Here is the syntax of the mysqli_ssl_set function:

Syntax of the mysqli_ssl_set function

mysqli_ssl_set($connection, $key, $cert, $ca, $capath, $cipher);

Parameters

ParameterDescription
$connectionA connection object created with mysqli_init(). Required.
$keyPath to the client private key file (PEM). Pass null if not used.
$certPath to the client public-key certificate file (PEM). Pass null if not used.
$caPath to the certificate authority (CA) file used to verify the server. Pass null if not used.
$capathPath to a directory of trusted CA certificates in PEM format. Pass null if not used.
$cipherA list of allowable ciphers to use for SSL/TLS. Pass null to use the defaults.

Any parameter you do not need can be passed as null. A common minimal setup only supplies the CA file ($ca) so the client can verify the server's certificate, leaving the rest as null.

When would you use it?

You use mysqli_ssl_set whenever the PHP application and the MySQL server communicate over an untrusted network — for example, a web server connecting to a managed cloud database, or any connection that crosses the public internet. Without SSL/TLS, credentials and query results travel in plain text and can be read by anyone who can observe the traffic.

Features of mysqli_ssl_set Function

The mysqli_ssl_set function provides several capabilities that make it essential for securing database communications in PHP. Some of the key features include:

1. Secure Data Transmission

The primary purpose of mysqli_ssl_set is to configure an SSL/TLS encrypted channel between your PHP application and the MySQL server. This protects sensitive data from interception during transit.

2. Pre-Connection Configuration

The function allows you to set SSL parameters on a connection object before the actual connection is established. This ensures that the server enforces encryption from the moment the handshake begins.

3. Boolean Return Value

The function returns a boolean value indicating success or failure. It does not automatically generate warnings or errors; instead, you should check the return value and use mysqli_connect_error() or mysqli_error() to retrieve detailed failure information if needed.

How to Use mysqli_ssl_set Function

To use mysqli_ssl_set correctly, you must initialize the connection object, set the SSL parameters, and then establish the connection using mysqli_real_connect(). Order matters: mysqli_ssl_set() only stores the SSL options on the connection object. They are applied during the handshake performed by mysqli_real_connect(), so calling mysqli_ssl_set() after a connection is already open has no effect.

1. Initialize and Configure SSL Parameters

First, create a connection object using mysqli_init(), then apply the SSL settings before connecting:

Example of PHP mysqli_ssl_set Function

<?php

$connection = mysqli_init();
if (!$connection) {
    die('mysqli_init failed');
}

// Set SSL parameters before connecting
mysqli_ssl_set($connection, '/path/to/ssl/key', '/path/to/ssl/cert', '/path/to/ssl/ca', null, null);

// Establish the connection
if (!mysqli_real_connect($connection, 'localhost', 'username', 'password', 'mydatabase')) {
    die('Connection failed: ' . mysqli_connect_error());
}

echo 'Secure connection established successfully.';
?>

2. Verify the Encrypted Connection

After connecting, you can verify that SSL is active by querying the server status with mysqli_query(). If the connection is encrypted, Ssl_cipher returns the negotiated cipher name; if it is empty, the connection is not using SSL:

<?php
$result = $connection->query("SHOW STATUS LIKE 'Ssl_cipher'");
$row = $result->fetch_row();

if (!empty($row[1])) {
    echo 'Encrypted connection using cipher: ' . $row[1];
} else {
    echo 'Connection is NOT encrypted.';
}
?>

Common gotchas

  • Certificate verification fails. If mysqli_real_connect() fails with a certificate error, make sure the $ca file matches the authority that signed the server's certificate, and that the file is readable by the PHP process.
  • SSL is silently ignored. A successful connection does not guarantee encryption. Always confirm with the Ssl_cipher check above, and check mysqli_connect_error() when mysqli_real_connect() returns false.
  • Order of calls. Set SSL options with mysqli_ssl_set() before calling mysqli_real_connect(), not after.

Conclusion

The mysqli_ssl_set function is a vital tool for securing database communications in PHP. By configuring SSL/TLS parameters before calling mysqli_real_connect(), you ensure that all data exchanged between your application and the MySQL server travels over an encrypted channel. Always verify your server's SSL configuration and check the connection status to maintain robust security in your projects.

Practice

Practice
What is true about mysqli_ssl_set() in PHP?
What is true about mysqli_ssl_set() in PHP?
Was this page helpful?