Era Host hosting
EraHost – Free Domain, Cheap Hosting!
Client Area
Support 24/7
Menu

How to Add a Vary: Accept Header Using PHP

3 min read
07.05.2026

The Vary: Accept HTTP response header is used when the content served by your application may vary based on the Accept header sent by the client. This is particularly useful when your server supports content negotiation for different media types, such as JSON, XML, or HTML.

PHP Vary Accept Header
PHP — `header("Vary: Accept");` before any output.

For closely related Vary / caching topics, see Gzip and Vary: Accept-Encoding, cPanel Vary: Accept Header, and How to Specify Vary: Accept-Encoding in Nginx.

You can add the Vary: Accept header dynamically in PHP by using the header() function.

Step-by-Step Guide

Add the Header in PHP

Use the header() function to include the Vary: Accept header in the HTTP response. This ensures that caches recognize that the resource might differ based on the Accept request header.

Example:

<?php

// Add the Vary: Accept header

header("Vary: Accept");



// Optional: Serve content based on the client Accept header

$acceptHeader = $_SERVER['HTTP_ACCEPT'];



if (strpos($acceptHeader, 'application/json') !== false) {

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

    echo json_encode(['message' => 'This is a JSON response']);

} elseif (strpos($acceptHeader, 'text/xml') !== false) {

    header('Content-Type: text/xml');

    echo '<?xml version="1.0"?><message>This is an XML response</message>';

} else {

    header('Content-Type: text/html');

    echo '<html><body><p>This is an HTML response</p></body></html>';

}

?>
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

When to Use Vary: Accept

You should use this header if:

  • Your server or application delivers different representations of a resource (e.g., JSON, XML, or HTML) based on the Accept header in the request.
  • You want to ensure that caching mechanisms (e.g., CDNs, browsers) store and serve the correct version for each type of Accept request.

Verify the Header

You can verify the Vary: Accept header using:

  • Browser Developer Tools:
    • Open the browser developer tools (F12).
    • Go to the Network tab.
    • Reload the page and inspect the response headers.
  • cURL: Run the following command:
  • curl -I https://yourdomain.com/your-script.php

    Look for the Vary: Accept header in the response.

Why Vary: Accept is Important

  1. Cache Differentiation: Ensures that CDNs, proxies, and browsers store separate versions of the resource based on the Accept request header.
  2. Content Negotiation: Properly informs caching mechanisms and clients that different representations are available.
  3. Improved SEO: Search engines like Google use Vary headers to index appropriate content versions (e.g., mobile-specific or JSON APIs).

Best Practices

  1. Use the Header Only When Necessary: Overusing Vary headers (e.g., Vary: Accept, User-Agent) can lead to cache fragmentation and reduced efficiency.
  2. Combine with Proper Cache-Control Headers: Ensure the resource has appropriate caching policies alongside the Vary header.
  3. Test Across Clients: Verify that different client requests receive the appropriate response type.
  4. Document Supported Media Types: Clearly document which Accept values your application supports for easier debugging and integration.

By adding the Vary: Accept header with PHP, you ensure proper caching and content negotiation for your application, improving performance and compatibility.

Frequently asked questions
Yes — PHP error `headers already sent` if you call header() after anything written to output buffer. Set at the very top of script, before any echo or include that might emit. If you absolutely need late header(), enable output buffering: `ob_start()` at top, flush at end.
Both work. `header('Vary: Accept, Accept-Encoding');` sends one combined header. Or `header('Vary: Accept', false); header('Vary: Accept-Encoding', false);` — second arg false appends rather than replaces. HTTP spec treats both as equivalent. Combined form is more readable.
Each unique Vary'd header value creates a cache entry. For CDN, Vary: Accept with 5 content types = 5x storage. For Accept-Language with 20 locales = 20x. Normalize before responding or accept the fragmentation. Cloudflare normalizes Accept-Language automatically; self-hosted caches don't.
`curl -sI https://yoursite.com/page.php | grep Vary`. Returns the actual Vary header. If empty, header didn't set or was overridden. If wrong, another header() call later replaced it. Use header_list() in PHP to see all headers PHP intends to send.
Related articles
How to Add Vary: Accept Header Using cPanel
Gzip and Vary: Accept-Encoding Header
Set Encoding for Dynamic Content with charset=windows-1251