Jump to content
umarzuki

macam mana nak call function php

Recommended Posts

result:

blank page kat ip.php

aku dapat error ni kat error.log apache

PHP Fatal error:  Using $this when not in object context in /var/www/html/ip.php

kod ip.php

<?php

  class networkInterfaces {
    var $osName;
    var $interfaces;

    function networkInterfaces() {
      $this->osName = strtoupper(PHP_OS);
    }

    function get_interfaces() {
      if ($this->interfaces){
        return $this->interfaces;
      }

      switch ($this->osName) {
        case 'WINNT': $ipRes = shell_exec('ipconfig');
                      $ipPattern = '/IP Address[^:]+: ' .
                                   '([\d]{1,3}\.[\d]' .
                                   '{1,3}\.[\d]{1,3}' .
                                   '\.[\d]{1,3})/';
                      break;
        case 'LINUX': $ipRes = shell_exec('ifconfig');
                      $ipPattern = '/inet addr:([\d]' .
                                   '{1,3}\.[\d]{1,3}' .
                                   '\.[\d]{1,3}\.' .
                                   '[\d]{1,3})/';
                      break;

        default     : break;
      }

      if (preg_match_all($ipPattern, $ipRes,
         $matches)) {
        $this->interfaces = $matches[1];
        return $this->interfaces;
      }
    }
  }
  
  print_r($this->interfaces);

?>

 

kemudian bila aku tukar

print_r($this->interfaces);

kepada

$inst = new networkInterfaces();
print_r($inst->get_interfaces());

ada la keluar array IP network interface (eth1, eth0 & lo)

Array ( [0] => 10.0.2.15 [1] => 192.168.56.216 [2] => 127.0.0.1 )

Edited by umarzuki

Share this post


Link to post
Share on other sites

macam mana nak keluarkan IP sahaja?

ada spesifik function/API yang boleh guna untuk list nework interface dan IP?

dengan ikut contoh dari http://php.net/manual/en/control-structures.foreach.php

aku cuba senaraikan satu persatu IP tapi dapat error "PHP Notice:  Array to string conversion in /var/www/html/ip.php"

$arr = array($inst->get_interfaces());
reset($arr);

while (list($key, $value) = each($arr)) {
    print_r($value);
}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...