How to configure local and remote server using a single configuration file

I use different configuration files on my localhostand remote server. I am very uncomfortable, because every time I need to change the server name, password and db name.
Does this mean that any other can have a single configuration method for connecting both the localhostremote server?

This is my configuration file:

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
    }

    $conn->close();
?> 
+4
source share
3 answers

You need to check the host startup time.

The following code checks the host each time the file is run.

And depending on the host, it will use different credentials.

<?php
$host = $_SERVER['HTTP_HOST'];
if ($host == 'localhost') {
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
}
else {
    $servername = "REMOTE_HOST";
    $username = "REMOTE_USERNAME";
    $password = "REMOTE_PASSWORD";
    $dbname = "REMOTE_DB";
}

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?> 
+1
source

$_SERVER array HTTP_HOST, .

strpos . HTTP_HOST , localhost:8080. , localhost .

<?php
    if (strpos($_SERVER['HTTP_HOST'], 'localhost') !== false)
    { 
        $servername = "localhost";
        $username = "username";
        $password = "password";
        $dbname = "myDB";
    } else {
        $servername = "10.11.12.13";
        $username = "username";
        $password = "P@$$w0rd";
        $dbname = "myDBName";            
    }

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $conn->close();
?> 
+1

Set up a virtual host environment

    SetEnv environment {production/development}

So your virtual host might look like this:

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/path/httpd"
    ServerName mysite.local
    ServerAlias mysite.local
    SetEnv environment production
</VirtualHost>

php code:

<?php
if (getenv('environment') == 'production') {
    $servername = "localhost";
    $username = "production-username";
    $password = "production-password";
    $dbname = "myDB";
} else {
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
}

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}
$conn->close();

?> 
+1
source

All Articles