roopunk
roopunk

Reputation: 4361

php : Show the headers received from the browser

So when a browser makes a HTTP request to a server, it is in the form some headers (get/post, cookies, host, User Agent, etc..). Is there a way I can read and display them in a php script? And yes $_GET, $_POST and $_COOKIE are there alright. I was looking for the rest of header info. eg http://pgl.yoyo.org/http/browser-headers.php Thanks.

Upvotes: 4

Views: 10261

Answers (4)

Elastic Lamb
Elastic Lamb

Reputation: 353

My favorite: http://php.net/manual/en/function.apache-request-headers.php

Fetches all HTTP request headers from the current request.

<?php
    $headers = apache_request_headers();

    foreach ($headers as $header => $value) {
        echo "$header: $value <br />\n";
    }
?>

Upvotes: 6

Starx
Starx

Reputation: 79031

get_headers() function is what you are looking for. As quoted

get_headers — Fetches all the headers sent by the server in response to a HTTP request

Simple example:

$url = 'http://www.example.com';   
print_r(get_headers($url));   

Outputs all the information the send by the server:

Array (

[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix)  (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html )

Update

TO receive the information that is send by browsers, they can be accessed from $_SERVER super global variable. For example, the following snippet gives all the browser related information.

echo $_SERVER['HTTP_USER_AGENT'];

Similarly,

  • REQUEST_METHOD : gives the HTTP Request method such as GET, HEAD, Put, Post
  • HTTP_HOST: gives the HOST information
  • HTTP_COOKIE: gives the raw information about the Cookie header [Source]

Upvotes: 8

Jaxkr
Jaxkr

Reputation: 1293

Yes. Look into the $_POST, $_GET, and $_COOKIE superglobals. They are arrays that contain the header info.
http://www.tizag.com/phpT/postget.php

Upvotes: 0

Fluffeh
Fluffeh

Reputation: 33542

You can use the $_GET['requestName'], $_POST['requestName'] (or $_REQUEST['requestName'] to get either a GET or POST) to get these.

Cookies are via $_COOKIE['varName']. You may also want to look into $_SESSION[] as well.

Upvotes: 2

Related Questions