Sohil Sardhara
Sohil Sardhara

Reputation: 153

PHP header request not received

I am passing header by ajax,

$.ajax({
     headers: {
          key: value
     }
});

I can see the value in Request Header in the browser. I am using Codeigniter to get key by $this->input->get_request_header('key').

It is working fine in local but not working on the server.

I have tried apache_request_headers() and $_SERVER and getallheaders() and etc.. but i cannot see my custom header in this array.

So how to get this custom header by PHP?

Upvotes: 2

Views: 2242

Answers (2)

Szymon Cybulski
Szymon Cybulski

Reputation: 148

I had this issue with Codeigniter 3 and Authorization header. It was working locally but didn't work on the server. I found out that other headers work – I've changed Authorization to Authorization2 just to test. The PHP getallheaders() method was also returning all headers with the Authorization header filtered out.

This led me to this answer so I've added:

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

to the .htaccess file and the Authorization header reappeared.

Upvotes: 2

This is an example of how to set a header in an Ajax request.

$.ajax({
    type: "POST",
    beforeSend: function (request) {
        request.setRequestHeader(key, value);
    },
});

By sending it that way you don't have to have problems receiving it in your backend with Codeigniter 3.1.11 which is the one that includes the function you are using.

Returns a single member of the request headers array or NULL if the searched header is not found.

$this->input->get_request_header('some-header', TRUE);

Upvotes: 0

Related Questions