最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Detect Ajax calling URL - Stack Overflow

programmeradmin2浏览0评论

I have an HTML document, which loads content from a PHP file using an AJAX call. The important bit of my code is below:

default.html :

/*more code above*/
var PHP_URL = "content.php";
var Content = document.getElementById('Content');
ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange =
    function() {
        if(ajaxRequest.readyState==4) {
            if (ajaxRequest.status==200)
                Content.innerHTML = ajaxRequest.responseText;
            else
                Content.innerHTML = "Error:<br/>unable to load page at <b>"+PHP_URL+"</b>";
            Content.className = "Content Solid";
        }
    }
ajaxRequest.open("GET",PHP_URL,true);
ajaxRequest.send();
/*more code below*/

Is it possible for the file at 'content.php' to detect that it has been called from 'default.html', or a different calling document as necessary?

I have an HTML document, which loads content from a PHP file using an AJAX call. The important bit of my code is below:

default.html :

/*more code above*/
var PHP_URL = "content.php";
var Content = document.getElementById('Content');
ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange =
    function() {
        if(ajaxRequest.readyState==4) {
            if (ajaxRequest.status==200)
                Content.innerHTML = ajaxRequest.responseText;
            else
                Content.innerHTML = "Error:<br/>unable to load page at <b>"+PHP_URL+"</b>";
            Content.className = "Content Solid";
        }
    }
ajaxRequest.open("GET",PHP_URL,true);
ajaxRequest.send();
/*more code below*/

Is it possible for the file at 'content.php' to detect that it has been called from 'default.html', or a different calling document as necessary?

Share Improve this question edited Jul 16, 2010 at 16:53 Bill the Lizard 406k211 gold badges572 silver badges889 bronze badges asked Jun 26, 2010 at 16:39 OrigamiguyOrigamiguy 1,3242 gold badges13 silver badges19 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 13

Most well-known Ajax frameworks like jQuery and mooTools add a specific header which you can check with PHP:

if (strcasecmp('XMLHttpRequest', $_SERVER['HTTP_X_REQUESTED_WITH']) === 0)
{
    // Ajax Request
}

I guess the best would be to set a request header in your AJAX call, such as

st.setRequestHeader('X-Sent-From','default.html')

then in content.php,

$sentFrom=$_SERVER['HTTP_X_SENT_FROM']; // outputs default.html

$_SERVER['HTTP_REFERER'] might be what you want

Reference

  • http://php.net/manual/en/reserved.variables.server.php

It is not possible to simply detect that a request came from an AJAX call on the server. You could, however, add a parameter that you send when requesting it via AJAX that indicates it is coming from an ajax call.

For example:

/*more code above*/
var PHP_URL = "content.php?mode=AJAX";
var Content = document.getElementById('Content');
ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange =
    function() {
        if(ajaxRequest.readyState==4) {
            if (ajaxRequest.status==200)
                Content.innerHTML = ajaxRequest.responseText;
            else
                Content.innerHTML = "Error:<br/>unable to load page at <b>"+PHP_URL+"</b>";
            Content.className = "Content Solid";
        }
    }
ajaxRequest.open("GET",PHP_URL,true);
ajaxRequest.send();
/*more code below*/

If simply detecting that the call came from default.html is enough (and not distinguishing between an AJAX call or a clicked link), then checking the Referrer header will do the trick, as suggested by @Jamie Wong.

发布评论

评论列表(0)

  1. 暂无评论