在php中判断一个请求是ajax请求还是普通请求的方法_PHP
Ajax
/path/to/pkphp.com/script.php?ajax在php脚本中使用如下方法判断:
复制代码 代码如下:
if(isset($_GET['ajax'])) {
...这是一个ajax请求,然后...
}
else {
...这不是一个ajax请求,然后...
}
通过传递_GET参数的方法简单实现了网页请求的判断。但是如果需要这样的功能,这个方法可能就有弊端,功能需求如下:
1.通过ajax请求的网页与普通请求的网页内容是不相同的
2.通过ajax请求的网页是为了方便用户操作,两种方法请求打开的网页必须的内容是相同的,只是ajax请求到的网页内容比较简化和使用,去除了网页的大框架模板。
3.这么做的目的是:用户在网页操作时通过ajax实现,而搜索引擎访问网页时(相当于普通打开网页),得到的内容是一个完整的网页(包含了网页的大框架模板)。
要完成上面的这个功能,就不能使用前面介绍的通过GET参数传递来判断了,如果使用GET传递来判断的话,用户ajax请求和普通网页请求都会是一样的内容,因为你不可能为一个链接设置一个带ajax判断参数和不带的URL。那么如何才能实现这个功能呢?必须通过服务器端PHP判断解决这个问题。也就是今天要说的PHP如何判断ajax请求。这个问题要解决有一个先决条件,那就是你使用的ajax框架必须是jquery。在jquery框架中,对于通过它的$.ajax, $.get, or $.post方法请求网页内容时,它会向服务器传递一个HTTP_X_REQUESTED_WITH的参数,你可以利用如下方法判断某个请求是ajax请求还是普通请求:
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
..这是一个ajax请求,然后...
}
else {
..这不是一个ajax请求,然后...
}
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH
{
...这是一个ajax请求,然后...
}
else {
...这不是一个ajax请求,然后...
}
利用这个来进行判断操作,可以使网页端的URL保持一致,但是能够对两种不同的请求却能够得到不同内容的网页。即实现了用户操作优化,又不影响搜索引擎收录,我觉得是一个很棒的解决方案!
这里有一个另外需要注意的问题,就是如果你的jquery请求是通过iframe打开网页的,那么HTTP_X_REQUESTED_WITH参数不会被传递,也就是说你没有办法判断请求的类型。
主要内容是:
1.
这个问题要解决有一个先决条件,那就是你使用的ajax框架必须是jquery。在jquery框架中,对于通过它的$.ajax, $.get, or $.post方法请求网页内容时,它会向服务器传递一个HTTP_X_REQUESTED_WITH的参数,你可以利用如下方法判断某个请求是ajax请求还是普通请求:
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
2.
这里有一个另外需要注意的问题,就是如果你的jquery请求是通过iframe打开网页的,那么HTTP_X_REQUESTED_WITH参数不会被传递,也就是说你没有办法判断请求的类型。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to extend the expiration time of Ajax requests? When making network requests, we often encounter situations where we need to process large amounts of data or complex calculations, which may cause the request to time out and fail to return data normally. In order to solve this problem, we can ensure that the request can be completed successfully by extending the expiration time of the Ajax request. The following will introduce some methods and specific code examples to extend the expiration time of Ajax requests. When making an Ajax request using the timeout attribute, you can set the timeout attribute to

AJAX requests have no fixed expiration time: "Asynchronous JavaScript and XML" is a technology for sending asynchronous requests on web pages, which uses JavaScript to send requests to the server and receive responses without refreshing the entire page.

In the Yii framework, controllers play an important role in processing requests. In addition to handling regular page requests, controllers can also be used to handle Ajax requests. This article will introduce how to handle Ajax requests in the Yii framework and provide code examples. In the Yii framework, processing Ajax requests can be carried out through the following steps: The first step is to create a controller (Controller) class. You can inherit the basic controller class yiiwebCo provided by the Yii framework

Practical guide: Which Ajax request libraries are suitable for your project? With the continuous development of front-end development, Ajax has become an indispensable part of web development. Choosing an Ajax request library suitable for the project is crucial to improving development efficiency and optimizing user experience. This article will introduce several commonly used Ajax request libraries to help readers choose the tool suitable for their own projects. jQueryAjax There is no denying that jQuery is one of the most popular JavaScript libraries out there. It provides a rich

Development essentials: Explore what are the commonly used Ajax request libraries? In modern front-end development, using Ajax for asynchronous requests has become a standard feature, and choosing an appropriate Ajax request library can allow us to process network requests more efficiently, improving development efficiency and user experience. This article will explore some commonly used Ajax request libraries to help developers choose the tools suitable for their projects. jQueryAjax: As one of the most popular JavaScript libraries, jQuery provides powerful Ajax request functions.

Can the expiration time of Ajax requests be customized? In web development, we often use Ajax to implement asynchronous requests to dynamically load data in the page. When making Ajax requests, sometimes we need to control the timeout of the request, that is, set a time limit, and process it if no response is received within the specified time. So, can the expiration time of Ajax requests be customized? This article will introduce this problem in detail and provide specific code examples. Using jQuery's Ajax function

Under what circumstances will an Ajax request expire? With the development of Web applications, Ajax (Asynchronous JavaScript and XML) technology has become an essential part of Web development. Through Ajax, we can obtain data from the server and dynamically update the content of the web page without refreshing the entire page. However, when using Ajax to send requests, sometimes you encounter request expiration. So, under what circumstances will an Ajax request expire?

How to set the expiration time of Ajax request? Need specific code examples With the development of Internet applications, Ajax has become an indispensable part of Web development. When sending an Ajax request, sometimes we need to limit the expiration time of the request to prevent the request from being too long, resulting in poor user experience or browser freezes. This article will introduce in detail how to set the expiration time of Ajax requests and give specific code examples. Setting the expiration time of Ajax requests mainly requires XMLHttpRequest
