I’ve a PHP document, let’s say jsonarray.php.
This file returns the json_encode of an associative array when some parameters are given.
I need to forbid an unwanted user to access and use this file for his own purposes.
Is this possible?
In addition, I want to specify that it is a client-side request, which I make in a JavaScript code.
if you are looking this from another PHP file, you can set
BASEPATHconstant there & add the following line in top of all the files you need to protect:so if the file is accessed from that particular file,
BASEPATHis set & everything works. But incase of some url directly tried to access, script would terminate.If you are calling it this from Javascript — other option is
to add
noncetoken to every javascript request which is basically a randomly generated unique token that is valid for single request. see Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet for more details.here is a example taken from here: