目录

PHP mysqli ssl_set() 函数

❮ PHP MySQLi 参考

示例 - 面向对象风格

创建 SSL 连接:

<?php
$mysqli = mysqli_init();
if (!$mysqli) {
  die("mysqli_init failed");
}

$mysqli -> ssl_set("key.pem", "cert.pem", "cacert.pem", NULL, NULL);

if (!$mysqli -> real_connect("localhost","my_user","my_password","my_db")) {
  die("Connect Error: " . mysqli_connect_error());
}

// Some queries...

$mysqli -> close();
?> 

查看底部的程序样式示例。


定义和用法

ssl_set() / mysqli_ssl_set() 函数用于使用 SSL 建立安全连接。但是,除非启用 OpenSSL 支持,否则此函数不会执行任何操作。

笔记:该函数必须先调用真实连接()

笔记:MySQL 本机驱动程序在 PHP 5.3.3 之前不支持 SSL。从 PHP 5.3+ 开始,MySQL 本机驱动程序在 Microsoft Windows 上默认启用。


语法

面向对象风格:

$mysqli -> ssl_set( key, cert, ca, capath, cipher)

程序风格:

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

参数值

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

技术细节

返回值: 永远正确。如果 SSL 设置不正确,真实连接()当您尝试连接时将返回错误
PHP 版本: 5+

示例 - 程序风格

创建 SSL 连接:

<?php
$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", "my_user", "my_password", "my_db")) {
  die("Connect Error: " . mysqli_connect_error());
}

// Some queries...

mysqli_close($con);
?>


❮ PHP MySQLi 参考