php - 在服务器上安装 SSL 证书时,是否需要更改 PHP 表单提交和 MySQL intersterer 代码?
问题描述
我希望你今天一切都好。
我有一个问题,我找不到明确的答案。大多数资源让我相信这个问题没有实际意义,我不需要担心,但很遗憾,我不确定。
我有一个简单的网站,没什么太复杂的。在那个网站上有一个表单,用户可以使用简单的PHP函数提交保存到MySQL数据库的数据。数据由POST提交,经过清理,然后发送到 localhost 数据库,以使用 mysqli() 和准备好的语句插入到表中。
现在说到重点。现在一切正常,但数据可能是私有的,所以我想使用 SSL 证书(托管服务提供商也出售证书,所以安装本身是一键式的)
问题:我是否需要修改我的代码以实现 SSL 兼容性?
我以前从未这样做过,那么在我的简单案例中,有什么需要考虑和学习的吗?
我知道插入函数本身可能不需要任何修改,因为它连接到与后端位于同一主机上的数据库,但表单毕竟来自外部用户,所以我应该担心那?
还有一个更广泛的问题:对于像我这样的简单网站(只是一些常规的 HTML、CSS、PHP,如图所示,一些用于前端样式和谷歌分析和标签管理器的 vanilla JS 和 jQuery)我还应该考虑什么切换到 SSL,除了重新配置 htaccess 以强制查看者使用 HTTPS 并确保没有混合内容?
我阅读的所有内容都让我相信,一旦托管服务提供商安装了证书,我就应该能够输入,仅https://example.com
此而已,一切都应该正常工作。
这是真的吗?
对不起,如果问题很蹩脚,但我找不到明确的答案 - 我有时不明白。感谢您提前回答。
这是我的一些代码,以防万一。
Index.php 和表格:
<?php
// Nonce (not mentioned in the question, but I didn't want to omit it for clarity)
$timestamp = time();
$form_action = 'submit_form';
$nonce = create_nonce($form_action, $timestamp);
// Pushing data
if(isset($_POST['form1'])){
if ( ! empty( $_POST ) ) {
$insert = process_data($_POST);
}
}
?>
// The Form
<form action="" method="post">
// Nonce fields, ignore
<input type="hidden" name="timestamp" value="<?php echo $timestamp; ?>">
<input type="hidden" name="form_action" value="<?php echo $form_action; ?>">
<input type="hidden" name="nonce" value="<?php echo $nonce; ?>">
// Here is the rest of the form
<input type="text" name="stuff1" required>
<input type="text" name="stuff2" required>
<input type="text" name="stuff3" required>
<input type="text" name="stuff4" required>
<input type="text" name="stuff5" required>
<button type="submit" name="form1">SEND STUFF</button>
</form>
函数本身:
// NONCE - Creating the nonce
if ( ! function_exists( 'create_nonce' ) ) {
function create_nonce($action, $time) {
$str = sprintf('%s_%s_%s', $action, $time, NONCE_SALT1);
$nonce = hash('sha512', $str);
return $nonce;
}
}
// NONCE - Verification
if ( ! function_exists( 'verify_nonce' ) ) {
function verify_nonce($nonce, $action, $time) {
$check = create_nonce($action, $time);
if ( $nonce == $check ) {
return true;
}
return false;
}
}
// Now the important bit: the function processing data from the form
if ( ! function_exists( 'process_data' ) ) {
function process_data($post) {
// Check nonce
$verify = verify_nonce($post['nonce'], $post['form_action'], $post['timestamp']);
if ( false === $verify ) {
return false;
}
// Sanitization
$args = array(
'stuff1' => 'FILTER_SANITIZE_STRING',
'stuff2' => 'FILTER_SANITIZE_STRING',
'stuff3' => 'FILTER_SANITIZE_STRING',
'stuff4' => 'FILTER_SANITIZE_STRING',
'stuff5' => 'FILTER_SANITIZE_STRING',
);
$filter_post = filter_var_array($post, $args);
// Insert into the database
$mysql = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$stmt = $mysql->prepare("
INSERT INTO mydatatable (stuff1,stuff2,stuff3,stuff4,stuff5)
VALUES(?,?,?,?,?)
");
$stmt->bind_param("sssss",
$filter_post['stuff1'], $filter_post['stuff2'], $filter_post['stuff3'], $filter_post['stuff4'], $filter_post['stuff5']
);
$insert = $stmt->execute();
// Closing connections
$stmt->close();
$mysql->close();
return $insert;
}
}
解决方案
安装 SSL 证书后,您的网站必须使用带有 HTTPS 的 URL 处理数据。由于建议使用 SSL,我建议 form 方法确实是 post,因为enctype="multipart/form-data"
当页面已经使用 HTTPS 加载时,该属性不会有太大区别。
当使用 HTTPS 从浏览器(客户端)将数据从打开的页面发送到 URL(包含在操作属性中)时,就像您所做的那样,浏览器会将加密信息发送到托管服务器。反过来,PHP 将接收这些干净的数据进行操作。就服务器级别的数据安全而言,我认为审查 URL 参数化就足够了,并且可以将其包含在 htaccess 文件中,就像带有 www 和非 www 的 URL 一样,都是 HTTPS URL 必须有效,包括返回 404 代码(如果未找到)或 301(重定向)请参阅https://www.htaccessredirect.net/index.php中的更多信息
推荐阅读
- excel - 使用复选框打开另一个 excel 文件
- r - rbind对象到数据框的问题
- edi - EDIFACT 格式发送 EDI 消息状态
- node.js - 在 React App 中重定向时出现白色空白页
- apache-zookeeper - ZooKepper 无法启动AdminServer,异常退出
- c# - 如何使用 TcpListener 读取大数据
- python - 如何在Python中单击条形图的各个条形图时打印它们的标签
- jenkins - 自由样式作业与流水线作业中的 Jenkins 流水线脚本添加
- python - Reportlab 中的拉伸表列
- java - 变量只能在此位置为空结果集