AJAX Php ensure page loaded in framework SOLUTION; same using LightWindow; Ajax.Updater OR Ajax.Request ALSO handy $_SERVER['REQUEST_METHOD']=="GET" trick

Wow, I tried a MILLION solutions to this,
and some of them worked...almost...perfectly...
and along the way I learned A LOT,
including some awesome JavaScript and PHP logic, URL sniffers, decision makers, GLOBALS, and so on...
but now, of course, the TRUE solution,
and duh, of course, it's SIMPLE, the SIMPLEST

Scenario, AJAX framework,
loads external pages into a div in the framework,
we don't want said external pages loaded by themselves, that is, their URLs should be visit-able...

Solution:
when AJAX function that loads an external page calls for it,
it also submits with its request a form, or a variable, to the page being loaded in,
page being called looks for that variable,
and without it, follows another line of action,
DUH!

example using Prototype AJAX functions:

AJAX.Updater Version:
button or link on framework base main page calls this function below, sending the variable camefromhome (could be anything), with a value of camefromhome (again, could be anything), requesting that page_to_load be loaded into the div_to_fill div:

<script>
function doit(page_to_load)
{
new Ajax.Updater('div_to_fill', page_to_load,
{
method: 'get',
parameters: { camefromhome:'camefromhome' },
onCreate: load_started_function,
onComplete: load_finished_function
}
);
}
</script>

the page_to_load.php or similar contains this sniffer for sent token variable camefromhome to see if it was called by the framework or NOT! :

<?
if(($_SERVER['REQUEST_METHOD']=="GET")&&($_GET['camefromhome'] == 'camefromhome'))
{
//echo "loaded inside the framework, all clear";
}
else
{
...
}
?>


AJAX.Request Version:
Version that sends a variable with a Lightwindow javascript call and checks as above:

first modify lightwindow.js roughly line 1347 to look something like this:

<script>
_loadWindow : function() {
switch (this.windowType) {

case 'page' :
var pars = 'camefromhome=camefromhome';
var newAJAX = new Ajax.Request(
this.contentToFetch, {
method: 'get',
parameters: pars,
onComplete:
function(response){
$('lightwindow_contents').innerHTML += response.responseText;
this.resizeTo.height = $('lightwindow_contents').scrollHeight+(this.options.contentOffset.height);
this.resizeTo.width = $('lightwindow_contents').scrollWidth+(this.options.contentOffset.width);
this._processWindow();
}.bind(this)
}
);
break;

default : throw("Page Type could not be determined, please amend this lightwindow URL "+this.contentToFetch);
break;
}
}
</script>

then in the document getting loaded in check for the variable being passed or get outta there:

<?
if($_GET['camefromhome']=='camefromhome')
{}
else
{
header('Location:http://www.framework_home.com'); }
?>

Posted on 20 Nov 2009, 16:28 - Category: AJAX
Edit - Delete


No comments posted yet.
Loading