php - How can I Configure localhost and remote server with single config file -
i'm using different configuration files in localhost
, remote server. makes me uncomfortable because every time need change server name, password , db name.
other have single config method connect both localhost
, remote server?
this 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 check host run time.
the following code check host whenever file runs.
and depending upon host, 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(); ?>
Comments
Post a Comment