关键词搜索

源码搜索 ×
×

PHP笔记-获取客户端IP及平台及浏览器

发布2022-03-07浏览2964次

详情内容

代码如下:

  1. class CookieTool{
  2. ......
  3. ......
  4. ......
  5. protected function getIPAddress(): string{
  6. $ipaddress = "";
  7. if (isset($_SERVER['HTTP_CLIENT_IP']))
  8. $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  9. else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  10. $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  11. else if(isset($_SERVER['HTTP_X_FORWARDED']))
  12. $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  13. else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
  14. $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  15. else if(isset($_SERVER['HTTP_FORWARDED']))
  16. $ipaddress = $_SERVER['HTTP_FORWARDED'];
  17. else if(isset($_SERVER['REMOTE_ADDR']))
  18. $ipaddress = $_SERVER['REMOTE_ADDR'];
  19. else
  20. $ipaddress = 'UNKNOWN';
  21. return $ipaddress;
  22. }
  23. protected function getBrowser($agent): string{
  24. $browserAgent = "";
  25. if(strstr($agent, 'MSIE')) {
  26. $browserAgent="Internet Explorer";
  27. }
  28. else if(strstr($agent, 'Opera')) {
  29. $browserAgent="Opera";
  30. }
  31. else if(strstr($agent, 'Firefox')) {
  32. $browserAgent="Firefox";
  33. }
  34. else if(strstr($agent, 'Chrome')) {
  35. $browserAgent = "Chrome";
  36. }
  37. else if(strstr($agent, 'Safari')) {
  38. $browserAgent = "Safari";
  39. }
  40. else{
  41. $browserAgent = "unknown";
  42. }
  43. return $browserAgent;
  44. }
  45. protected function getPlatform($agent): string{
  46. $agent = strtolower($agent);
  47. $platform = "";
  48. if(strstr($agent, 'win')) {
  49. $platform="windows";
  50. }
  51. else if(strstr($agent, 'linux')) {
  52. $platform = "linux";
  53. }
  54. else{
  55. $platform = "unknown";
  56. }
  57. return $platform;
  58. }
  59. protected function getMacAddress(): string{
  60. $MAC = exec('getmac');
  61. print_r($MAC);
  62. $MAC = strtok($MAC, ' ');
  63. return $MAC;
  64. }
  65. public function printCookieArray(){
  66. global $cookieAndSessionArray;
  67. print_r($cookieAndSessionArray);
  68. }
  69. public function XXXXXX(){
  70. $ipAddress = $this->getIPAddress();
  71. $browserAgent = $this->getBrowser($_SERVER['HTTP_USER_AGENT']);
  72. $platform = $this->getPlatform($_SERVER['HTTP_USER_AGENT']);
  73. echo $ipAddress . "<br/>";
  74. echo $browserAgent . "<br/>";
  75. echo $platform . "<br/>";
  76. }
  77. }

运行截图如下:

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载