CENTRAL DO CLIENTE   
(usehost) Base de Conhecimentos

Home | Sumário | Favoritos | Contato | Efetuar Login Home | Sumário | Favoritos | Contato | Efetuar Login
Pesquisar na base de dados Pesquisar por categoria
Detectando Ajax Request com PHP
Detalhes do artigo

Última atualização
11th o June, 2010

Opções de usuário (6 votos)
100% thumbs up 0% thumbs down

Como você avaliaria este artigo?
Útil
Inútil
Here’s a quick piece of code that i find useful to check if a request that comes to a PHP page was made via an Ajax call or a simple form post. This method uses the $_SERVER['HTTP_X_REQUESTED_WITH'] request to determine if data was sent to a specific page using an xmlhttprequest. It’s worth bearing in mind that there is no guarantee that every web server will provide this setting, servers may omit specific $_SERVER parameters, That said, a large number of these variables are accounted for, you can find more information about $_SERVER variables here.

The PHP


if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
// If its an ajax request execute the code below
echo 'This is an ajax request!';
exit;
}
//if it's not an ajax request echo the below.
echo 'This is clearly not an ajax request!';

The code is pretty self explanatory we are literally checking to see if the request was sent via an xmlhttprequest. In the Demo below i’ve setup a page that contains a jQuery Ajax request to our code above and a simple form with a button that just submits to the same page.

demo



Comentários
Não hpa comentários de visitantes. Comentar
Postou comentário para o artigo "Detectando Ajax Request com PHP"
Para postar um comentário para este artigo, preencha o formulário a seguir. campos marcados com asterisco são obrigatórios.
   Seu nome:
   Endereço de e-mail:
* Comentário:
* Digite o código a seguir::
 
Artigos relacionados
Não foi encontrado artigos relacionados.
Anexos
Nenhum anexo.

Continuar

Pagamento de Faturas
2ª Via do Boleto Bancário
Digite o N° da Cobrança:

 

Se seu pagamento não for por
boleto bancário, acesse a nossa
Central do Cliente. E proceda o pagamento.

UMA EMPRESA DO GRUPO INTECORP TECNOLOGIA®
(C) Copyright 2004-2010 (usehost) Serviços de Internet do Brasil.
Compartilhe: