Strict URL encode for HTTP link “preconnect” in PHP

Premising that I just want to be nice and generate this in a safe way:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Link: <https://example.com>; rel="preconnect"
</code>
<code>Link: <https://example.com>; rel="preconnect" </code>
Link: <https://example.com>; rel="preconnect"

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link

Premising that accordingly to the Mozilla Developers Documentation:

The URI (absolute or relative) must encode char codes greater than 255:

Question:

How would you “encode char codes greater than 255” in PHP?

I’m not looking for something that “works”, I would like something that respects the standard (since in Internet “we” try to be permissive about what I accept, and rigid about what I give).

Thanks for any help for PHP.


Counter examples

The rawurlencode() (that follows RFC 3986) is incorrect because it escapes : and /:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>echo rawurlencode('https://example.com/苗条');
</code>
<code>echo rawurlencode('https://example.com/苗条'); </code>
echo rawurlencode('https://example.com/苗条');

Result (wrong):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>https%3A%2F%2Fexample.com%2F%E8%8B%97%E6%9D%A1
</code>
<code>https%3A%2F%2Fexample.com%2F%E8%8B%97%E6%9D%A1 </code>
https%3A%2F%2Fexample.com%2F%E8%8B%97%E6%9D%A1

The urlencode() is also incorrect, same result (as expected):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>echo urlencode('https://example.com/苗条');
</code>
<code>echo urlencode('https://example.com/苗条'); </code>
echo urlencode('https://example.com/苗条');

Result (wrong):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>https%3A%2F%2Fexample.com%2F%E8%8B%97%E6%9D%A1
</code>
<code>https%3A%2F%2Fexample.com%2F%E8%8B%97%E6%9D%A1 </code>
https%3A%2F%2Fexample.com%2F%E8%8B%97%E6%9D%A1

Workaround

I may just use rawurlencode() and then restore some characters. Stuff like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>echo str_replace(['%3A', '%2F', '%3C', '%3E'], [':', '/', '<', '>'], rawurlencode('https://example.com/苗条'));
</code>
<code>echo str_replace(['%3A', '%2F', '%3C', '%3E'], [':', '/', '<', '>'], rawurlencode('https://example.com/苗条')); </code>
echo str_replace(['%3A', '%2F', '%3C', '%3E'], [':', '/', '<', '>'], rawurlencode('https://example.com/苗条'));

Result (correct):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>https://example.com/%E8%8B%97%E6%9D%A1
</code>
<code>https://example.com/%E8%8B%97%E6%9D%A1 </code>
https://example.com/%E8%8B%97%E6%9D%A1

But, to respect the standard, I should probably loop chr() from zero to 255 to get all rawurlencode(chr(1..255)) to restore them… so this is becoming a bit hacky and overkill.

Current Approach

So, trying to be strict about “encode char codes greater than 255”, so, I tried looping every single character(/byte?) and only run rawurlencode() if the result is 255, and it works well:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
/**
* Encode char codes greater than 255.
* @param string $s
* @return string
*/
function escape_link_header($s) {
$result = '';
foreach (mb_str_split($s) as $c) {
if(mb_ord($c) >= 255) {
$c = rawurlencode($c);
}
$result .= $c;
}
return $result;
}
echo escape_link_header('https://example.com/苗条');
</code>
<code><?php /** * Encode char codes greater than 255. * @param string $s * @return string */ function escape_link_header($s) { $result = ''; foreach (mb_str_split($s) as $c) { if(mb_ord($c) >= 255) { $c = rawurlencode($c); } $result .= $c; } return $result; } echo escape_link_header('https://example.com/苗条'); </code>
<?php
/**
 * Encode char codes greater than 255.
 * @param string $s
 * @return string
 */
function escape_link_header($s) {
    $result = '';
    foreach (mb_str_split($s) as $c) {
        if(mb_ord($c) >= 255) {
            $c = rawurlencode($c);
        }
        $result .= $c;
    }
    return $result;
}

echo escape_link_header('https://example.com/苗条');

So the code is strict and the result seems correct to me:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>https://example.com/%E8%8B%97%E6%9D%A1
</code>
<code>https://example.com/%E8%8B%97%E6%9D%A1 </code>
https://example.com/%E8%8B%97%E6%9D%A1

Premising that this sounds a bit esoteric to me, is this a correct approach in PHP?

Any other approach to “encode char codes greater than 255” to have a safe <...> value for the Link HTTP header?

Again, the goal is to “just” strictly sanitize the <...> in this part:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>header('Link: <https://example.com>; rel="preconnect"');
</code>
<code>header('Link: <https://example.com>; rel="preconnect"'); </code>
header('Link: <https://example.com>; rel="preconnect"');

Thanks!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật