- PHP

How to get the current domain name in PHP

Use $_SERVER[‘SERVER_NAME’].

<?php 
  echo $_SERVER['SERVER_NAME'];
?>

If you need more details, like these about the specific path, use also $_SERVER[‘QUERY_STRING’].

<?php 
  echo $_SERVER['REQUEST_URI'];
?>

Example:

<?php 
  echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
?>

Output: localhost/snippetsdb/