简体   繁体   English

如何使用PHP查找哪个文件发出了Ajax请求

[英]How to find what file made an ajax request with php

With php I want to know whether an ajax request came from ajax.js or index.php . 使用php我想知道ajax请求是来自ajax.js还是index.php The file that is being requested is ajax.php . 请求的文件是ajax.php I have tried using $_SERVER[HTTP_REFERER] to figure this out but instead of http://www.example.com/ajax.js , I am receiving http://www.example.com/index.php . 我尝试使用$_SERVER[HTTP_REFERER]来解决此问题,但我收到的不是http://www.example.com/ajax.js ,而是http://www.example.com/index.php

So how can I figure out what file made the ajax call using php using data that the client CAN NOT CHANGE ? 那么,如何确定使用客户端无法更改的数据使用php进行ajax调用的文件是什么?

You could add a custom header when executing your request using AJAX. 您可以在使用AJAX执行请求时添加自定义标头。 Then check if your custom header is present in get_headers() . 然后检查您的自定义标头是否存在于get_headers()中

If your custom header is present, the request come from your js file. 如果存在您的自定义标头,则请求来自您的js文件。 If not, it's a regular request. 如果没有,这是常规请求。

You can check how to add a custom header here . 您可以在此处检查如何添加自定义标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM