mysqli ssl_set() Method
The mysqli::ssl_set() / mysqli_ssl_set() function is used for establishing secure connections using SSL. It must be called before mysqli_real_connect(). This function does nothing unless OpenSSL support is enabled.
Syntax
//Object-oriented style public mysqli::ssl_set(key, certificate, ca_certificate, ca_path, cipher_algos) //Procedural style mysqli_ssl_set(mysql, key, certificate, ca_certificate, ca_path, cipher_algos)
Parameters
mysql |
Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init(). |
key |
Required. Specify the path name to the key file. |
certificate |
Required. Specify the path name to the certificate file. |
ca_certificate |
Required. Specify the path name to the certificate authority file. |
ca_path |
Required. Specify the pathname to a directory that contains trusted SSL CA certificates in PEM format. |
cipher_algos |
Required. Specify a list of allowable ciphers to use for SSL encryption. |
Return Value
Always returns true value. If SSL setup is incorrect mysqli_real_connect() will return an error when trying to connect.
Example: Object-oriented style
The example below shows the usage of mysqli::ssl_set() method.
<?php $mysqli = mysqli_init(); if (!$mysqli) { die('mysqli_init failed'); } $mysqli -> ssl_set("key.pem", "cert.pem", "cacert.pem", NULL, NULL); //establishing connection to the database if (!$mysqli->real_connect("localhost", "user", "password", "database")) { die('Connect Error: '. mysqli_connect_error()); } echo 'Success... ' . $mysqli->host_info . "\n"; //closing the connection $mysqli->close(); ?>
The output of the above code will be similar to:
Success... MySQL host info: localhost via TCP/IP
Example: Procedural style
The example below shows the usage of mysqli_ssl_set() function.
<?php $mysqli = mysqli_init(); if (!$mysqli) { die('mysqli_init failed'); } mysqli_ssl_set($mysqli, "key.pem", "cert.pem", "cacert.pem", NULL, NULL); //establishing connection to the database if (!mysqli_real_connect($mysqli, "localhost", "user", "password", "database")) { die('Connect Error: '. mysqli_connect_error()); } echo 'Success... ' . mysqli_get_host_info($mysqli) . "\n"; //closing the connection mysqli_close($mysqli); ?>
The output of the above code will be similar to:
Success... MySQL host info: localhost via TCP/IP
❮ MySQLi Functions Reference