Hàm mysqli ssl_set () trong PHP
Ví dụ - Kiểu hướng đối tượng
Tạo kết nối 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();
?>
Xem ví dụ về phong cách thủ tục ở dưới cùng.
Định nghĩa và Cách sử dụng
Hàm ssl_set () / mysqli_ssl_set () được sử dụng để thiết lập các kết nối an toàn bằng SSL. Tuy nhiên, chức năng này không có tác dụng gì trừ khi hỗ trợ OpenSSL được bật.
Lưu ý: Hàm này phải được gọi trước real_connect () .
Lưu ý: MySQL Native Driver không hỗ trợ SSL trước PHP 5.3.3. MySQL Native Driver được bật theo mặc định trên Microsoft Windows từ PHP 5.3+.
Cú pháp
Phong cách hướng đối tượng:
$mysqli -> ssl_set(key, cert, ca, capath, cipher)
Phong cách thủ tục:
mysqli_ssl_set(connection, key, cert, ca, capath, cipher)
Giá trị tham số
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 |
Chi tiết kỹ thuật
Giá trị trả lại: | Luôn luôn đúng. Nếu thiết lập SSL không chính xác, real_connect () sẽ trả về lỗi khi bạn cố gắng kết nối |
---|---|
Phiên bản PHP: | 5+ |
Ví dụ - Phong cách thủ tục
Tạo kết nối 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);
?>
❮ Tham khảo PHP MySQLi