w3resource

PHP mysqli: ssl_set() function

mysqli_ssl_set() function / mysqli::ssl_set

The mysqli_ssl_set() function / mysqli::ssl_set is used to establish secure connections using SSL. However, this function does nothing unless OpenSSL support is enabled.

Note: This function must be called before mysqli_real_connect().

Note: MySQL Native Driver does not support SSL before PHP 5.3.3. MySQL Native Driver is enabled by default on Microsoft Windows from PHP 5.3+.

Syntax:

Object oriented style

bool mysqli::ssl_set ( string $key , string $cert , string $ca , string $capath , string $cipher )

Procedural style

bool mysqli_ssl_set ( mysqli $link , string $key , string $cert , string $ca , string $capath , string $cipher )

Parameter:

Name Description Required/Optional
link A link identifier returned by mysqli_connect() or mysqli_init() Required for procedural style only and Optional for Object oriented style
key The path name to the key file. Required
cert The path name to the certificate file. Required
ca The path name to the certificate authority file. Required
capath The pathname to a directory that contains trusted SSL CA certificates in PEM format. Required
cipher A list of allowable ciphers to use for SSL encryption. Required
Any unused SSL parameters may be given as NULL.

Usage: Procedural style

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

Parameter:

Name Description Required/Optional
connection Specifies the MySQL connection to use Required.
key Specifies the path name to the key file Required
cert Specifies the path name to the certificate file Required
ca Specifies the path name to the certificate authority file Required
capath Specifies the pathname to a directory that contains trusted SSL CA certificates in PEM format Required
cipher Specifies a list of allowable ciphers to use for SSL encryption Required

Return value:

This function always returns TRUE value. If SSL setup is incorrect mysqli_real_connect() will return an error when you attempt to connect.

Version: PHP 5, PHP 7

Example of object oriented style:

Example:

<?php
$con=mysqli_connect("localhost","user1","datasoft123","hr");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$con=mysqli_init();
if (!$con)
  {
  die("mysqli_init failed");
  }

mysqli_ssl_set($con,"key.pem","cert.pem","cacert.pem",NULL,NULL); 

if (!mysqli_real_connect($con,"localhost","user1","datasoft123","hr"))
  {
  die("Connect Error: " . mysqli_connect_error());
  }

// Some queries...

mysqli_close($con);
?>

See also

PHP Function Reference

Previous: sqlstate
Next: stat



Follow us on Facebook and Twitter for latest update.