2

Okay so I am trying to convert my php program/php files into a .exe output so it can be ran by anyone I give the file to, wherever they are. It works perfectly on my end. However, when my friend ran the same .exe file on his own computer, there's an error saying how it can't connect to my database. I want him to access the program without having to download additional software and stuff. I tried reading some possible solutions and I found something that suggests I should include this in my database connection code. They also used Server2go to make it work:

<?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.");

?>    

The thing is I am not sure how to properly integrate it to the connection code I currently have. The error always refers to $conn = mysqli_connect....portion. Here's my existing connection code. How do I integrate the suggested solution?

<?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 "";
}
?>
lexus
  • 101
  • 12

3 Answers3

4

localhost when used on your PC means YOUR PC

localhost when used on your friends PC means YOUR friends PC who does not have a MYSQL Server or your database

You will have to use the WAN IP Address of your router in the code.

This assumes that your ISP has given you a static IP Address. If your WAN IP Address changes on each reboot of the router or even occasionally when rebooted, when that happens your program when run on the friends PC will not be able to find your database.

Then port forward port 3306 in your router to port 3306 on you PC running MYSQL.

I STRONGLY suggest that you create a new user account in MYSQL that is allowed to be used from either (most secure) only your friends PC's IP ADDRESS, and only has access to this specific database. Then change the code to use that user account and not root with no password at all, that is seriously foolish and a major attack vector for even the newest hacker in town

Alternatively create a MYSQL accout that can be used from any IP ADDRESS but also is only allowed access to this one database.

Make sure the password is strong as an open port 3306 on your router will be spotted very soon by hackers.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

The s2g solution works by reading an environment variable that will probably not be there on your friends computer. Therefore this method requires him to install / set up stuff. If you want to avoid this, the best way to go is really something that implements a little independent sql universe within one file, or at least within your app; sqlite is perfect for that.

dkellner
  • 8,726
  • 2
  • 49
  • 47
-1

Create hosts local host in windows/system32/drivers/etc/hosts add

127.0.0.1 localhost ::1 localhost

Tijo John
  • 686
  • 1
  • 9
  • 20