PHP超级全局变量【$GLOBALS,$_SERVER,$_REQUEST等】用法实例分析

æ¬æå®ä¾è®²è¿°äºPHPè¶çº§å¨å±åéç¨æ³ãå享ç»å¤§å®¶ä¾å¤§å®¶åèï¼å·ä½å¦ä¸ï¼

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION

å±ä»¬è¿æ¬¡å°±æ¥èè$GLOBALS&$_SERVER&$_REQUESTè¿ä¸ä¸ªè¶çº§å¨å±åéãé¦åæ¯$GLOBALSï¼å®æ¯PHPçä¸ä¸ªè¶çº§å¨å±åéç»ï¼å¨ä¸ä¸ªPHPèæ¬çå¨é¨ä½ç¨å中é½å¯ä»¥è®¿é®ï¼å®è¿æ¯PHPçä¸ä¸ªè¶çº§å¨å±åéç»ï¼å¨ä¸ä¸ªPHPèæ¬çå¨é¨ä½ç¨å中é½å¯ä»¥è®¿é®ãå·ä½ç¨æ³å¦ä¸ï¼

<?php 
$x = 75; 
$y = 25;
function addition() 
{
 $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; 
}
addition();
echo $z;
?>

å¨ä¸é¢çå®ä¾ä¸­ï¼ z æ¯ä¸ä¸ª$GLOBALSæ°ç»ä¸­çè¶çº§å¨å±åéï¼è¯¥åéåæ ·å¯ä»¥å¨å½æ°å¤è®¿é®ã

æ¥ä¸æ¥çä¸$_SERVERï¼å®æ¯ä¸ä¸ªåå«äºè¯¸å¦å¤´ä¿¡æ¯(header)ãè·¯å¾(path)ã以åèæ¬ä½ç½®(script locations)ç­ç­ä¿¡æ¯çæ°ç»ãè¿ä¸ªæ°ç»ä¸­ç项ç®ç± Web æå¡å¨å建ãä¸è½ä¿è¯æ¯ä¸ªæå¡å¨é½æä¾å¨é¨é¡¹ç®ï¼æå¡å¨å¯è½ä¼å¿½ç¥ä¸äºï¼æèæä¾ä¸äºæ²¡æå¨è¿éå举åºæ¥ç项ç®ã使ç¨$_SERVER中çåç´ çå®ä¾å¦ä¸ï¼

<?php 
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>

å±ä»¬æ¥çä¸ææ $_SERVER åé中çéè¦åç´ ï¼

