How to define ajax cross domain request in php

For a normal ajax request, I use:

strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'

      

But this doesn't work with cross domain request.

How can i do this?

+3


source to share


3 answers


Edit2: If you use jQuery.ajax function like this:

var request = $.ajax({
url: "http://somesite.com/somescript.php?somevar=somevalue",
dataType: "jsonp",
jsonp: 'callback',
success: function(data) {
alert('Done!');
}
});

      

Then you can check the variable $_SERVER['REQUEST_URI']

or just $_GET['callback']

and $_GET['_']

. REQUEST_URI will look like this:

/somescript.php?somevar=somevalue&callback=jQuery172028849187534502896_1333494007273& _ = 1333494443880

Edit: The answer below is to see if it's cross-domain or not, rather than checking if it's AJAX



Answer to the question "How to determine if an ajax call is from another domain":

I am using jQuery.ajax call and for me using variable $_SERVER['HTTP_REFERER']

works fine.

If I use the page on my local machine, this superglobal returns an empty string.

If I am using a page on the web, the value $_SERVER['HTTP_REFERER']

returns the url of the page that made the ajax call. So checking the meaning of this can tell you what you need to know.

+2


source


use $ _SERVER ['HTTP_ORIGIN'] to get the domain of the request



0


source


you can use php getallheaders () function. you can check host entry

-1


source







All Articles