简体   繁体   English

在php文件中隐藏mysql密码

[英]hide mysql password in php file

I have a PHP script that I use to run a query in mysql and export a file.我有一个 PHP 脚本,用于在 mysql 中运行查询并导出文件。 I have the login credentials within the php file.我在 php 文件中有登录凭据。 It will be invoked by a user visiting the page and the file automatically downloads.它将被访问该页面的用户调用,并且文件会自动下载。 I have read that you should write the credentials in another file and include it in the file.我已经读到您应该将凭据写入另一个文件并将其包含在该文件中。 I don't know enough about php to know where to store this file securely and how to properly include it.我对 php 知之甚少,无法安全地存储该文件的位置以及如何正确包含它。 I had a friend help me write the file.我有一个朋友帮我写文件。
The php script is as follows. php脚本如下。

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

?>

When putting PHP file on a properly set up server, the users will not be able to access the PHP source code.将 PHP 文件放在正确设置的服务器上时,用户将无法访问 PHP 源代码。 Therefore, if you trust the server, you can put the database credentials in the code without any problem.因此,如果您信任服务器,则可以毫无问题地将数据库凭据放入代码中。

However, there are a few other problems that you might encounter relating security:但是,您可能会遇到其他一些与安全相关的问题:

1. When you put a website on a server, you must ensure that this server is secure. 1.当您将网站放在服务器上时,您必须确保该服务器是安全的。 Someone from the outside should not be able to read the php source files from the server.来自外部的人应该无法从服务器读取 php 源文件。 Depending on your hosting provider, the server might be already all secured for you so you should not worry too much about that (as long as you keep to minimum the number of people having access to the server).根据您的托管服务提供商的不同,服务器可能已经为您提供了全部安全保护,因此您不必太担心(只要您将有权访问服务器的人数保持在最低限度)。

2. You're right that you need to put the credentials in an external file. 2.您是对的,您需要将凭据放在外部文件中。 However, this is not related to security on the server.但是,这与服务器上的安全性无关。 It is useful when you are working on a project with multiple other programmers and you want to keep your database credentials private.当您与多个其他程序员一起处理一个项目并且您希望将您的数据库凭据保密时,它非常有用。 Usually you want to store all the private data inside one file that you are not going to share with anyone else.通常您希望将所有私人数据存储在一个文件中,您不会与其他任何人共享。 For example, if you are using a version control system, such as git, you don't want to track this file and every programmer should have his own version of the file.例如,如果您使用版本控制系统,例如 git,您不想跟踪这个文件,每个程序员都应该有他自己的文件版本。 This reduces the risk of widespread of sensitive information.这降低了敏感信息广泛传播的风险。

Even if you put it in an include they can still get the password by echoing it out so that won't work for you.即使你把它放在一个include他们仍然可以通过回显来获取密码,所以这对你不起作用。

If you're sending an untrusted source a database password, you're doing it very wrong.如果您向不受信任的来源发送数据库密码,那您就大错特错了。 What you probably want to do is turn that script in to a REST API and there are lot of tutorials on that.您可能想要做的是将该脚本转换为 REST API,并且有很多关于此的教程。 Then, they never get to see your password.然后,他们永远不会看到您的密码。 All they would have to do is send a limited set of commands and your script would return the required data to them.他们所要做的就是发送一组有限的命令,您的脚本会将所需的数据返回给他们。 The REST API acts kind of like a black box and the end users only have access to a limited set of functions. REST API 的行为有点像一个黑匣子,最终用户只能访问有限的一组功能。

If you have full server access or access to directories behind the WWW dir, write a file with the SQL credentials and store it behind the www directory, then give -rwx to everyone and +r to the Apache/Nginx user (usually www-data).如果您有完整的服务器访问权限或访问 WWW 目录后面的目录,请使用 SQL 凭据编写一个文件并将其存储在 www 目录后,然后将 -rwx 提供给所有人,将 +r 提供给 Apache/Nginx 用户(通常是 www-data )。 Set the www-data (or the httpd server user) shell to /bin/false and secure root with a private key.将 www-data(或 httpd 服务器用户)shell 设置为 /bin/false 并使用私钥保护 root。 Then include the file with the hard path.然后包含具有硬路径的文件。 Also, never uses the root user on MySQL.另外,永远不要在 MySQL 上使用 root 用户。 Create a specific user and give it only needed permission.创建一个特定的用户并只授予它所需的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM