how to create config file in php

Solutions on MaxInterview for how to create config file in php by the best coders in the world

showing results for - "how to create config file in php"
Lucas
30 May 2019
1<?php
2
3return (object) array(
4    'host' => 'localhost',
5    'username' => 'root',
6    'pass' => 'password',
7    'database' => 'db'
8);
9
10?>
11  This allows you to use the object syntax when you include the php : 
12$configs->host instead of $configs['host'].
13
14Also, if your app has configs you need on the client side (like for an Angular app), 
15you can have this config.php file contain all your configs 
16  (centralized in one file instead of one for JavaScript and one for PHP). 
17  The trick would then be to have another PHP file that would echo only the client side 
18  info (to avoid showing info you don't want to show like database connection string).
19  Call it say get_app_info.php :
20
21<?php
22
23    $configs = include('config.php');
24    echo json_encode($configs->app_info);
25
26?>
27
28The above assuming your config.php contains an app_info parameter:
29
30<?php
31
32return (object) array(
33    'host' => 'localhost',
34    'username' => 'root',
35    'pass' => 'password',
36    'database' => 'db',
37    'app_info' => array(
38        'appName'=>"App Name",
39        'appURL'=> "http://yourURL/#/"
40    )
41);
42
43?>
44So your database's info stays on the server side, 
45but your app info is accessible from your JavaScript, 
46with for example a $http.get('get_app_info.php').then(...); type of call.
Sara
27 Sep 2020
1<?php
2
3return (object) array(
4    'host' => 'localhost',
5    'username' => 'root',
6    'pass' => 'password',
7    'database' => 'db'
8);
9
10?>
Veronica
16 Oct 2018
1<?php
2$config = array(
3    "database" => "test",
4    "user"     => "testUser"
5);
6
7function writeConfig( $filename, $config ) {
8    $fh = fopen($filename, "w");
9    if (!is_resource($fh)) {
10        return false;
11    }
12    foreach ($config as $key => $value) {
13        fwrite($fh, sprintf("%s = %s\n", $key, $value));
14    }
15    fclose($fh);
16
17    return true;
18}
19
20function readConfig( $filename ) {
21    return parse_ini_file($filename, false, INI_SCANNER_NORMAL);
22}
23
24var_dump(writeConfig("test.ini", $config));
25var_dump(readConfig("test.ini"));
Debora
28 Feb 2019
1<?php
2    $servername = 'localhost';
3    $username   = 'root'; // Username
4    $password   = ''; // Password
5    $dbname     = "db_name";
6    $conn       = mysqli_connect($servername,$username,$password,"$dbname");
7    
8    if(!$conn){
9        die('Could not Connect MySql Server:' .mysql_error());
10    }
11?>
12
Aurelia
29 Nov 2020
1<?php
2 
3/*
4 * All database connection variables
5 */
6 
7define('DB_USER', "root"); // db user
8define('DB_PASSWORD', ""); // db password (mention your db password here)
9define('DB_DATABASE', "androidhive"); // database name
10define('DB_SERVER', "localhost"); // db server
11?>
12
queries leading to this page
default php config fileinclude config file in phpphp config filebest way config file in phpdo i need web config file phpcreate config file in php for databasehow to write php config filethe best way to create a config file in phpconfig inc php filephp config functionwhere to include config phphow to configur in config phpmake config file phpinclude php file config php big projects configuration filesetting up config file 3a phpwhat is configuration php tf file how to create config php filecreate config page in phpconfig php phpcreate a config file php poseidonphp whats confighow to create and get data from config files in php 24config 5b 5d in phpfile with php configphp configphp configuration fileconfig gile phpconfig inc phpget variable from config file in phpwhere is config php locatedcreate configuration php file using phpcodeconfiguration file example phpphp config file saveconfig php packagehow to create config php file for database in phphow to configure config php filephp web application configuration filestandard way to set variable in config file in phpinclude config phphow to access config files in phpphp iniphp config file without php iniphp load config filephp ini path on rrotarray in config file phphow to best config you main php filephp use config file config 3a 3a phpphp configwhere is php config filephp variable in confighow to create config phpwhere is 2fconfig phpconfig variables phpphp config files websitehow to make config file for phpwhy we include config php in our filehow to include config file in phpconfig phpconfiguration file in php websitesactivate php iniphp common config filephp parameters from config filephp use config 3finiphp config methodfile config phpsimple php database config file in phpwhere to put your own config file in php rpoject php config fielapplication 2fconfig 2fconfig phpconfig inc php exampleconfig phpcscreate config file in phpphp create configuration filecreate config file in php for sess databaseset up config phpwhere is config inc phpphp used config file php iniphp include config filephp how to change config fileget config 28 29 in phpphp define configuration filephp include configown config file phpphp config files create one config phpcreating config file and editing using php pageconfig in phpphp config filephp make a local config filephp make a config filephp configuration filesconfig method phpcreate phpcs config filecreate php config filecreate config phpconfig php codephp in configconfig for phpphp config file nameconfig 28 29 in phpphp create config file and includewhat is config inc phphow to get values from config file phpdefine 28 29 vs config file phpsettings file phpphp ini filehow to create config file in phpconfig phpinclude config php phpset php config directly on codephp config settings 24config phpphp custom configuration filecreate configuration file phpphp read config filesetup config phpcreate a config file phpconfig php exampleconfig 2fconfig php 24config in phpwhen i inlclude config file dataconenction dont work phpwhere are php config locatedhow to add config file in phpphp settingshow to do a config file phpwhat is config php filephp import config filewhere can i put the config file phpinstructions config inc phpconfig database php exampleset config php iniconfig file for php applicationhow to store configuration variable in phphow to generate config file in phphow to create some configuration files in phpphp config phpphp read config file exampleconfig server phpphp get config from config php filespecify creation of configuration on first run phpuse config 3b phpconfig function in phpphp config 28how to make a config file phpphp create config filehandle config file in phpconfig php filerecommended for configuration file in phpphp simple config fileconfig file in php oopsphp get variable from php config filephp config file changephpstorm config filephp get config valueshow to read from config phpconfig php in phpphp ini setupphp load config from separate filewhy do we need config phpphp specify config file 22config inc php 22how to work with config file in phphow to set config file for php in servermake a php config gilewhere php config file is located 2fconfig phpconfig file phpadd conf file to phpphp set config in codehow i can create config file phpwhere to config config ini phpphp write to config filehow to create php config file php configuration setting filephp config variable gethow to define configuration values in phpset up config file php on local hostcreate config value file in phpphp config 28 29set config variable phpphp save settings to filemake php config fileconfig file php exampleconfig file for phpset up a config file php on local hostphp load configconfig file in phpinclude config phpphp 24configphp stan configconfig php libraryconfig in phpphpcs configphp require config fileload php config filewhere is the php config fileconfig inc phpwhere is config phphow to create config file in php