我有一个PHP脚本,用于在mysql中运行查询并导出文件。我有在PHP文件中的登录凭据。它将被访问该页面的用户调用,并且该文件会自动下载。我已经读过,你应该在另一个文件中写入凭证并将其包含在文件中。我对PHP知之甚少,不知道如何安全地存储这个文件,以及如何正确地包含它。我有一位朋友帮我写这个文件。
php脚本如下。在php文件中隐藏mysql密码
<?php
header("Access-Control-Allow-Origin: *");
$db_user = "user"; //replace with your mysql username
$db_pass = "password"; //replace with your mysql password
$db_source = "database"; //replace with your database name
$mysqli = mysqli_connect("localhost", $db_user, $db_pass, $db_source);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
// place sql query between quotes for $sql -- do not end with ;
$sql = " SQL Statement Here ";
$res = mysqli_query($mysqli, $sql);
$rows = array();
if ($res) {
while ($r = mysqli_fetch_assoc($res)) {
$rows[] = $r;
}
} else {
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
}
$return = array(
$rows
);
array_to_csv_download($rows);
mysqli_free_result($res);
mysqli_close($mysqli);
unset ($db_user, $db_pass, $db_source);
}
function array_to_csv_download($array, $filename = "export.csv", $delimiter=",") {
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'";');
$f = fopen('php://output', 'w');
foreach ($array as $line) {
fputcsv($f, $line, $delimiter);
}
}
?>
这是*种*真。将版本保存在版本控制之外的单独文件中更好,所以偶尔将Github私有化一段时间而不会暴露它。例如,参见'。env'文件在Laravel。 – ceejayoz
@ceejayoz true!我会将其添加到我的答案。谢谢! – Maxime