当前位置:首页 > 操作系统 > MacOs

php获取客户端网卡mac物理地址

php编程中,获取到用户mac地址,就可以实现与客户电脑的绑定、防止垃圾注册等。
一个非常简单的类,使用时只要实例化后直接打印它的macAddr属性即可。

代码:

  1. class Getmac{
  2. var $result = array(); // 返回带有MAC地址的字串数组
  3. var $macAddr;
  4. /*构造*/
  5. function __construct($osType){
  6. switch ( strtolower($osType) ){
  7. case "unix": break;
  8. case "solaris": break;
  9. case "aix": break;
  10. case "linux": {
  11. $this->for_linux_os();
  12. }break;
  13. default: {
  14. $this->for_windows_os();
  15. }break;
  16. }
  17. $temp_array = array();
  18. foreach($this->result as $value){
  19. if(preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i",$value,
  20. $temp_array ) ){
  21. $this->macAddr = $temp_array[0];
  22. break;
  23. }
  24. }
  25. unset($temp_array);
  26. return $this->macAddr;
  27. }
  28. /*linux系统中获取方法*/
  29. function for_linux_os(){
  30. @exec("ifconfig -a", $this->result);
  31. return $this->result;
  32. }
  33. /*win系统中的获取方法*/
  34. function for_windows_os(){
  35. @exec("ipconfig /all", $this->result);
  36. if ( $this->result ) {
  37. return $this->result;
  38. } else {
  39. $ipconfig = $_SERVER["WINDIR"]."system32ipconfig.exe";
  40. if(is_file($ipconfig)) {
  41. @exec($ipconfig." /all", $this->result);
  42. } else {
  43. @exec($_SERVER["WINDIR"]."systemipconfig.exe /all", $this->result);
  44. return $this->result;
  45. }
  46. }
  47. }
  48. }
  49. /*1.实现化类 2.直接访问它的macAddr属性*/
  50. $getMac = new Getmac(PHP_OS);
  51. echo $getMac->macAddr;
  52. ?>


客户端, 网卡
【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