PHP reading config data from json file

config.json, the json config file can store the database login info or some webservice api login info or any other system configuration data.

	"database" : {
		"host" : "localhost",
		"user" : "ken",
		"password": "123456",
		"api_key": "kD3l@gs56jsDG#s9@Fgj4839",
		"api_secret": "SG353Ge245gVw3Ht3eye@309pDeg"
		"api_key": "DG3dfsg3@gs9@Fgj4ddh$drg",
		"api_secret": "DplR9ne245HfBEw#HNs0dsfh"

connect.php, the php file can read the data from the json file, convert it to php std object, and use these data as you needed.

$jsonStr = file_get_contents("config.json");
$config = json_decode($jsonStr); // if you put json_decode($jsonStr, true), it will convert the json string to associative array
echo var_dump($config);

$con = mysqli_connect(
if (mysqli_connect_errno())
  echo "Failed to connect to the database: ".$config->database->dbname. mysqli_connect_error()."\n";
	echo "connected to database ".$config->database->dbname."\n";

Search within Codexpedia

Custom Search

Search the entire web

Custom Search