'Check if my GET request has Header: Token

I want to send a request with or without 'Token' as a header.

  • If request has 'Token' as a header: if the user already has that item, it will return the item with the proper item_id of a specific user (based on its token), otherwise it will return null.

  • If request doesn't have 'Token' as a header: it will return the item with that item_id

I'm working with Zend Framework and in ItemResource I have this method:

public function fetch($id)
    {
}

How can I check if my request has Token as a header or not and implement both cases inside fetch()?



Solution 1:[1]

You can get all HTTP header values by getallheaders() or just get the specific value by $_SERVER['HTTP_XXX'], in your case, replace XXX with Token, $_SERVER['HTTP_Token'].

Manual: https://www.php.net/manual/en/reserved.variables.server.php

public function fetch($id)
{
    $token = $_SERVER['HTTP_Token'];
    // do your busniess code 
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jun Pan