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.
- It is
HTTP_HOST
nothttp_host
- 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>";
精彩评论