How to determine if a page is called via Ajax or on its own

I have a page that loads other pages via Ajax (think frames except without frames).

Obviously, these pages can be called independently, so I want to determine if they are called via Ajax, and if not, redirect to the Ajax main page.

Pages are php pages, so I also have access to this.

index:

goto = "StandalonePrograms.php"; var clear = "<br style='clear:both;'>" if(goto != ''){ $.ajax({ url: goto, context: document.body, success: function(data){ $('#mainwindow').html(data + clear); $('#mainwindow').find("script").each(function(i){ eval($(this).text()); }); } }); } 
+8
jquery ajax php
source share
2 answers

Modern browsers add the following request header when a request is executed using the XMLHttpRequest object:

 X-Requested-With: XMLHttpRequest 

In PHP, check for the presence of this header using:

 $_SERVER['HTTP_X_REQUESTED_WITH'] 
+12
source share

You can never trust customers and their information! Headers can be tampered with by hackers (e.g. cURL), and even HTTP_X_REQUESTED_WITH is not reliable. There is no 100% reliable way to find out. The only way to use captcha ...

-one
source share

All Articles