好的,所以我正在尝试将我的php程序/php文件转换成.exe输出,这样它就可以被我提供文件的任何人运行,无论他们在哪里。在我这一端它工作得很好。然而,当我的朋友在他自己的计算机上运行相同的.exe文件时,有一个错误告诉我它无法连接到我的数据库。我想让他不用下载额外的软件和东西就能访问程序。我试着阅读了一些可能的解决方案,我发现一些建议我应该在我的数据库连接代码中包括这一点。他们还使用了Server2go来使其工作:
<?php
// ExeOutput for PHP: MySQL sample using the WAMP package Server2Go
// By default, Server2go comes with a sample database. Root admin is not password-protected.
$mysqlusername = "root";
$mysqlpass = "";
// Do not modify the following lines
$mysqlport = getenv('S2G_MYSQL_PORT');
$mysqlhost = "localhost:".$mysqlport;
// We verify that our ExeOutput application was started by Server2go, otherwise, the MySQL server may not have started.
if (empty($mysqlport)) die("This application cannot be started directly. Programmers: please use the Server2go EXE file, it will start this application automatically.");
?> 问题是我不确定如何正确地将它集成到我目前拥有的连接代码中。错误总是指向$conn = mysqli_connect....portion。这是我现有的连接代码。如何集成建议的解决方案?
<?php
function db_connect()
{
$host = "localhost";
$user = "root";
$password = "";
$database = "csv_db";
$conn = mysqli_connect($host, $user, $password, $database);
if ($conn == FALSE)
{
echo "Error: Unable to connect to the database!";
return NULL;
}
return $conn;
}
function db_disconnect($conn)
{
mysqli_close($conn);
return;
}
function checkUserAccessCookie()
{
/* Check if the user has the "userAccess" cookie (set during login) */
if (isset($_COOKIE["userAccess"]))
{
return true;
}
return false;
}
function getDefaultUserFromCookie()
{
/* If the user has been here before, then a cookie named "userLogin"
* with the user's username will be available. */
if (isset($_COOKIE["userLogin"]))
{
return $_COOKIE["userLogin"];
}
/* If the cookie does not exist, then return blank instead */
return "";
}
?>发布于 2016-06-30 16:01:16
当在您的PC上使用localhost时,表示您的PC
localhost在您的朋友PC上使用时,指的是没有MYSQL Server或您的数据库的朋友PC
您必须在代码中使用路由器的WAN IP地址。
这假设您的ISP为您提供了一个静态IP地址。如果您的广域网IP地址在每次重新启动路由器时更改,甚至在重新启动时偶尔更改,当发生这种情况时,您的程序在朋友PC上运行时将无法找到您的数据库。
然后将路由器中的端口3306转发到运行MYSQL的PC上的端口3306。
我强烈建议您在MYSQL中创建一个新的用户帐户,该帐户允许从您的朋友PC的IP地址使用(最安全),并且只能访问此特定数据库。然后更改代码,使用该用户帐户,而不是没有密码的 root ,这是非常愚蠢的,即使是镇上最新的黑客也是一个主要的攻击媒介
或者,创建一个MYSQL帐户,它可以从任何IP地址使用,但也只允许访问这一个数据库。
确保密码是强的,因为黑客很快就会发现路由器上的开放端口3306。
发布于 2018-10-20 17:49:00
s2g解决方案的工作方式是读取一个可能不存在于您朋友的计算机上的环境变量。因此,这种方法需要他安装/设置东西。如果你想避免这种情况,最好的方法是在一个文件中实现一个独立的sql世界,或者至少在你的应用程序中实现一个独立的sql世界;sqlite非常适合这样做。
发布于 2016-06-30 16:06:01
在windows/system32/drivers/etc/hosts中创建主机本地主机添加
127.0.0.1 localhost ::1 localhost
https://stackoverflow.com/questions/38113465
复制相似问题