Advertisement
Virajsinh

Get Current IP Address Using PhP

Jul 13th, 2022
1,385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.92 KB | None | 0 0
  1. $user_agent = getenv("HTTP_USER_AGENT");
  2.  
  3. if(strpos($user_agent, "Win") !== FALSE)
  4. {
  5.     $db_user = ($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['HTTP_HOST'] == getHostByName(getHostName())) ? 'root' : 'database_name';
  6.     $db_pass = ($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['HTTP_HOST'] == getHostByName(getHostName())) ? '' : '';
  7. }elseif(strpos($user_agent, "Mac") !== FALSE){
  8.     $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  9.     $res = socket_connect($sock, '8.8.8.8', 53);
  10.     // You might want error checking code here based on the value of $res
  11.     socket_getsockname($sock, $current_ip); // Use $current_ip Variable For The Mac Current IP Address
  12.     socket_shutdown($sock);
  13.     socket_close($sock);
  14.  
  15.     $db_user = ($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['HTTP_HOST'] == $current_ip) ? 'root' : 'database_name';
  16.     $db_pass = ($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['HTTP_HOST'] == $current_ip) ? '' : '';
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement