Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Adding a Contact Form Checking the Request Method

$_SERVER["REQUEST_METHOD"]

Hello!

"$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these;"

1)This is a passage I got from a site that someone recommended for me to read, but I am still a little confused by its function. Is the purpose of $_SERVER() is to get all variables in headers, path and script and store them in an array?

2) In code, how does headers, path and script locations look like?

3) I tried to use <?php var_dump($_SERVER)?> but it displayed an error on my screen. Can you please tell me where in this project should I put this var_dump($_SERVER) so that I can see what is being stored in this array?

4) What is the meaning of "REQUEST_METHOD" why does $_SERVER() need it as its argument, when it is supposed to be an array storing all kinds of variables. Why does $_SERVER() need an argument?

======================================================== Hello all,

I believe some changes were made to the Post page that are affecting the the Best Answer option display.

**Please make sure to reply to the above post under Add an Answer section below****

Cheers all!

Thanks!

1 Answer

Henrik Hansen
Henrik Hansen
23,176 Points

Just put the the var_dump($_SERVER) in a new phpfile, and you will see the structure.

<?php

var_dump($_SERVER);

You can get the root path from this array, i recommend

$docRoot = filter_input( INPUT_SERVER, 'document_root');

There is a bunch of other data stored in this array.

The request method is mainly GET or POST method, it is the way the request was sent.

regarding $_SERVER() $_SERVER is an array, not a method.

  'REQUEST_METHOD' => string 'GET' (length=3)
  'HTTP_HOST' => string 'localhost' (length=9)
  'HTTP_USER_AGENT' => string 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0' (length=72)
  'HTTP_ACCEPT' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' (length=63)
  'HTTP_ACCEPT_LANGUAGE' => string 'sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3' (length=35)
  'HTTP_ACCEPT_ENCODING' => string 'gzip, deflate' (length=13)
  'HTTP_CONNECTION' => string 'keep-alive' (length=10)  
  'SERVER_SOFTWARE' => string 'Apache/2.4.9 (Win64) PHP/5.5.12' (length=31)
  'SERVER_NAME' => string 'localhost' (length=9)
  'SERVER_ADDR' => string '::1' (length=3)
  'SERVER_PORT' => string '80' (length=2)
  'REMOTE_ADDR' => string '::1' (length=3)
  'DOCUMENT_ROOT' => string 'E:/Dropbox/Lovella AB/public_html/' (length=34)
  'SCRIPT_NAME' => string '/testor.php' (length=11)
  'PHP_SELF' => string '/testor.php' (length=11)
  'REQUEST_TIME_FLOAT' => float 1422735267.585
  'REQUEST_TIME' => int 1422735267
// ...