获取PHP中的URL标题

huangapple go评论56阅读模式
英文:

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"]

huangapple
  • 本文由 发表于 2023年2月6日 02:21:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定