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