A I have a PHP file that if the user access it directly he should be redirected to another location, but if my script call it through ajax, it should do nothing special.
For example, if a user access
/site/page.php
he should be redirected to
/index.php?view=page
But if he is on the index.php?view=page the file should load without redirects.
How can I do that?
EDIT: If you want to determine if a script was requested through Javascript or not, you’ll have to signal it somehow.
Several toolkits define the header
X-Requested-With. In that case, you can check for a Javascript call with:You can check the size of the result given bydebug_backtrace.Alternatively (better), you can check
$_SERVER['SCRIPT_FILENAME']: