PHP: print '<meta http-equiv="refresh" content="0;url=' with URL parameters

PHP: print '<meta http-equiv="refresh" content="0;url=' with URL parameters

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Escaping and Encoding in PHP Meta Refresh Tags

Building a meta refresh tag in PHP requires careful attention to how characters are interpreted by the browser versus how they are handled by the server output stream. The specific issue you encountered—where b becomes ) or parameters fail entirely—is a classic collision between HTML entity encoding and URL query string encoding.

When you write print '&lt;meta ...&gt;', you are explicitly telling PHP to output the literal characters < and > because those entities represent angle brackets in HTML. If your goal is to output raw HTML that triggers a browser redirect, this approach is actually correct for the structural tags. However, the content inside the attributes tells a different story.

The core problem lies in mixing two distinct encoding schemes: HTML entities and URL query parameters. The character & serves as a delimiter in URLs (e.g., ?a=1&b=2) but also acts as the start of an HTML entity reference (e.g., &lt;). If you simply pass a string with unencoded ampersands into echo or print, PHP's output buffering might not automatically handle the context correctly if mixed with other encoding functions, or worse, you might be double-encoding.

Consider this flawed attempt:

$url = 'http://domain.com?a=1&b=2';
print '<meta http-equiv="refresh" content="0;url=' . $url . '">' . "\n";

If this code runs in a standard PHP context without output buffering or specific escaping, the browser receives: ?a=1&b=2. While browsers often handle unescaped ampersands gracefully within attribute values, relying on this is brittle. More importantly, if you try to manually escape the HTML entities first using htmlspecialchars() or htmlentities(), you must ensure you do not double-encode the URL parameters.

The Correct Approach: Context-Aware Encoding

The most robust solution involves separating the concerns of HTML structure and URL data. You should construct the HTML string with proper entity encoding for the tag itself, but leave the URL parameters unencoded (or specifically encoded using rawurlencode) depending on where they appear.

If you are generating this string to be inserted into a template or concatenated dynamically, ensure that the ampersands inside the url attribute are treated as literal characters within an HTML attribute value. In standard HTML parsing, an ampersand inside a double-quoted attribute only needs to be escaped if it is followed by a valid entity name (like &b;). Since &b is not a valid entity, most modern parsers handle &b=2 fine. However, strict HTML5 validation and some legacy browsers prefer the ampersand to be escaped as &amp; within attribute values to avoid ambiguity.

Here is the safe implementation pattern:

$url = 'http://domain.com?a=1&b=2'; // Keep URL parameters as-is or use rawurlencode() if needed
$htmlTag = '<meta http-equiv="refresh" content="0;url=' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '">';
print $htmlTag;

Using htmlspecialchars() on the variable $url is crucial here. It converts special characters in the variable to their HTML entities (& becomes &amp;, < becomes &lt;). This ensures that if your URL parameter contains a value like b=5>, it won't break the HTML structure. The browser will then parse &amp;b=2 correctly because, within an attribute context, &amp; is interpreted as a literal ampersand.

Understanding the "Double Encoding" Trap

The specific error where b turns into ) usually stems from double-encoding or incorrect entity usage. For instance, if you manually replace & with &amp; inside the variable before passing it to htmlspecialchars(), and then htmlspecialchars() sees an ampersand, it might encode it again or behave unpredictably depending on the flags used.

If you attempted to use &amp;amp; in your raw string, you created a three-layer encoding issue:

  1. The string literal contains &.
  2. You manually changed it to &amp;.
  3. The parser sees &amp; and thinks "Oh, that's an entity." It stops there, but if the browser then tries to parse the rest of the string, it gets confused by the trailing characters or the context switch.

Modern Alternatives

While meta refresh tags work for simple redirects, they are deprecated in modern HTML specifications. The refresh attribute is considered obsolete because it relies on client-side parsing which can be blocked by strict privacy settings or disabled by users. If you have full control over the application stack, consider issuing a direct HTTP redirect using headers instead of relying on a meta tag.

header('Location: http://domain.com?a=1&b=2', true, 302);
exit;

This method is faster, cleaner, and doesn't require waiting for the browser to parse the HTML document before redirecting. However, if your requirement specifically mandates a meta refresh (perhaps for SEO compatibility or legacy system constraints), stick to the htmlspecialchars pattern described above. It guarantees that the URL parameters are safely embedded in the HTML attribute without breaking the document structure.

Our network