PHP mysqli_ssl_set() Function
Definition and Usage
The mysqli_ssl_set() function establishes a secured connection using SSL with MySQL server.
Syntax
mysqli_ssl_set($con, $key, $cert, $ca, $capath, $cipher);
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
con(Mandatory) This is an object representing a connection to MySQL Server. |
| 2 |
key(Mandatory) This is a string variable representing the name of the path to the key file. |
| 3 |
cert(Mandatory) This is a string variable representing the name of the certificate file. |
| 4 |
ca(Mandatory) This is a string variable representing the name of the path to the certificate authority file. |
| 5 |
capath(Mandatory) This is a string variable representing the name of the path to the directory containing the SSL CA certificates in PEM format. |
| 6 |
cipher(Mandatory) List of ciphers that are allowed for the encryption. |
Return Values
This function returns the boolean value which is true in case of success and false incase of failure.
PHP Version
This function was first introduced in PHP Version 5 and works works in all the later versions.
Example
Following example demonstrates the usage of the mysqli_ssl_set() function (in procedural style) −
<?php
//Creating a connection
$con = new mysqli("localhost", "root","password","test");
//Securing the connection
$con->ssl_set("key.pem", "cert.pem", "cacert.pem", NULL, NULL);
//Creating the connection
$con = $con->real_connect("localhost","root","password","test");
if($con){
print("Connection Established Successfully");
}else{
print("Connection Failed ". mysqli_connect_error());
}
?>
This will produce following result −
Connection Established Successfully
Example
In object oriented style the syntax of this function is $con-> ssl_set(); Following is the example of this function in object oriented style $minus;
<?php
//Creating a connection
$con = new mysqli("localhost", "root","password","test");
//Securing the connection
$con->ssl_set("key.pem", "cert.pem", "cacert.pem", NULL, NULL);
//Creating the connection
$con = $con->real_connect("localhost","root","password","test");
if($con){
print("Connection Established Successfully");
}else{
print("Connection Failed ". mysqli_connect_error());
}
?>
This will produce following result −
Connection Established Successfully