PHP server variable

29 April 2009

The PHP server variable is a preset variable in PHP.

As the title, server variable, says - it does exactly that, holds an array of data about the server.

Using the server variable in PHP can be handy if you want to find out:

  • Where the user came from (the server referrer)
  • What the current URI is
  • The filename of the executing script
  • The server's IP address
  • The server's name
  • The current query string
  • What web browser (or user agent) is being used to view the website

The list goes on... And you can find the full list at:

A server variable can be called upon by using $_SERVER['array_item'] where array_item is one of the items held in the server variable array.

For example, to display the name of the user agent (web browser) that is currently being used to display a website you can do this with the server variable by using:


Ok, so it won't often be shown to you what user agent you're using - but it may be used in a "if" statement - If they're using Internet Explorer 6 or below do this, else do something else for example?

You will also want to do a little bit of testing when using this - like Internet Explorer for example is called MSIE (followed by the version number) when being referred to as the user agent.

The server variable is a very useful variable array and can be used in all kinds of ways - have a play and see what you come up with.

Please leave a comment using the form provided.