Prevent direct access to a PHP page

前端 未结 10 1992
说谎
说谎 2020-12-01 09:34

How do I prevent my users from accessing directly pages meant for ajax calls only?

Passing a key during ajax call seems like a solution, whereas access without the k

10条回答
  •  被撕碎了的回忆
    2020-12-01 10:13

    As others have said, Ajax request can be emulated be creating the proper headers. If you want to have a basic check to see if the request is an Ajax request you can use:

     if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
         //Request identified as ajax request
     }
    

    However you should never base your security on this check. It will eliminate direct accesses to the page if that is what you need.

提交回复
热议问题