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



PHP: Tips of the Day

var_export(): var_export() dumps a PHP parseable representation of the item.

You can pass true as the second parameter to return the contents into a variable.

Example:

<?php
$myarray = [ "PHP", "Tips" ];
$mystring = "PHP Tips";
$myint = 28;

var_export($myarray);
var_export($mystring);
var_export($myint);
?>

Output:

array (
  0 => 'PHP',
  1 => 'Tips',
)'PHP Tips'28

To put the content into a variable, you can do this:

$array_export = var_export($myarray, true);
$string_export = var_export($mystring, true);
$int_export = var_export($myint, 1); // any `Truthy` value

After that, you can output it like this:

printf('$myarray = %s; %s', $array_export, PHP_EOL);
printf('$mystring = %s; %s', $string_export, PHP_EOL);
printf('$myint = %s; %s', $int_export, PHP_EOL);

Example:

<?php
$myarray = [ "PHP", "Tips" ];
$mystring = "PHP Tips";
$myint = 28;
$array_export = var_export($myarray, true);
$string_export = var_export($mystring, true);
$int_export = var_export($myint, 1);
printf('$myarray = %s; %s', $array_export, PHP_EOL);
printf('$mystring = %s; %s', $string_export, PHP_EOL);
printf('$myint = %s; %s', $int_export, PHP_EOL);
?>

This will produce the following output:

Output:

$myarray = array (
  0 => 'PHP',
  1 => 'Tips',
);
$mystring = 'PHP Tips';
$myint = 28;