You need to use PHP
to read the .env
file and return those values to JavaScript
(if wanted).
$filename = "your_env_file.env";
$file = fopen($filename, "r") or die("500 Server Error: Can't open file.");
$content = fread($file, filesize($filename));
fclose($file);
$lines = explode("\n", $content);
$info = array();
foreach($lines as $line)
{
$data = explode("=", $line);
$info[$data[0]] = rtrim($data[1], "\r");
}
$json_encoded_info = json_encode($info); // to send to JavaScript
/*
* Now we can echo the $info array to the HTML with
* echo $info;
*
* or we can send JS the variables using
*/
echo "<script>
var info = JSON.parse(\"".$json_encoded_info."\");
//Do something with the info here
</script>";
/*
* Unfortunately, this shows all of your .env information in the string.
* A better way is with AJAX.
* See link reference below for more ways to do this.
*/
How do I pass variables and data from PHP to JavaScript?
What this code does is:
- Read all of the content of your
.env
file
- Split all of the lines
- Put all of the line information into
$info
The comments explain what you can do with that information (echo to HTML, send to Javascript).
Looking at your question, it seems like you want to replace **APP_NAME**
with its respective value. You can use Javascript
to obtain the value from PHP
(it should be Python in this case) through one of the methods in the linked Stack Overflow answer, and then replace **APP_NAME**
(or whatever other key you want) with the value received from PHP
.