$_SERVER is an array containing information about server and execution environments. The array has values for headers, paths and script locations. The values are created by the web server.

Let’s go through the $_SERVER array. You can see the complete list of variables in the array using the following code.

List of Complete Global $_SERVER Variables

  <pre>
    <?php
      print_r($_SERVER);
    ?>
  </pre>

Detect the browser in PHP

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

Detect Browser in PHP

Get the IP Address of the Host Server in PHP

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

Get the Currently Executing Script Name in PHP

Let’s say you are executing script “script.php”

<?php
echo basename($_SERVER['REQUEST_URI'], ".php");
?>

Output

script

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

Output

script.php

    <?php
       echo basename($_SERVER['PHP_SELF']);
    ?>

Output

script.php

If you need to extract the absolute pathname of the currently executing script, try

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

The output will be something like C:/wamp64/www/yourfileName.php if you have installed wamp64 in C directory.

Get the server Name in PHP

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

This will return ‘localhost’ if you are running the code on the local host

Get the server’s identification string in PHP

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

Get the document’s root directory in PHP

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

Related Articles

Last modified: March 15, 2019

Comments

Write a Reply or Comment

Your email address will not be published.