简体   繁体   English

PHP-远程数据库连接

[英]PHP - remote database connection

I have a hybrid app that runs with information from a database related to a portable server. 我有一个混合应用程序,可以运行与便携式服务器相关的数据库中的信息。

I need to retrieve information from my web server (different to the previous one) and I don't know how to do this. 我需要从我的Web服务器上检索信息(与前一个服务器不同),但我不知道该怎么做。

I have a configuration file with the information and function to establish a database connection IN THE WEB SERVER. 我有一个配置文件,其中包含用于在WEB SERVER中建立数据库连接的信息和功能。

What I'm trying to do is to include this config.php file into an app file to establish the connection from app server with web database. 我想做的是将此config.php文件包含到应用程序文件中,以建立从应用程序服务器与Web数据库的连接。

I've tried cURL with no success. 我尝试过cURL失败。

So if i'm understanding you right you want to exchange the configuration of a database connection dynamically between two "hosts" ? 因此,如果我理解正确,您是否想在两个“主机”之间动态交换数据库连接的配置? Currently you are trying to download the config.php file via cURL ? 当前,您正在尝试通过cURL下载config.php文件吗? This is not going to work, if you access the config.php via cURL it is processed by the webserver, so you probably get an empty response because the config.php has no output. 如果您通过cURL访问config.php是由Web服务器处理的,则此操作将无效,因为config.php没有输出,因此您可能会得到一个空响应。

There are some solutions for this but I have serious security concerns. 有一些解决方案,但是我有严重的安全问题。 If you make your database configuration without any security measures available through a "webservice" this may result in very serious problems like dataleaks etc. 如果您在没有通过“ Web服务”提供任何安全措施的情况下进行数据库配置,则可能会导致非常严重的问题,例如数据泄漏等。

Please do not use the following in a production envorionment. 请不要在生产过程中使用以下内容。

The keypoint is you have to build an array / object which holds all the data you need. 关键是您必须构建一个数组/对象来保存所需的所有数据。 Then you have to serialize it and transfer it to the second host. 然后,您必须对其进行序列化并将其传输到第二个主机。

A very simple and very insecure way to do this is to write a new script which can be reached through an GET request which responds with the serialized object. 一种非常简单且非常不安全的方法是编写一个新脚本,该脚本可以通过GET请求到达,该GET请求以序列化的对象作为响应。 On the other host you simple unserialize the response and you have the object which contains your configurations. 在另一台主机上,您可以简单地反序列化响应,并且拥有包含配置的对象。

The file you access from the remote host: 您从远程主机访问的文件:

<?php
// Build your config array / object
$config["url"] = "your.database.host.com";
$config["port"] = "The port you use";
$config["other"] = "other stuff you want to transmit";
// serialize it and output it
echo serialize($config);
?>

Your remote host script: 您的远程主机脚本:

<?php
// get the response with your config
$content = file_get_contents("<your host>");
// unserialisze it
$config = unserialize($content);
// database stuff
?>

I urge you not to transmit sensible information like this without a proper security concept. 我敦促您不要在没有适当安全概念的情况下传送此类明智的信息。 Maybe you could use ssh to copy the file or build a proper REST API with authentication and validation. 也许您可以使用ssh复制文件或通过身份验证和验证构建适当的REST API。

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

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