PHP Proxy script to solve javascript and jquery cross domain issues

I just finished a project in which a jquery dialog used a jquery.ajax call using get to obtain details of a transaction from a url on another domain, effectively a cross domain ajax request. Since firefox 3.0 does not allow itself to be set to allow cross domain requests, I had to use some other way. I could have just used an iframe, but that is another story in itself.

Here is the bit of javascript code.

[sourcecode language="js"]

function refaxorder(){
query=”urlid=1&inOID=” + idfield + “&inKey=” + key + “&inAction=reFax”;


$(“#actiondivin”).dialog({autoOpen:false, modal:true, title:”ReFax”, buttons:{OK: function(){$(this).dialog(‘destroy’).remove(); }}});;

$.ajax({type:”GET”, url: “proxy.php”, data: query,  dataType:”html”, success:function(msg){


To get around this, I implemented a couple of lines as proxy.php to act as a proxy. Please note that I am only allowing it to be passed the urlid instead of a url to prevent it from becoming an open proxy. Also IP based filtering could be implemented to make it more safer. Have a look, maybe it will come in useful for you too.

[sourcecode language='php']

if ($key!=’url’) $query[]=$key.”=”.$value;
if (count($query)) $querystring=implode(“&”,$query);

$page = ”;
if ($fp===false) die(“Unable to open”);
while (!feof($fp)) {
$page .= fread($fp, 8192);
print $page;


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>