PHP mysqli ssl_set() 函数
定义和用法
ssl_set()
/ mysqli_ssl_set()
函数用于使用 SSL 建立安全连接。但是,除非启用了 OpenSSL 支持,否则此函数不执行任何操作。
注意:此函数必须在 real_connect()
之前调用。
注意:在 PHP 5.3.3 之前,MySQL 原生驱动程序不支持 SSL。从 PHP 5.3+ 开始,MySQL 原生驱动程序在 Microsoft Windows 上默认启用。
实例
例子 1 - 面向对象风格
创建 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());
}
// 一些查询...
$mysqli -> close();
?>
例子 2 - 过程式风格
创建 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());
}
// 一些查询...
mysqli_close($con);
?>
语法
面向对象风格:
$mysqli -> ssl_set(key, cert, ca, capath, cipher)
过程式风格:
mysqli_ssl_set(connection, key, cert, ca, capath, cipher)
参数 | 描述 |
---|---|
connection | 必需。指定要使用的 MySQL 连接 |
key | 必需。指定密钥文件的路径名 |
cert | 必需。指定证书文件的路径名 |
ca | 必需。指定证书颁发机构文件的路径名 |
capath | 必需。指定包含受信任的 SSL CA 证书的目录的路径名,证书格式为 PEM |
cipher | 必需。指定用于 SSL 加密的允许的密码列表 |
技术细节
返回值: |
始终返回 TRUE。 如果 SSL 设置不正确,real_connect() 将在尝试连接时返回错误。 |
---|---|
PHP 版本: | 5+ |