25-07-2008

PHP PHP class - ajax bridge

This is a very simple bridge script that enables you to direct ajax calls directly at PHP classes.
Do consider security when using this.

<?php
//no cache:
header("Expires: Mon, 26 Jul 1990 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
 
// include whatever classes you want to open up to ajaxCalls
include('./classes/someClass.php');
include('./classes/whateverClass.php
 
$object = false; 
if ($_REQUEST['classname']){
	$object=new $_REQUEST['classname']; 
}
if ($object){
	$functioncalled = $_REQUEST['function'];
	$args = array();
	if($_REQUEST['args']) $args = explode(',',$_REQUEST['args']);
 
	echo call_user_func_array(array(&$object, $functioncalled),$args);	
} else {
	echo 'error occured';
}
 
 
?>

This is an example bit of jQuery javascript code using the above bridge:

$.ajax({
  type: "POST",
  url: "ajax.php",
  data: "classname=Menu&function=storeCategory&args=1,2,3",
  success: function(reply){
    alert(reply);
  }
});

Comments:

Your comment:

»

 

[x]