SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    349
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Setting a PHP $_SERVER variable

    I have read that there's a PHP variable "$_SERVER['HTTP_X_REQUESTED_WITH']", which can be used to detect Ajax requests. I understand it is only available on some servers. I hope, therefore, that it can be switched ON or OFF. It's certainly not listed with the PHP variables when I run 'phpinfo()' on XAMPP (localhost).

    Can anyone tell me if this variable can be switched on somewhere, and if so, how, please ? I've not found it in php.ini.

    I'm aware that if I can get it working locally I'll then have to persuade my remote host to switch it on there in due course.
    Tim Dawson
    Isle of Mull, Scotland

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    IIRC, just using a JS library which sends this header along with the AJAX request will populate this value.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTTP_X_REQUESTED_WITH is not something you switch on within PHP. It’s a request reader which is set and sent by JavaScript libraries when requesting a page asynchronously i.e. using AJAX. For instance jQuery does it and I guess so does Mootools. If you have a custom JS library you can configure it to do the same and easily check it from the server with PHP to tell an AJAX from one which is not.

  4. #4
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    349
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks. But I don't use a JS library (perhaps I should, but that's a different matter).

    If a library can send this header, I should be able to send it from my own JS (not quite sure of the syntax, though). However, if it's not shown in phpinfo() my understanding is that it's not 'provided' by the server, so there's no point in sending it. Is that correct ?
    Tim Dawson
    Isle of Mull, Scotland

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    JavaScript sends the header, which in turn, populates the server-side variable. If you don't send the header, it won't be there. If you do...

    As to how to send a header, try taking a look at XMLHTTPRequest and the setRequestHeader method.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    request.setRequestHeader() is how you add a header to an ajax request.

    Code JavaScript:
    request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

    'request' is an xmlHttpRequest object.

  7. #7
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    349
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you both. I'll try it and let you know what happens.
    Tim Dawson
    Isle of Mull, Scotland


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •