How to tell if a page is being called via Ajax, or on it's own

前端 未结 2 839
情话喂你
情话喂你 2021-01-11 23:52

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

Obviously these pages can all be called independently, so I want to detect

相关标签:
2条回答
  • 2021-01-12 00:13

    You cannot never trust clients and their sent information! The headers can be spoofed by hackers (for example with cURL) and even HTTP_X_REQUESTED_WITH is not reliable. there is no 100% trusted way to know that. The Only way is using captcha...

    0 讨论(0)
  • 2021-01-12 00:29

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

    X-Requested-With: XMLHttpRequest
    

    In PHP, check the existence of this header using:

    $_SERVER['HTTP_X_REQUESTED_WITH']
    
    0 讨论(0)
提交回复
热议问题