开发者

URL writing using $_SERVER parms

开发者 https://www.devze.com 2023-03-10 22:39 出处:网络
So I have a bunch of URLs that I need to write, Is it bad practice to begin the urls with: $_SERVER[\'http_host\']

So I have a bunch of URLs that I need to write, Is it bad practice to begin the urls with:

$_SERVER['http_host']

so my URL would look like this:

ec开发者_JAVA百科ho"<a href='http://".$_SERVER['HTTP_HOST']."/".$category."/".$article."'>link</a>";


It should be uppercase HTTP_HOST. And while this is a client-supplied value, on most shared hosting servers this field is pre-filtered by Apache. So it's safe to use this way (apply htmlspecialchars anyway).

You forgot the http:// protocol prefix however for your link.

If you need it for a "bunch of urls" it would make sense to package it into a separate function. Commonly this also handles the optional HTTP_PORT being anything but the standard 80.


Terrible practice. It won't even work.

  1. It is HTTP_HOST not http_host
  2. You are missing the protocol

Even if it did, you'd end up with code that was much easier to read if you simply used relative URIs and interpolation:

echo "<a href='/$category/$article'>link</a>";
0

精彩评论

暂无评论...
验证码 换一张
取 消