search
HomeBackend DevelopmentPHP Tutorial谁的头最牛逼?关于Php的头信息

头信息很重要,2个人,一个叫服务器(服务端),一个叫浏览器(客户端),即有2个头信息,请求头信息和返回头信息。

 

那么如果通过php获取这些头信息,服务端想获取浏览器请求时的头信息,可以用$_SERVER,代码如下:

<?phpfunction GetHeaderInfo(){  	foreach ($_SERVER as $name => $value){ 		$headers[$name] = $value;      }      return $headers;  }var_dump(GetHeaderInfo());?>

 输出:

array (size=32)  'HTTP_HOST' => string 'localhost' (length=9)  'HTTP_USER_AGENT' => string 'Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0' (length=65)  'HTTP_ACCEPT' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' (length=63)  'HTTP_ACCEPT_LANGUAGE' => string 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3' (length=35)  'HTTP_ACCEPT_ENCODING' => string 'gzip, deflate' (length=13)  'HTTP_COOKIE' => string 'bdshare_firstime=1365907820098' (length=30)  'HTTP_CONNECTION' => string 'keep-alive' (length=10)  'HTTP_CACHE_CONTROL' => string 'max-age=0' (length=9)  'PATH' => string 'C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;D:\svn\bin;D:\curl\curl-7.19.5;E:\mysql5.5\mysql\bin;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Ruby193\bin;C:\Program Files\SinoVoice\jTTS 5.0 Desktop\Bin;D:\php\xampp\php;C:\wamp\bin\php\php5.4.3\php.exe;' (length=266)  'SystemRoot' => string 'C:\WINDOWS' (length=10)  'COMSPEC' => string 'C:\WINDOWS\system32\cmd.exe' (length=27)  'PATHEXT' => string '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH' (length=48)  'WINDIR' => string 'C:\WINDOWS' (length=10)  'SERVER_SIGNATURE' => string '' (length=0)  'SERVER_SOFTWARE' => string 'Apache/2.2.22 (Win32) PHP/5.4.3' (length=31)  'SERVER_NAME' => string 'localhost' (length=9)  'SERVER_ADDR' => string '127.0.0.1' (length=9)  'SERVER_PORT' => string '80' (length=2)  'REMOTE_ADDR' => string '127.0.0.1' (length=9)  'DOCUMENT_ROOT' => string 'C:/wamp/www/' (length=12)  'SERVER_ADMIN' => string 'admin@localhost' (length=15)  'SCRIPT_FILENAME' => string 'C:/wamp/www/1.php' (length=17)  'REMOTE_PORT' => string '2483' (length=4)  'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)  'SERVER_PROTOCOL' => string 'HTTP/1.1' (length=8)  'REQUEST_METHOD' => string 'GET' (length=3)  'QUERY_STRING' => string '' (length=0)  'REQUEST_URI' => string '/1.php' (length=6)  'SCRIPT_NAME' => string '/1.php' (length=6)  'PHP_SELF' => string '/1.php' (length=6)  'REQUEST_TIME_FLOAT' => float 1366034246.187  'REQUEST_TIME' => int 1366034246

 可以看到不仅输出客户端的一些信息,也输出了服务端的一些信息。针对这些信息具体作用,这里不多介绍了,请查看http://www.cnblogs.com/IAmBetter/archive/2013/04/11/3014796.html

 

其实针对apache服务器可以使用现成的函数:getallheaders(),用这个函数的好处不多说了,直接看返回值吧

array (size=8)  'Host' => string 'localhost' (length=9)  'User-Agent' => string 'Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0' (length=65)  'Accept' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' (length=63)  'Accept-Language' => string 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3' (length=35)  'Accept-Encoding' => string 'gzip, deflate' (length=13)  'Cookie' => string 'bdshare_firstime=1365907820098' (length=30)  'Connection' => string 'keep-alive' (length=10)  'Cache-Control' => string 'max-age=0' (length=9)

 完全没有类似于HTTP_这样的参数,还需要去处理等问题。

 

知道如何获取请求过来的头信息了,那么也应该知道如何设置头信息,比如:

<?phpheader("name:anleb");?>

 

 

客户端获取到了服务端设置的头信息[name]

 

那么这个有什么用呢?其实有判断对方到底是什么请求?是同步请求呢,还是异步请求

 

2.html页面是请求页面

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><script>function CallAjax(){	var xmlHttp = new XMLHttpRequest();	xmlHttp.open("POST", '1.php', false);	xmlHttp.setRequestHeader("Is_Ajax", "yes");	xmlHttp.send("name=anleb");	result = xmlHttp.responseText;	alert("Result:" + result);}</script><body><input type='button' value='点击Ajax' onclick='CallAjax();'></body></html>

 返回页面

<?phpif(isset(getallheaders()['is_ajax']) && getallheaders()['is_ajax']=='yes'){	echo 'yes';}else{	echo 'no';}?>

 结果:

 

完毕,说的不好大家见谅。

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool