- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP getallheaders(): Get All Headers
The PHP getallheaders() function is used when we need to fetch all HTTP request headers. For example:
<?php $headers = getallheaders(); foreach ($headers as $key => $value) { echo "$key: $value"; echo "<BR>"; } ?>
The output of the above PHP example on the getallheaders() function is:
That is:
Host: localhost Connection: keep-alive Cache-Control: max-age=0 sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100" sec-ch-ua-mobile: ?0 sec-ch-ua-platform: "Windows" Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 Sec-Fetch-Site: none Sec-Fetch-Mode: navigate Sec-Fetch-User: ?1 Sec-Fetch-Dest: document Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Cookie: foo=bar
The above example can also be written in this way:
<?php foreach(getallheaders() as $key => $value) echo "<b>", $key, "</b>: ", $value, "<BR>"; ?>
Now the output should be:
You can also show the information of all HTTP headers in the form of a table using the following PHP script:
<?php echo "<table><tr>"; echo "<th>Name</th>"; echo "<th>Value</th>"; echo "</tr>"; foreach(getallheaders() as $key => $value) { echo "<tr>"; echo "<td>", $key, "</td>"; echo "<td>", $value, "</td>"; echo "</tr>"; } echo "</table>"; ?>
PHP getallheaders() syntax
The syntax of the getallheaders() function in PHP is:
getallheaders()
Advantages of the getallheaders() function in PHP
- The getallheaders() function is very simple to use, and it only takes one call to retrieve all of the headers.
- All headers sent in the current request are returned by the getallheaders() function, making it useful for applications that need to access and manipulate headers.
- The HTTP specification has standardized the getallheaders() function, ensuring compatibility across different web servers and platforms.
- Use getallheaders() to retrieve security-related headers such as Authorization or Cookie, allowing applications to implement appropriate security measures.
Disadvantages of the getallheaders() function in PHP
- The getallheaders() function can only be used with PHP versions 5 and up, and not all web servers may support it.
- When dealing with a large number of headers, the getallheaders() function may experience performance issues because it reads every header sent in the current request.
- Headers frequently have many components, including multiple values or various formats. These headers must be parsed carefully because errors could occur.
- Headers could potentially contain malicious code or be used to launch attacks like injection or cross-site scripting if they are not properly validated or sanitized (XSS).
« Previous Tutorial Next Tutorial »