PHP apache_request_headers () has dialogs with reality (as Firebug confirms): why?

I wrote a PHP web application that uses Ajax requests (made using YUI.util.Connect.asyncRequest).

In most cases, this works great. The request is sent with an X-Requested-With value of XMLHttpRequest . My PHP controller code uses apache_request_headers () to check if the incoming Ajax request or not, and everything works fine.

But not always. With interruptions, I get a situation where an Ajax request is sent (and Firebug confirms that the request headers include X-Requested-With from XMLHttpRequest), but apache_request_headers () does not return this header in its list.

The result when I var_dump apache_request_headers () is as follows (note the lack of X -

'Host' => string 'peterh.labs.example.com' (length=26)
'User-Agent' => string 'Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.0.3) Gecko/2008101315 Ubuntu/8.10 (intrepid) Firefox/3.0.3' (length=105)
'Accept' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' (length=63)
'Accept-Language' => string 'en-gb,en;q=0.5' (length=14)
'Accept-Encoding' => string 'gzip,deflate' (length=12)
'Accept-Charset' => string 'ISO-8859-1,utf-8;q=0.7,*;q=0.7' (length=30)
'Keep-Alive' => string '300' (length=3)
'Connection' => string 'keep-alive' (length=10)
'Referer' => string 'http://peterh.labs.example.com/qmail/' (length=40)
'Cookie' => string 'WORKFLOW_SESSION=55f9aff2051746851de453c1f776ad10745354f6' (length=57)
'Pragma' => string 'no-cache' (length=8)
'Cache-Control' => string 'no-cache' (length=8)

But Firebug tells me:

Request Headers:
Host             peterh.labs.example.com
User-Agent       Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.0.3) Gecko/2008101315 Ubuntu/8.10 (intrepid) Firefox/3.0.3
Accept           text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8
Accept-Language  en-gb,en;q=0.5
Accept-Encoding  gzip,deflate
Accept-Charset   ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive       300
Connection       keep-alive
X-Requested-With XMLHttpRequest
Referer          http://peterh.labs.example.com/qmail/
Cookie           WORKFLOW_SESSION=55f9aff2051746851de453c1f776ad10745354f6

This mismatch is (apparently) intermittent when executing the same code. But I donโ€™t believe in โ€œintermittentโ€ when it comes to software! Help!

+5
source share
4 answers

I'm not sure why apache_request_headers () and firebug are incompatible, but in order to read request headers you can use super-global <_prever $ _SERVER

, ( , ) $SERVER. HTTP, (_)

:

$_ SERVER ['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'

+7

, , "" , . 302, X-Requested-With , . , .

+1

, $_SERVER. , apache_ *, .

, X-Requested - ? , , , .

0
source

I cannot specifically answer this case, but in general, I would recommend using the (request) parameter to request xmlhttp requests instead of the header. You never know which ridiculous security or proxy server could take on a script with HTTP headers or cache an AJAX response, which should have been a simple HTML response from the browser (or vice versa).

0
source

All Articles