Search code examples
phprequest-headers

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.


Solution

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