2016-06-30 15 views
2

好吧,所以我試圖將我的php程序/ php文件轉換爲.exe輸出,以便它可以由任何我給予文件的人運行,無論他們在哪裏。它完全符合我的要求。但是,當我的朋友在他自己的計算機上運行相同的.exe文件時,有一個錯誤說明它無法連接到我的數據庫。我希望他能夠訪問該程序,而無需下載其他軟件和內容。我嘗試閱讀一些可能的解決方案,並發現一些建議我應該將其包含在我的數據庫連接代碼中的內容。他們還使用Server2go,使其工作:PHP Exe輸出 - 無法訪問數據庫

<?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 ....部分。這是我現有的連接代碼。我如何整合建議的解決方案?

<?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 ""; 
} 
?> 
+0

如果你想可執行文件,PHP是寫在最壞的語言 –

+0

你應該使用sqlite而不是mysql連接php以防.exe(桌面應用程序) –

回答

2

localhost您的電腦上使用時,意味着你的PC

PC意味着你的朋友PC 誰沒有一個MySQL服務器或數據庫上你的朋友使用時

localhost

您將有在代碼中使用路由器的WAN IP地址。

這假定你的ISP給你一個靜態IP地址。如果您的WAN IP地址在每次重新啓動路由器時發生變化,甚至偶爾在重新啓動時發生,那麼當您的程序在朋友PC上運行時將無法找到您的數據庫。

然後將路由器中的端口3306端口轉發到運行MYSQL的PC上的端口3306。

我強烈建議您在MYSQL中創建一個新的用戶帳戶,該用戶帳戶允許從(最安全的)只有您的好友PC的IP ADDRESS中使用,並且只能訪問此特定數據庫。然後修改代碼,使用該用戶帳戶,而不是root與根本沒有密碼,這是嚴重愚蠢的,甚至在城市最新的黑客

或者創建可從使用的MYSQL戶頭者的主要攻擊任何IP地址,但也只允許訪問這一個數據庫。

確保密碼很強大,因爲路由器上的開放端口3306很快就會被黑客發現。

-1

創建主機在Windows本地主機/ SYSTEM32 /司機/ etc/hosts中添加

127.0.0.1本地主機 :: 1本地主機