0

I am new to PHP and got confused, I wrote a PHP script to log the server environment variables when user make request, and my code looks like this:

<?php
$req_dump = print_r($_SERVER, TRUE);
$fp = fopen('/tmp/request.log', 'a');
fwrite($fp, $req_dump);
fclose($fp);
echo "hello world";

However, the output looks like below:

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
)

How I tried to access the array:

FYI, here is the code how I tried to access that array:

# 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

The output doesn't look like straight forward to me. Say I want to get the MAC address of ethernet4, and $_SERVER["HTTP_X_RHN_PROVISIONING_MAC_4"] doesn't work for me. Can anyone help me explain how to achieve that in PHP?

6
  • could you show your actual piece of code where you are attempting to get the value of $_SERVER["HTTP_X_RHN_PROVISIONING_MAC_4"] ? Commented Jun 11, 2014 at 3:41
  • @shatheesh Info added. Commented Jun 11, 2014 at 3:47
  • your $_SERVER['HTTP_X_RHN_PROVISIONING_MAC_4'] output is eth4 00:02:C9:4F:xx:xx which also has a prefix eth4 where as your $Table has 00:02:C9:4F:aa:bb which makes the keys mismatch and actually you are trying to get $Table['eth4 00:02:C9:4F:xx:xx'] which is non existent in your $Table array Commented Jun 11, 2014 at 3:52
  • @JakeGould the point is OP has added the log to track the output of $_SERVER and he is accessing the values directly from $_SERVER Commented Jun 11, 2014 at 3:54
  • 1
    @shatheesh nice observation sir. I got confused by the key name which I was assuming it is the Mac only. If you can post your answer, I will accept it. And thanks for the effort from other people too. :) Commented Jun 11, 2014 at 3:58

4 Answers 4

2

your $_SERVER['HTTP_X_RHN_PROVISIONING_MAC_4'] output is eth4 00:02:C9:4F:xx:xx which also has a prefix eth4 where as your $Table has 00:02:C9:4F:aa:bb which makes the keys mismatch and actually you are trying to get $Table['eth4 00:02:C9:4F:xx:xx'] which is non existent in your $Table array

Try this:

// We are splitting the mac address by space so that $macAddress contains '00:02:C9:4F:xx:xx' and $eth contains eth4 

list($eth,$macAddress) = explode(' ',$_SERVER['HTTP_X_RHN_PROVISIONING_MAC_4']);

// Make sure the value in $macAddress => 00:02:C9:4F:xx:xx is 00:02:C9:4F:aa:bb or change your array accordingly


$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",
);

$finalAddress = $Table[$macAddress]; 

printf 'DEVICE=eth4 \nIPADDR=<?php echo $finalAddress ; ?>' > /etc/sysconfig/network-scripts/ifcfg-eth4 '
Sign up to request clarification or add additional context in comments.

Comments

1

If you just save the array, it becomes like a useless print out. To make it accessible, use json_encode when you save it:

$fp = fopen('/tmp/request.log', 'w');
fwrite($fp, json_encode($_SERVER));
fclose($fp);

Note how I removed the print_r since it’s unnecessary for a task like this. I also changed fopen to overwrite the file using w instead of a so the saved JSON is valid.

Then when you open the file, just use json_decode like this:

$server_variables_json = file_get_contents('/tmp/request.log');
$server_variables = json_decode($server_variables_json , true);

Then $server_variables is an actual array you can act on like this:

if (array_key_exists('HTTP_X_RHN_PROVISIONING_MAC_4', $server_variables)) {
  echo $server_variables['HTTP_X_RHN_PROVISIONING_MAC_4'];
}

The if (array_key_exists(…)) is something I put in place to help me debug this locally on my machine since I don’t have HTTP_X_RHN_PROVISIONING_MAC_4 set in my $_SERVER values.

A simple check I did locally to debug this—since I don’t have HTTP_X_RHN_PROVISIONING_MAC_4 in my setup—was just to get the HTTP_HOST like this:

if (array_key_exists('HTTP_HOST', $server_variables)) {
  echo $server_variables['HTTP_HOST'];
}

1 Comment

Thanks for your suggestion of removing beautiful print, it helped me debug that there is a prefix eth4 in the value.
0

If you want to put the var $_SERVER["HTTP_X_RHN_PROVISIONING_MAC_4"], don't forgot the quotes.

Comments

0

You need to use quotes to identify a non-numeric index:

echo $_SERVER['HTTP_X_RHN_PROVISIONING_MAC_4'];

You can get more basic information from the docs themselves.

Edit: I am not sure what you mean:

<?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);
?>

Outputs the following:

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
)

2 Comments

sorry Sir... typo, can you review my question?
@JakeGould No probs, I answered the question that was posted at the time - it seems that they are quite different now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.