Print de array php

Asked

Viewed 401 times

2

Nice afternoon to everyone!!! : D I am creating a php page that informs me my local ip, I am doing this through the command: shell_exec('ipconfig') so far so good because it returns the ip of my machine right, but it does not return only the ip, but also Address Ipv6, Ipv4, subnet mask, Gateway... finally returns the whole class there as when we type the command ipconfig at PC Command Prompt.

Here’s my dilemma! I want him to print on the screen just the Ip, but he’s printing a list and in the middle of this list is my ip. So the brilliant mind here thought that I could print only ip if the list was an array, assign the value returned by the command to an array variable and print the variable and the obvious happened, it printed:

  Array ( [0] => a turminha toda do resultado do comando ipconfig e blá blá blá ) 

all my list is at index 0. I wonder if there is a way to divide the array, to print only ip or if there is another way to print only ip from this list.

Thank you, if anyone knows, I’d be most grateful :)

There goes the code:

<!DOCTYPE html>
<html>
<head>
    <title>teste</title>
    <META HTTP-EQUIV="Refresh" CONTENT="60">
</head>
<body>
<?php 
$str = array(shell_exec('ipconfig'));

print_r($str);
?>
</body>
</html> 
  • 2

    Ever tried to use $_SERVER['SERVER_ADDR'] instead of ipconfig?

  • Thanks @zekk already tried but did not work it returns ::1

1 answer

3


You can use the exec for this purpose, rather than shell_exec.

$exec = exec('ipconfig', $array);

if($exec){

echo '<pre>';
var_dump($array);
echo '</pre>';

}

Will return:

array(15) {
  [0]=>
  string(0) ""
  [1]=>
  string(29) "Configuração de IP do Windows"
  [2]=>
  string(0) ""
  [3]=>
  string(0) ""
  [4]=>
  string(43) "Adaptador de Rede sem Fio Conexão Local* 2:"
  [5]=>
  string(0) ""
  [6]=>
  string(67) "   Estado da mídia. . . . . . . . . . . . . .  : mídia desconectada"
  [7]=>
  string(48) "   Sufixo DNS específico de conexão. . . . . . :"
  [8]=>
  string(0) ""
  [9]=>
  string(32) "Adaptador de Rede sem Fio Wi-Fi:"
  [10]=>
  string(0) ""
  [11]=>
  string(48) "   Sufixo DNS específico de conexão. . . . . . :"
  [12]=>
  string(60) "   Endereço IPv4. . . . . . . .  . . . . . . . : 192.AAA.AAA.AAA"
  [13]=>
  string(62) "   Máscara de Sub-rede . . . . . . . . . . . . : 255.255.255.0"
  [14]=>
  string(60) "   Gateway Padrão. . . . . . . . . . . . . . . : 192.BBB.BBB.BBB"
}

To get Ipv4 just use this:

/!\ This is a gambit!

foreach($array as $item){

    if (!!strpos($item, 'IPv4')) {
        list($descricao, $ip) = explode(': ', $item);
    }

}

echo $ip;

Upshot:

192.AAA.AAA.AAA

Another solution:

If you only want to get the IP address of the server, you can use two changes.

Get the "Ipv4 Address":

$nomeHost = gethostname();
$ip = gethostbyname($nomeHost);

echo $ip;

Upshot:

192.AAA.AAA.AAA

Get the "Server Address":

$ip = $_SERVER['SERVER_ADDR'];

echo $ip;

Upshot:

127.0.0.1
  • 2

    Thank you very much for the second option $nomeHost = gethostname();&#xA;$ip = gethostbyname($nomeHost);&#xA;&#xA;echo $ip; worked perfectly just returned my ip vlw :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.