EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP $_SERVER

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP $_SERVER

PHP $_SERVER

Introduction to PHP $_SERVER

PHP comes out a function of $_Server which is a super global variable in PHP having information about paths, headers. Basically these super variables are always available in the scope of any PHP code.

$_Server consists of an array that have the information created by the web server such as headers,path, scriptlocations. It is directly related to the runtime environment of the current PHP script. All the information regarding the server can be get using this function. This is available from PHP version 4 and higher.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

The Syntax for the getting details using $ server function is :-

$_SERVER['VARIABLE_NAME']

Variable name contains the name of the information we need to get from the $server method.

For Ex :- Suppose we want to get the Server Name .

echo $_SERVER['SERVER_NAME'];

So from this we can get the details whatever required for that PHP environment.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,678 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Screenshot:

PHP $_SERVER-1

Working of $_ SERVER Function

  • The $_Server Function is a super global variable in PHP. A super global variables are built in variables whose scope is defined over the entire PHP script.
  • $_ Server gets the information of the headers, path and the location of the script from the web server in the form of Array.
  • $_Serveris basically set by the web server where the PHP code is deployed. There is no guarantee for the thing that server will provide with the all the details required sometimes there may be a case where the information are not listed in and sometimes some excess information can also come in place. It is directly related to the scripts at the run time.

Example of PHP $_SERVER

Let us see some functioning of $ Server with some Example:

1. $_Server[‘PHP_SELF’]

This defines the file name of the script where the current PHP code is being run. This gives the path of the PHP .

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['PHP_SELF'];
?>
</body>
</html>

Output:

PHP $_SERVER-1.1

2. $_SERVER[‘argv’]

This stores the argument passed in an array and the return type for this is any array .

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['argv'];
?>
</body>
</html>

Output:

PHP $_SERVER-1.2

3. $_SERVER[‘argc’]

This gives the information about the number of command line parameter.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['argc'];
?>
</body>
</html>

Output:

PHP $_SERVER-1.3

4. $_Server[‘GATEWAY_INTERFACE’]

This gives the information about the common gateway interface if any . If there is not gateway used the result obtained is null.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['GATEWAY_INTERFACE'];
?>
</body>
</html>

Output:

Blank as no Gateway interface is used.

5. $_SERVER[‘SERVER_ADDR’]

It returns the IP address of the host server where the PHP script is being run. If ran on local this gives the local IP address of the machine.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['SERVER_ADDR'];
?>
</body>
</html>

Output:

The IP address of the host machine.

6. $_SERVER[‘SERVER_SOFTWARE’]

This gives the detail of the software used in the server. this may be Apache, or any hosted web servers.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['SERVER_SOFTWARE'];
?>
</body>
</html>

Output:

Apache (cent Os)

7. $_SERVER[‘SERVER_PROTOCOL’]

This gives the detail of the protocol via the request is made . HTTP , HTTPS are the most probable information for this function.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['SERVER_PROTOCOL'];
?>
</body>
</html>

Output:

HTTP/1.1

8. $_SERVER[‘REQUEST_METHOD’]

This gives the detail of the request method used to access a particular page .

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['REQUEST_METHOD'];
?>
</body>
</html>

Output:

Get /post /put / head

9. $_SERVER[‘REQUEST_TIME’]

This records the timestamp for the start of any request.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['REQUEST_TIME'];
?>
</body>
</html>

Output:

PHP $_SERVER-1.4

10. $_SERVER[‘QUERY_STRING’]

If any query is used for accessing the page this method gives the information about that.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['QUERY_STRING'];
?>
</body>
</html>

Output:

If query used the Query or else none.

11. $_SERVER[‘HTTP_ACCEPT’]

It gives the details of the accept of the HTTP request if exists or else none.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['HTTP_ACCEPT'];
?>
</body>
</html>

Output:

The accept request if exists else none.

12. $_SERVER[‘HTTP_HOST’]

It gives the name of the host where the server is hosted.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['HTTP_HOST'];?>
</body>
</html>

Output:

The host Name.

13. $_SERVER[‘HTTP_REFERER’]

This returns the complete URL of the current page.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['HTTP_REFERER'];
?>
</body>
</html>

Output:

https://www.google.com

14. $_SERVER[‘REMOTE_HOST’]

It gives the host name from where user is viewing the page.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['REMOTE_HOST'];
?>
</body>
</html>

Output:

The host name from where the page is accessed.

Above were some methods that were used to get the details from the $ Server variable of PHP. There are other function also that are used with this variable toget the details about this like:-

$_SERVER['SERVER_PORT'],$_SERVER['SERVER_SIGNATURE'],
$_SERVER['PATH_TRANSLATED'],
$_SERVER['SCRIPT_NAME'],
$_SERVER['SCRIPT_URI']

Note: Some of the screenshot didn’t have the output as the output depends on the configuration we have set for the particular PHP server .

Conclusion

From the above article we saw the use of Function $Server in PHP. From various example and classification, we tried to understand how the $server function works in PHP and what are is use in the programming level. We also saw the internal working and the advantages of having the type of data which we are defining for various programming purpose. Also the syntax and examples helped us to understand much precisely over the function.

Recommended Articles

This is a guide to PHP $_SERVER. Here we also discuss the introduction and working of $_ server function along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. PHP Zip Files
  2. PHP Object to String
  3. PHP Stack Trace
  4. PHP XML Parser

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training Course Learn More