PHP Exe输出-无法访问数据库
好的,我正在尝试将我的php程序/php文件转换为.exe输出,这样无论我将文件提供给谁,都可以运行它。这对我来说非常有效。然而,当我的朋友在他自己的计算机上运行相同的.exe文件时,出现了一个错误,说明它如何无法连接到我的数据库。我想让他不用下载额外的软件和东西就可以访问这个程序。我试着阅读了一些可能的解决方案,发现一些建议我应该在数据库连接代码中包含这一点。他们还使用Server2go使其工作:PHP Exe输出-无法访问数据库,php,mysql,wamp,exe,server2go,Php,Mysql,Wamp,Exe,Server2go,好的,我正在尝试将我的php程序/php文件转换为.exe输出,这样无论我将文件提供给谁,都可以运行它。这对我来说非常有效。然而,当我的朋友在他自己的计算机上运行相同的.exe文件时,出现了一个错误,说明它如何无法连接到我的数据库。我想让他不用下载额外的软件和东西就可以访问这个程序。我试着阅读了一些可能的解决方案,发现一些建议我应该在数据库连接代码中包含这一点。他们还使用Server2go使其工作: <?php // ExeOutput for PHP: MySQL sample usin
<?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\u connect…部分。这是我现有的连接代码。如何整合建议的解决方案
<?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 "";
}
?>
localhost
在您的电脑上使用时,表示您的电脑
localhost
在您的好友电脑上使用时,表示您的好友电脑没有MYSQL服务器或数据库
您必须在代码中使用路由器的WAN IP地址
这假设您的ISP已为您提供静态IP地址。如果您的WAN IP地址在每次重新启动路由器时发生更改,甚至在偶尔重新启动时发生更改,则在friends PC上运行的程序将无法找到您的数据库
然后将路由器中的端口3306转发到运行MYSQL的PC上的端口3306
我强烈建议您在MYSQL中创建一个新的用户帐户,该帐户只允许从您朋友电脑的IP地址(最安全)使用,并且只能访问此特定数据库。然后更改代码使用该用户帐户<强>而不使用代码>根/代码>,完全没有密码,这是非常愚蠢的,甚至是最新的黑客在城镇<强> > /p>的主要攻击向量。
或者创建一个MYSQL帐户,可以从任何IP地址使用,但也只允许访问这一个数据库
确保密码很强,因为黑客很快就会发现路由器上的3306开放端口
在windows/system32/drivers/etc/hosts add中创建主机本地主机
127.0.0.1本地主机
::1 localhosts2g解决方案的工作原理是读取一个可能不在您朋友的计算机上的环境变量。因此,这种方法需要他安装/设置东西。如果你想避免这种情况,最好的办法是在一个文件中实现一个独立的sql世界,或者至少在你的应用程序中实现;sqlite非常适合这样做。如果你想要可执行文件,php是最糟糕的编写语言。如果是.exe(桌面应用程序),你应该将php与sqlite而不是mysql连接起来