English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_ssl_set() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_ssl_set() function uses SSL to establish a secure connection to the database.

Definition and Usage

mysqli_ssl_set()The function uses SSL to establish a secure connection with the MySQL server.

Note: This function is effective only when OpenSSL support is enabled.
Note: This function must be called before mysqli_real_connect().
Note: In PHP 5.3.3 In earlier versions, MySQL Native Driver does not support SSL. Since PHP 5.3+ Starting from, the MySQL Native Driver is enabled by default on Microsoft Windows.

Syntax

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

Parameter

Serial NumberParameters and Description
1

con(Required)

This is an object representing the connection to the MySQL Server.

2

key(Required)

This is a string variable representing the path name of the key file.

3

cert(Required)

This is a string variable representing the name of the certificate file.

4

ca(Required)

This is a string variable representing the path name of the certificate authority file.

5

capath(Required)

This is a string variable representing the path name of the directory containing the PEM format SSL CA certificate.

6

cipher(Required)

List of SSL encryption algorithms that can be used.

Return Value

This function returns a boolean value, and returnstrue; if failed, it returnsfalse.

PHP Version

This function was originally introduced in PHP version5introduced and can be used in all higher versions.

Online Example

The following examples demonstratemysqli_ssl_set()Function Usage (Procedural Style)-

<?php
   //Establish connection
   $con = new mysqli("localhost", "root","password","test");
   //Secure connection
   $con-ssl_set("key.pem", NULL, "cacert.pem", NULL, NULL);
   //Establish connection
   $con = $con-real_connect("localhost","root","password","test");
   if($con){
      print("Connection established successfully");
   }
      print("Connection failed	" . mysqli_connect_error());
   }
?>

Output result

Connection established successfully

Online Example

In object-oriented style, the syntax of this function is$con-ssl_set();.The following is an example of this function in object-oriented style;

<?php
   //Establish connection
   $con = new mysqli("localhost", "root","password","test");
   //Secure connection
   $con-ssl_set("key.pem", NULL, "cacert.pem", NULL, NULL);
   //Establish connection
   $con = $con-real_connect("localhost","root","password","test");
   if($con){
      print("Connection established successfully");
   }
      print("Connection failed	" . mysqli_connect_error());
   }
?>

Output result

Connection established successfully

PHP MySQLi Reference Manual