我有一個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