åç´ /ä»£ç  æè¿°
$_SERVER['PHP_SELF'] å½åæ§è¡èæ¬çæ件åï¼ä¸ document root æå³ãä¾å¦ï¼å¨å°å为 http://example.com/test.php/foo.bar çèæ¬ä¸­ä½¿ç¨ $_SERVER['PHP_SELF'] å°å¾å° /test.php/foo.barã__FILE__ 常éåå«å½å(ä¾å¦åå«)æ件çå®æ´è·¯å¾åæ件åã ä» PHP 4.3.0 çæ¬å¼å§ï¼å¦æ PHP 以å½ä»¤è¡æ¨¡å¼è¿è¡ï¼è¿ä¸ªåéå°åå«èæ¬åãä¹åççæ¬è¯¥åéä¸å¯ç¨ã
$_SERVER['GATEWAY_INTERFACE'] æå¡å¨ä½¿ç¨ç CGI è§èççæ¬ï¼ä¾å¦ï¼"CGI/1.1"ã
$_SERVER['SERVER_ADDR'] å½åè¿è¡èæ¬æå¨çæå¡å¨ç IP å°åã
$_SERVER['SERVER_NAME'] å½åè¿è¡èæ¬æå¨çæå¡å¨ç主æºåãå¦æèæ¬è¿è¡äºèæ主æºä¸­ï¼è¯¥å称æ¯ç±é£ä¸ªèæ主æºæ设置çå¼å³å®ã(å¦: www.runoob.com)
$_SERVER['SERVER_SOFTWARE'] æå¡å¨æ è¯å­ç¬¦ä¸²ï¼å¨ååºè¯·æ±æ¶ç头信æ¯ä¸­ç»åºã (å¦ï¼Apache/2.2.24)
$_SERVER['SERVER_PROTOCOL'] 请æ±é¡µé¢æ¶éä¿¡åè®®çå称åçæ¬ãä¾å¦ï¼"HTTP/1.0"ã
$_SERVER['REQUEST_METHOD'] 访é®é¡µé¢ä½¿ç¨ç请æ±æ¹æ³ï¼ä¾å¦ï¼"GET", "HEAD"ï¼"POST"ï¼"PUT"ã
$_SERVER['REQUEST_TIME'] 请æ±å¼å§æ¶çæ¶é´æ³ãä» PHP 5.1.0 èµ·å¯ç¨ã (å¦ï¼1377687496)
$_SERVER['QUERY_STRING'] query stringï¼æ¥è¯¢å­ç¬¦ä¸²ï¼ï¼å¦ææçè¯ï¼éè¿å®è¿è¡é¡µé¢è®¿é®ã
$_SERVER['HTTP_ACCEPT'] å½å请æ±å¤´ä¸­ Accept: 项çå容ï¼å¦æå­å¨çè¯ã
$_SERVER['HTTP_ACCEPT_CHARSET'] å½å请æ±å¤´ä¸­ Accept-Charset: 项çå容ï¼å¦æå­å¨çè¯ãä¾å¦ï¼"iso-8859-1,*,utf-8"ã
$_SERVER['HTTP_HOST'] å½å请æ±å¤´ä¸­ Host: 项çå容ï¼å¦æå­å¨çè¯ã
$_SERVER['HTTP_REFERER'] å¼å¯¼ç¨æ·ä»£çå°å½å页çåä¸é¡µçå°åï¼å¦æå­å¨ï¼ãç± user agent 设置å³å®ã并ä¸æ¯ææçç¨æ·ä»£çé½ä¼è®¾ç½®è¯¥é¡¹ï¼æçè¿æä¾äºä¿®æ¹ HTTP_REFERER çåè½ãç®è¨ä¹ï¼è¯¥å¼å¹¶ä¸å¯ä¿¡ã)
$_SERVER['HTTPS'] å¦æèæ¬æ¯éè¿ HTTPS å议被访é®ï¼å被设为ä¸ä¸ªé空çå¼ã
$_SERVER['REMOTE_ADDR'] æµè§å½å页é¢çç¨æ·ç IP å°åã
$_SERVER['REMOTE_HOST'] æµè§å½å页é¢çç¨æ·ç主æºåãDNS åå解æä¸ä¾èµäºç¨æ·ç REMOTE_ADDRã
$_SERVER['REMOTE_PORT'] ç¨æ·æºå¨ä¸è¿æ¥å° Web æå¡å¨æ使ç¨ç端å£å·ã
$_SERVER['SCRIPT_FILENAME'] å½åæ§è¡èæ¬çç»å¯¹è·¯å¾ã
$_SERVER['SERVER_ADMIN'] 该å¼ææäº Apache æå¡å¨éç½®æ件中ç SERVER_ADMIN åæ°ãå¦æèæ¬è¿è¡å¨ä¸ä¸ªèæ主æºä¸ï¼å该å¼æ¯é£ä¸ªèæ主æºçå¼ã(å¦ï¼[email protected])
$_SERVER['SERVER_PORT'] Web æå¡å¨ä½¿ç¨ç端å£ãé»è®¤å¼ä¸º "80"ãå¦æä½¿ç¨ SSL å®å¨è¿æ¥ï¼åè¿ä¸ªå¼ä¸ºç¨æ·è®¾ç½®ç HTTP 端å£ã
$_SERVER['SERVER_SIGNATURE'] åå«äºæå¡å¨çæ¬åèæ主æºåçå­ç¬¦ä¸²ã
$_SERVER['PATH_TRANSLATED'] å½åèæ¬æå¨æ件系ç»ï¼éæ档根ç®å½ï¼çåºæ¬è·¯å¾ãè¿æ¯å¨æå¡å¨è¿è¡èæå°çå®è·¯å¾çæ ååçç»æã
$_SERVER['SCRIPT_NAME'] åå«å½åèæ¬çè·¯å¾ãè¿å¨é¡µé¢éè¦æåèªå·±æ¶é常æç¨ã__FILE__ 常éåå«å½åèæ¬(ä¾å¦åå«æ件)çå®æ´è·¯å¾åæ件åã
$_SERVER['SCRIPT_URI'] URI ç¨æ¥æå®è¦è®¿é®ç页é¢ãä¾å¦ "/index.html"ã

æåæ¥çä¸$_REQUESTï¼å®ä¸»è¦ç¨äºæ¶éHTML表åæ交çæ°æ®ã以ä¸å®ä¾æ¾ç¤ºäºä¸ä¸ªè¾å¥å­æ®µï¼inputï¼åæ交æé®(submit)ç表å(form)ã å½ç¨æ·éè¿ç¹å» "Submit" æé®æ交表åæ°æ®æ¶, 表åæ°æ®å°åéè³<form>æ ç­¾ä¸­ action å±æ§ä¸­æå®çèæ¬æ件ã å¨è¿ä¸ªå®ä¾ä¸­ï¼æ们æå®æ件æ¥å¤ç表åæ°æ®ãå¦ææ们å¸æå¶ä»çPHPæ件æ¥å¤ç该æ°æ®ï¼æ们就å¯ä»¥ä¿®æ¹è¯¥æå®çèæ¬æ件åã ç¶åï¼æ们å¯ä»¥ä½¿ç¨è¶çº§å¨å±åé $_REQUEST æ¥æ¶é表å中ç input å­æ®µæ°æ®ï¼å·ä½ä»£ç å¦ä¸ï¼

<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
$name = $_REQUEST['fname'];
echo $name;
?>
</body>
</html>

好å¦ï¼æ¬æ¬¡è®°å½å°±å°è¿éäºã

æ´å¤å³äºPHPç¸å³å容æå´è¶£ç读èå¯æ¥çæ¬ç«ä¸é¢ï¼ãphp常ç¨å½æ°ä¸æå·§æ»ç»ãããphpå­ç¬¦ä¸²(string)ç¨æ³æ»ç»ãããPHPæ°æ®ç»æä¸ç®æ³æç¨ãããphpç¨åºè®¾è®¡ç®æ³æ»ç»ãåãPHPæ°ç»(Array)æä½æ巧大å¨ã

å¸ææ¬ææ述对大家PHPç¨åºè®¾è®¡ææ帮å©ã

php

相关推荐