How can I Configure localhost and remote server with single config file

Haris M

I'm using different configuration files in my localhost and remote server. It makes me very uncomfortable because every time I need to change the server name, password and db name.
Is that any other to have single config method to connect both localhost and remote server?

This is my config 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();
?> 
Pupil

You need to check the host run time.

The following code will check the host whenever the file runs.

And depending upon 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();
?> 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I configure my OpenVPN client config file to route traffic only to the remote LAN?

How can I configure a second SSH key + user for a single server?

how to configure axon server client with remote server host? (without localhost)

how to configure hibernate config file for sql server

how can i deploy the jar file in remote server

how can I run python file in remote server using Paramiko?

How can I jq a line from a file from a remote server?

How do I configure Postgres 9.5 server so that I can access it from a remote machine?

How can i configure apache server to Proxy to http://localhost:3000/ and exclude some URL?

In Rails 5.2 how can I configure my system tests to use a selenium remote server?

How can I configure the max connection limit for Remote Desktop Connections on a Windows 2003 Server?

How can I configure NLog in .NET Core with appsettings.json instead of an nlog.config file?

How can i configure local by flywheel v5.0.7 site.config.hbs file for roots bedrock

How to configure Postfix so that I can authenticate via SMTP to localhost

How do I configure a transparent proxy where the proxy server is remote?

How can I send a zip file from local to remote and save it to remote directory when connecting directly to remote server is not an option?

How to configure Laravel on remote server

How can i provide a .config function to configure my library

How can i read a remote file in nodejs?

How can I copy file from local server to remote with creating directories which absent via SSH?

How can I fetch a list of file names from a Remote Server via FTP?

How can I debug a single Javascript file?

Should I change the localhost to be my public IP DNS address inside Nginx server config file?

Can Apache NiFi configure logback.xml file to send logs to remote server over UDP?

How to setup a remote ssh server for tunneling to localhost?

How do I configure apache-traffic-server to forward an http request to an https remote server?

How can I connect remotely to a WebSphere Server running on localhost?

How can I use TLS on a local apache development server (on localhost)

How can I access server localhost ports via OpenVPN connection?

TOP Ranking

HotTag

Archive