gpt4 book ai didi

php - PHP-docker容器中的环境变量

转载 作者:可可西里 更新时间:2023-10-31 23:28:55 26 4
gpt4 key购买 nike

我想在我的 docker 容器中显示一个环境变量。PHP 脚本如下所示:

<html>
<head>
<title>Show Use of environment variables</title>
</head>
<body>
<?php
print "env is: ".$_ENV["USER"]."\n";
?>
</body>
</html>

我使用 OpenShift 启动容器。 PHP - 容器显示:

env is: 

现在我更改容器的 dc 配置:

oc env dc/envar USER=Pieter
deploymentconfig "envar" updated

当我访问容器时。 USER 的环境变量是 Pieter

docker exec -it 44a0f446ae36 bash
bash-4.2$ echo $USER
Pieter

但我的脚本仍然显示:“env is:”它没有填写变量。

最佳答案

改变

print "env is: ".$_ENV["USER"]."\n";

print "env is: ".getenv("USER")."\n";

.

test.php

<html>
<head>
<title>Show Use of environment variables</title>
</head>
<body>
<?php
print "env via \$_ENV is: ".$_ENV["USER"]."\n";
print "env via getenv is: ".getenv("USER")."\n";
?>
</body>
</html>
# export USER=Sascha
# echo $USER
Sascha

php 测试.php

    <html>
<head>
<title>Show Use of environment variables</title>
</head>
<body>
PHP Notice: Array to string conversion in /test.php on line 7
PHP Notice: Undefined index: USER in /test.php on line 7
env via $_ENV is:
env via getenv is: Sascha
</body>
</html>

关于php - PHP-docker容器中的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35479304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com