英文:
Get URL headers in PHP
问题
So, let's say I'm a client entering this URL on my blog website: https://blogbyme.com/post?id=1
.
I want the server to see that URL and customize the page based off what post it is.
The main question is - when the request is made, how do I grab the id
header's value to send the correct blog post?
Right now I've got this far.
if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = [];
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
and that's still not working.
英文:
So, let's say I'm a client entering this URL on my blog website: https://blogbyme.com/post?id=1
.
I want the server to see that URL and customize the page based off what post it is.
The main question is - when the request is made, how do I grab the id
header's value to send the correct blog post?
Right now I've got this far.
if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = [];
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
and that's still not working.
答案1
得分: 1
使用PHP的超全局变量$_GET
来获取参数:
echo $_GET["id"];
要使服务器根据请求的内容自定义页面,主要问题是 - 当请求发出时,如何获取id标头的值以发送正确的博客文章?
您可以从以下代码中获取完整的请求URI:
$_SERVER["REQUEST_URI"]
英文:
Use the superglobal of PHP $_GET
:
echo $_GET["id"];
> I want the server to see that URL and customize the page based off what post it is. The main question is - when the request is made, how do I grab the id header's value to send the correct blog post?
You can get the full request URI from:
$_SERVER["REQUEST_URI"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论