我是PHP的新手并且感到困惑,我写了一个PHP脚本来记录用户发出请求时的服务器环境变量,我的代码如下所示:
<?php
$req_dump = print_r($_SERVER, TRUE);
$fp = fopen('/tmp/request.log', 'a');
fwrite($fp, $req_dump);
fclose($fp);
echo "hello world";
但是,输出如下所示:
Array
(
[HTTP_USER_AGENT] => anaconda/13.21.195
[HTTP_HOST] => 10.0.188.97
[HTTP_ACCEPT] => */*
[HTTP_X_ANACONDA_ARCHITECTURE] => x86_64
[HTTP_X_ANACONDA_SYSTEM_RELEASE] => Red Hat Enterprise Linux
[HTTP_X_RHN_PROVISIONING_MAC_0] => eth0 B4:99:BA:07:xx:xx
[HTTP_X_RHN_PROVISIONING_MAC_1] => eth1 B4:99:BA:07:xx:xx
[HTTP_X_RHN_PROVISIONING_MAC_2] => eth2 B4:99:BA:07:xx:xx
[HTTP_X_RHN_PROVISIONING_MAC_3] => eth3 B4:99:BA:07:xx:xx
[HTTP_X_RHN_PROVISIONING_MAC_4] => eth4 00:02:C9:4F:xx:xx
[HTTP_X_RHN_PROVISIONING_MAC_5] => eth5 00:02:C9:4F:xx:xx
[PATH] => /sbin:/usr/sbin:/bin:/usr/bin
[SERVER_SIGNATURE] => <address>Apache/2.2.15 (Red Hat) Server at 10.0.188.97 Port 80</address>
[SERVER_SOFTWARE] => Apache/2.2.15 (Red Hat)
[SERVER_NAME] => 10.0.188.97
[SERVER_ADDR] => 10.0.188.97
[SERVER_PORT] => 80
[REMOTE_ADDR] => 10.0.188.212
[DOCUMENT_ROOT] => /var/www/html
[SERVER_ADMIN] => root@localhost
[SCRIPT_FILENAME] => /var/www/html/ks.php
[REMOTE_PORT] => 59188
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => GET
[QUERY_STRING] =>
[REQUEST_URI] => /ks.php/images/install.img
[SCRIPT_NAME] => /ks.php
[PATH_INFO] => /images/install.img
[PATH_TRANSLATED] => /var/www/html/images/install.img
[PHP_SELF] => /ks.php/images/install.img
[REQUEST_TIME] => 1402439673
)
我是如何尝试访问该阵列的:
仅供参考,以下是我尝试访问该阵列的代码:
# ks.php
<?php
$Table = array(
"00:02:C9:10:aa:bb" => "10.0.188.91",
"00:02:C9:4F:aa:bb" => "10.0.188.92",
"00:02:C9:53:aa:bb" => "10.0.188.93",
"00:02:C9:56:aa:bb" => "10.0.188.94",
"00:02:C9:53:aa:bb" => "10.0.188.95",
"00:02:C9:4E:aa:bb" => "10.0.188.96",
"00:02:C9:5A:aa:bb" => "10.0.188.97",
);
?>
...
%post
...
printf 'DEVICE=eth4 \nIPADDR=<?php echo $Table[$_SERVER["HTTP_X_RHN_PROVISIONING_MAC_4"]]; ?>' > /etc/sysconfig/network-scripts/ifcfg-eth4
service network restart
...
%end
输出看起来不像我。假设我想获取ethernet4的MAC地址,$_SERVER["HTTP_X_RHN_PROVISIONING_MAC_4"]
对我不起作用。
任何人都可以帮我解释如何在PHP中实现这一点?
答案 0 :(得分:2)
您的$_SERVER['HTTP_X_RHN_PROVISIONING_MAC_4']
输出eth4 00:02:C9:4F:xx:xx
也有一个前缀eth4,其中$Table
有00:02:C9:4F:aa:bb
,这会导致密钥不匹配,实际上您正试图获取{{} 1}}在$Table['eth4 00:02:C9:4F:xx:xx']
数组
试试这个:
$Table
答案 1 :(得分:1)
如果您只是保存阵列,它就像一个无用的打印输出。要使其可访问,请在保存时使用json_encode
:
$fp = fopen('/tmp/request.log', 'w');
fwrite($fp, json_encode($_SERVER));
fclose($fp);
请注意我是如何删除print_r
的,因为这对于像这样的任务来说是不必要的。我还更改了fopen
以使用w
而不是a
覆盖文件,因此保存的JSON有效。
然后当您打开文件时,只需使用json_decode
,如下所示:
$server_variables_json = file_get_contents('/tmp/request.log');
$server_variables = json_decode($server_variables_json , true);
然后$server_variables
是一个你可以采取行动的实际数组:
if (array_key_exists('HTTP_X_RHN_PROVISIONING_MAC_4', $server_variables)) {
echo $server_variables['HTTP_X_RHN_PROVISIONING_MAC_4'];
}
由于我没有在if (array_key_exists(…))
值中设置HTTP_X_RHN_PROVISIONING_MAC_4
,因此$_SERVER
是我在我的计算机上本地调试此功能的原因。
我在本地进行了一次简单的检查以调试此问题 - 因为我的设置中没有HTTP_X_RHN_PROVISIONING_MAC_4
- 只是为了得到HTTP_HOST
这样:
if (array_key_exists('HTTP_HOST', $server_variables)) {
echo $server_variables['HTTP_HOST'];
}
答案 2 :(得分:0)
您需要使用引号来标识非数字索引:
echo $_SERVER['HTTP_X_RHN_PROVISIONING_MAC_4'];
您可以从文档本身获得更多basic information。
编辑:我不确定你的意思:
<?php
$array=array(
'HTTP_X_RHN_PROVISIONING_MAC_0' => 'eth0 B4:99:BA:07:xx:xx',
'HTTP_X_RHN_PROVISIONING_MAC_1' => 'eth1 B4:99:BA:07:xx:xx',
'HTTP_X_RHN_PROVISIONING_MAC_2' => 'eth2 B4:99:BA:07:xx:xx',
'HTTP_X_RHN_PROVISIONING_MAC_3' => 'eth3 B4:99:BA:07:xx:xx',
'HTTP_X_RHN_PROVISIONING_MAC_4' => 'eth4 00:02:C9:4F:xx:xx',
'HTTP_X_RHN_PROVISIONING_MAC_5' => 'eth5 00:02:C9:4F:xx:xx'
);
echo "Printing just a single element.\r\n";
echo $array['HTTP_X_RHN_PROVISIONING_MAC_4'];
echo "Printing the whole variable:\r\n";
print_r($array);
?>
输出以下内容:
Printing just a single element.
eth4 00:02:C9:4F:xx:xx
Printing the whole variable:
Array
(
[HTTP_X_RHN_PROVISIONING_MAC_0] => eth0 B4:99:BA:07:xx:xx
[HTTP_X_RHN_PROVISIONING_MAC_1] => eth1 B4:99:BA:07:xx:xx
[HTTP_X_RHN_PROVISIONING_MAC_2] => eth2 B4:99:BA:07:xx:xx
[HTTP_X_RHN_PROVISIONING_MAC_3] => eth3 B4:99:BA:07:xx:xx
[HTTP_X_RHN_PROVISIONING_MAC_4] => eth4 00:02:C9:4F:xx:xx
[HTTP_X_RHN_PROVISIONING_MAC_5] => eth5 00:02:C9:4F:xx:xx
)
答案 3 :(得分:0)
如果你想把var $ _SERVER [“HTTP_X_RHN_PROVISIONING_MAC_4”],请不要忘记引号。