2

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?

1
  • Which version of codeigniter are you using, since 4.0.3 is very different from 3.1.11 Commented Jun 24, 2020 at 18:37

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.