对于一个项目,我正在尝试制作一个应用程序,其中用户可以输入他们的值,但是当我在本地主机上测试它时,我不断收到此错误:警告:mysqli_connect():(HY000/3159):使用不安全的连接--require_secure_transport=ON 时禁止传输。这是我的 php 代码:
$bedrijfsnaam = $_POST['bedrijfsnaam']; $dag = $_POST['dag']; $caption = $_POST['caption']; var_dump($bedrijfsnaam, $dag, $caption); $dbh = mysqli_connect(hostname: $host, username: $username, password: $password, database: $dbname); if (mysqli_connect_errno()) { die("Connection error: " . mysqli_connect_errno()); } $sql = "INSERT INTO file_upload (bedrijfsnaam, dag, caption) VALUES (?, ?, ?)"; $stmt = mysqli_stmt_init($conn);
是否有一种安全的方法来修复此错误,因为该项目必须发布,并且据我了解,关闭 secure_transport 是不安全的。
我尝试使用另一个外部数据库,这导致另一个错误,连接被主动拒绝。该代码确实有效,因为我尝试在本地主机上运行它并且它确实有效
P粉9464374742023-12-21 17:42:37
您需要调用 [mysqli_real_connect][1] 并传入 $mysqli 实例作为参数
$mysqli = mysqli_init(); if (!$mysqli) { die('mysqli_init failed'); } $mysqli->ssl_set( '/path/to/client-key.pem', '/path/to/client-cert.pem', '/path/to/ca-cert.pem', NULL, NULL ); //any paths here will work if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) { die('Setting MYSQLI_INIT_COMMAND failed'); } if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) { die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed'); }
然后传递给mysqli_real_connect
$dbh = mysqli_real_connect(mysql:$mysqli, hostname: $host, username: $username, password: $password, database: $dbname); [1]: https://www.w3schools.com/Php/func_mysqli_real_connect.asp