4Answers
  • 10
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Detect Ajax calling URL

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 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
  • 2
Reply Report

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

Reference

  • 1
Reply Report
      • 1
    • Agreed - but the only other alternative I can think of is generate a unique hash stored in the session and include it in the AJAX request then check the user's session to see where it came from. That's kind of ugly and probably overkill.

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
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions