putenv() - php 选项信息函数
putenv()
(PHP 4, PHP 5, PHP 7)
设置环境变量的值
说明
putenv(string $setting): bool添加$setting到服务器环境变量。环境变量仅存活于当前请求期间。在请求结束时环境会恢复到初始状态。
设置特定的环境变量也有可能是一个潜在的安全漏洞。safe_mode_allowed_env_vars包含了一个以逗号分隔的前缀列表。在安全模式下,用户可以仅能修改用该指令设定的前缀名称的指令。默认情况下,用户仅能够修改以PHP_开头的环境变量(例如PHP_FOO=BAR)。注意:如果此指令是空的,PHP允许用户设定任意环境变量!
safe_mode_protected_env_vars指令包含了逗号分隔的环境变量列表,使用户最终无法通过putenv()修改。即使safe_mode_allowed_env_vars设置允许修改,这些变量也会被保护。
参数
$setting设置,例如"FOO=BAR"
返回值
成功时返回TRUE
,或者在失败时返回FALSE
。
范例
设置一个环境变量
注释
WarningThesafe_mode_allowed_env_vars和safe_mode_protected_env_vars指令仅仅在启用安全模式时有效。
参见
getenv()
获取一个环境变量的值apache_setenv()
设置 Apache 子进程环境变量
putenv/getenv, $_ENV, and phpinfo(INFO_ENVIRONMENT) are three completely distinct environment stores. doing putenv("x=y") does not affect $_ENV; but also doing $_ENV["x"]="y" likewise does not affect getenv("x"). And neither affect what is returned in phpinfo(). Assuming the USER environment variable is defined as "dave" before running the following: prints: env is: dave (doing: putenv fred) env is: dave getenv is: fred (doing: set _env barney) getenv is: fred env is: barney phpinfo() Environment Variable => Value ... USER => dave ...
White spaces are allowed in environment variable names so : Is not equivalent to
The other problem with the code from av01 at bugfix dot cc is that the behaviour is as per the comments here, not there:
It's the putenv() type of environment variables that get passed to a child process executed via exec(). If you need to delete an existing environment variable so the child process does not see it, use: putenv('FOOBAR'); That is, leave out both the "=" and a value.
Environment variables are part of the underlying operating system's way of doing things, and are used to pass information between a parent process and its child, as well as to affect the way some internal functions behave. They should not be regarded as ordinary PHP variables. A primary purpose of setting environment variables in a PHP script is so that they are available to processes invoked by that script using e.g. the system() function, and it's unlikely that they would need to be changed for other reasons. For example, if a particular system command required a special value of the environment variable LD_LIBRARY_PATH to execute successfully, then the following code might be used on a *NIX system: It will usually be appropriate to restore the old value after use; LD_LIBRARY_PATH is a particularly good example of a variable which it is important to restore immediately, as it is used by internal functions. If php.ini configuration allows, the values of environment variables are made available as PHP global variables on entry to a script, but these global variables are merely copies and do not track the actual environment variables once the script is entered. Changing $REMOTE_ADDR (or even $HTTP_ENV_VARS["REMOTE_ADDR"]) should not be expected to affect the actual environment variable; this is why putenv() is needed. Finally, do not rely on environment variables maintaining the same value from one script invocation to the next, especially if you have used putenv(). The result depends on many factors, such as CGI vs apache module, and the exact way in which the environment is manipulated before entering the script.
Great examples for the trivial case that most can figure out directly from the manual, but where is the trivially more complex example describing how to set multiple variables? I tried separating with spaces, commas, semicolons, multiple invocations of setenv, all to no avail. Please try to include trivial extensions to the examples...
Multiple invocations of putenv() work as expected: the real problem was that some of the putenv() invocations in my script contained typographical errors. I typed, e.g., putenv( "IMAGE_DATABASE=" . $_SERVER{'IMAGE_DATABASE'} ); which of course is incorrect. '{' and '}' should have been '[' and ']'. Because my first call to putenv() did not have that typo, it worked correctly, but the remaining three calls I coded with the typo merely cleared the corresponding environment variables and thus did not make it into the external script I invoked via a system() call. I suspect the fact my first invocation was typed correctly affected my efforts looking at the syntax in the following invocations. The trivial extension to the example provided is to merely call putenv() multiple times, once for each variable.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)