这篇文章主要介绍了ci检测是ajax还是页面post提交数据的方法,以PHP的环境变量为基础实例展示了针对CI框架配置文件的修改技巧,非常具有实用价值,需要的朋友可以参
本文实例讲述了ci检测是ajax还是页面post提交数据的方法。分享给大家供大家参考。具体实现方法如下:
一、问题:
因为项目需要我们希望知道提交数据来源是由ajax提交过来的数据还是由页面的post提交过来的数据从而进行不同层面上的处理。
二、解决方法:
在php中解决方法如下:
如果是ajax请求,以下表达式的值为真
复制代码 代码如下:
$_SERVER["HTTP_X_REQUESTED_WITH"]=="XMLHttpRequest"
就是一个PHP的环境变量。
ci中处理办法:
复制代码 代码如下:
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
define("IS_POST", strtolower($_SERVER['REQUEST_METHOD']) == 'post');
记得在用THINKPHP的时候有内置两个常量 IS_AJAX 与 IS_POST,想在ci中使用查了半天好像没有找到,那就自动动手丰衣足食
在项目的config/constants.php 配置文件加上上边两行代码,这下你就可以在所有的方法中直接调用
例如:
复制代码 代码如下:
if(IS_POST){
...
}
if(IS_AJAX){
...
}
希望本文所述对大家的CI框架程序设计有所帮助。
,
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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),