如何使用mysqli与SSL连接

zdwk9cvp  于 2023-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(129)

我正在尝试与我的数据库建立安全连接
我写了以下代码:

<?php

// form filled?
if (isset($_POST['submit'])) {
    $user = 'gebruiker';
    $pass = 'gebruiker';
    $db = new mysqli('localhost', $user, $pass, 'forum');
    if (mysqli_connect_errno()) {
        echo 'database doesnt work';
        file_put_contents('MySQLiErrors.txt', date('[Y-m-d H:i:s]') .          mysqli_connect_error() . "\r\n", FILE_APPEND);
        exit();
    } else {
        $username = $_POST['username'];
        $userspassword = $_POST['password'];
        $salt = strrev($userspassword . substr(0, 4));
        $password = hash('sha512', $userspassword . $salt);
        $statement = $db->prepare("SELECT id,username FROM user WHERE username = ? AND password = ?");
        $statement->bind_param("ss", $username, $password);
        $statement->execute();
        $result = $statement->get_result();
        $statement->close();
        $count = $result->num_rows;
        if ($count > 0) {
            session_start();
            $_SESSION["username"] = $username;
            header("Location: forum.php");
        } else {
            $_SESSION['Error'] = "Invalid username or password";
        }
    }
    $db->close();
}

我也在www.example.com上读到了一些关于SSL连接的内容php.net,但我不知道如何在这种情况下实现它。
http://php.net/manual/en/mysqli.ssl-set.php
我的代码运行在fedora 21上,它工作得很好,但接下来我想要的是使用SSL的安全连接。

bnl4lu3b

bnl4lu3b1#

您不需要客户端证书和私钥,并且在大多数情况下,您不希望MySQL服务器验证客户端证书。
但是,客户端必须使用CA证书验证服务器证书,以防止MITM。

<?php
$mysqli = mysqli_init();
$mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$mysqli->ssl_set(NULL, NULL, "/etc/ssl/certs/ca-bundle.crt", NULL, NULL);
$mysqli->real_connect('hostname', 'user', 'password', 'database');
$mysqli->close();
?>
rpppsulh

rpppsulh2#

<?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);
?>

connection -必需。指定要使用的MySQL连接
key -必需。指定密钥文件cert的路径名-必需。指定证书文件ca的路径名-必需。指定证书颁发机构文件capath的路径名-必需。指定包含PEM格式密码的受信任SSL CA证书的目录的路径名-必需。指定允许用于SSL加密的密码列表
SOURCE HERE
对于mysql_真实的_connect使用

<?php
ini_set ('error_reporting', E_ALL);
ini_set ('display_errors', '1');
error_reporting (E_ALL|E_STRICT);

$db = mysqli_init();
mysqli_options ($db, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);

$db->ssl_set('/etc/mysql/ssl/client-key.pem', '/etc/mysql/ssl/client-cert.pem', '/etc/mysql/ssl/ca-cert.pem', NULL, NULL);
$link = mysqli_real_connect ($db, 'ip', 'user', 'pass', 'db', 3306, NULL, MYSQLI_CLIENT_SSL);
if (!$link)
{
    die ('Connect error (' . mysqli_connect_errno() . '): ' . mysqli_connect_error() . "\n");
} else {
    $res = $db->query('SHOW TABLES;');
    print_r ($res);
    $db->close();
}
?>
jexiocij

jexiocij3#

一点简单的方法方便actucaly mysqli

<?php  namespace mysqlConnect;

class mymysqli extends \mysqli {

        
        public function __construct($db_host, $db_user, $db_pass, $db_name, $persistent = true, $ssl = false, $certpublic = "") {
            //$this->close();
            parent::init();
            //parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 1);
            if($ssl) {
                parent::options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, false);
                parent::ssl_set(NULL, NULL, $certpublic, NULL, NULL);
            }
            @parent::real_connect(($this->persistent ? 'p:' : '') . $this->db_host, $this->db_user, $this->db_pass, $this->db_name);

        }
}

相关问题