javascript - How to hold text file's data into variable and read/display it on php file? -


i make website on php , want read information text file. reason if, want update connection string, data base name, hyperlinks,echo messages etc. don't want update php code replace text in text file , relative changes reflects in php file.

for example,

1. php file :

    <?php     $servername = "server_name";   //comes db_config.txt file     $username = "user_name";       //comes db_config.txt file     $password = "password";        //comes db_config.txt file      // create connection     $conn = new mysqli($servername, $username, $password);      // check connection     if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error);     }     echo "conn_succ_msg";    //comes db_config.txt file     ?>   

2. text file : (db_config.txt)

    server_name = localhost     user_name = abc     password = 123     conn_succ_msg = connected 

i study "php 5 file open/read/close" http://www.w3schools.com/php/php_file_open.asp not getting how implement if possible in xml file, etc fine. or can possible java scripts, jquery etc. idea how ?

for simple task use file() function:

$config = file("db_config.txt"); 

so, array cell each line. can parse:

foreach ($config $parameter) {      $ar = explode(" = ", $parameter);      $$ar[0] = $ar[1]; } 

note assignment of "$$ar[0]" has 2 $$: create new variable name content of $ar[0]. way have parameters variables same name ($server_name, $user_name, etc).

then, final script:

$config = file("db_config.txt");  foreach ($config $parameter) {      $ar = explode(" = ", $parameter);      $$ar[0] = $ar[1]; }  // create connection $conn = new mysqli($server_name, $user_name, $password);  // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); } echo $conn_succ_msg; 

thus, not best way it. should create .php file parameters defined php variables , include it, this:

example db_config.php:

$server_name = 'localhost'; $user_name = 'abc'; $password = '123'; $conn_succ_msg = 'connected successfully'; 

then add @ head of php script:

include('db_config.php');  // create connection $conn = new mysqli($server_name, $user_name, $password);  // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); } echo $conn_succ_msg; 

this safer, because not risk can access file directly url, if set wrong file permissions.


Comments