• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Php Tip: Use header() to send a raw HTTP header

December 19, 2023

Php Tip: Use header() to send a raw HTTP header

When it comes to web development, PHP is one of the most popular programming languages. It offers a wide range of features and functions that make it easy to build dynamic and interactive websites. One such function is header(), which allows you to send a raw HTTP header to the browser.

What is a raw HTTP header?

Before we dive into the details of using the header() function, let’s first understand what a raw HTTP header is. When a web server sends a response to a browser, it includes a set of headers that provide additional information about the response. These headers can include details such as the content type, cache control, cookies, and more.

A raw HTTP header is a string of text that follows a specific format and contains information about the response. It typically consists of a header name followed by a colon and a space, and then the header value. For example, the Content-Type header specifies the type of content being sent, such as HTML, JSON, or XML.

Using the header() function

The header() function in PHP allows you to send a raw HTTP header to the browser. It takes a single argument, which is the header string you want to send. Let’s take a look at an example:

header("Content-Type: application/json");

In this example, we are using the header() function to set the Content-Type header to application/json. This tells the browser that the response will be in JSON format.

You can also use the header() function to set other headers, such as the Location header to redirect the browser to a different page:

header("Location: https://example.com");

This will redirect the browser to the specified URL.

Why use the header() function?

The header() function is particularly useful when you need to send specific headers to the browser. For example, if you are building a RESTful API, you can use the header() function to set the appropriate Content-Type header based on the requested resource.

Additionally, the header() function allows you to control the caching behavior of your web pages. By setting the Cache-Control header, you can specify how long the browser should cache the page and when it should request a fresh copy from the server.

Conclusion

The header() function in PHP is a powerful tool that allows you to send raw HTTP headers to the browser. It provides you with fine-grained control over the response headers, allowing you to set the appropriate content type, redirect the browser, and control caching behavior.

For more information about PHP and web development, consider exploring Server.HK, a leading VPS hosting company that offers top-notch hosting solutions for your website.

Recent Posts

  • How to Set Up Nginx on CentOS Stream for High-Performance Web Hosting
  • CentOS Stream Explained: Key Differences from CentOS Linux
  • How to Configure FirewallD in CentOS Stream: From Essential to Production-Grade
  • Installing Docker on CentOS: A Practical Setup Guide (CentOS Stream 9/10 – 2026)
  • How to Secure a CentOS Server: 15 Essential Hardening Techniques (CentOS Stream 9/10 – 2026)

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot