Very short snippet: how to identify calls Ajax in PHP

When building an Ajax Gateway, ie a file that is invoked by the object XmlHTTPRequest JavaScript, you can inadvertently create a tunnel for unwelcome intrusion. In fact, being a file like the others, located on our server is reachable from the address bar of your browser. Fortunately there is a simple way to identify the type of request:

1
2
3
4
5
@ isset ( $_SERVER [ 'HTTP_X_REQUESTED_WITH' ] ) ) { if (@ isset ($ _SERVER ['HTTP_X_REQUESTED_WITH'])) {
/ / Called by the HTTPRequest (Ajax)
{ Else {}
/ / Do nothing or displays an alert
}

In this way we prevent calls from the address bar of your browser. The chicciola (@, at) is not strictly necessary, however, if the headers were already sent to the page, you could get an unpleasant Warning.

4 comments: "Very short snippet: how to identify the Ajax calls in PHP"

  1. April 8, 2010 Michael:

    To me, this script does not work ... the ajax calls remain as "stale" ...

  2. April 8, 2010 Giovambattista Fazioli :

    @ Michael: Probably depends on your server, especially PHP ones. ] viene impostata automaticamente dall'ambiente server quando riceve una richiesta dall'oggetto XmlHTTPRequest . The variable $_SERVER [ 'HTTP_X_REQUESTED_WITH' ] is set automatically from the server when it receives a request from the object XmlHTTPRequest . Try checking your settings, partly because it works for me ... :)

  3. April 8, 2010 Michael:

    @ Giovambattista Fazioli: Excuse my ignorance, but I do not know how to check this setting : (

  4. April 8, 2010 Giovambattista Fazioli :

    @ Michael: If you can not ask your server administrator, make good the script that you used, perhaps the problem is in code and not the server. ] o, se possiedi FireFox con FireBug, alla verifica dei dati inviati e ricevuti sulla rete. Try a simple check on the $_SERVER [ 'HTTP_X_REQUESTED_WITH' ] or, if you have FireFox with FireBug, verification of data sent and received on the network.

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA