PHP超全局变量实现原理及代码解析

zhouyuqi 2020-09-15

è¶å¨å±åéå¨ PHP 4.1.0 中å¼å¥ï¼æ¯å¨å¨é¨ä½ç¨å中å§ç»å¯ç¨çåç½®åéã

PHP è¶å¨å±åé

PHP 中ç许å¤é¢å®ä¹åéé½æ¯âè¶å¨å±çâï¼è¿æå³çå®ä»¬å¨ä¸ä¸ªèæ¬çå¨é¨ä½ç¨å中é½å¯ç¨ãå¨å½æ°ææ¹æ³ä¸­æ éæ§è¡ global $variable; å°±å¯ä»¥è®¿é®å®ä»¬ã

è¿äºè¶å¨å±åéæ¯ï¼

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

æ¬èä¼ä»ç»ä¸äºè¶å¨å±åéï¼å¹¶ä¼å¨ç¨åçç« è讲解å¶ä»çè¶å¨å±åéã

$GLOBALS â å¼ç¨å¨å±ä½ç¨å中å¯ç¨çå¨é¨åé

$GLOBALS è¿ç§å¨å±åéç¨äºå¨ PHP èæ¬ä¸­çä»»æä½ç½®è®¿é®å¨å±åéï¼ä»å½æ°ææ¹æ³ä¸­åå¯ï¼ã

PHP å¨å为 $GLOBALS[index] çæ°ç»ä¸­å­å¨äºææå¨å±åéãåéçåå­å°±æ¯æ°ç»çé®ã

ä¸é¢çä¾å­å±ç¤ºäºå¦ä½ä½¿ç¨è¶çº§å¨å±åé $GLOBALSï¼

å®ä¾

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

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

PHP $_SERVER

$_SERVER è¿ç§è¶å¨å±åéä¿å­å³äºæ¥å¤´ãè·¯å¾åèæ¬ä½ç½®çä¿¡æ¯ã

ä¸é¢çä¾å­å±ç¤ºäºå¦ä½ä½¿ç¨ $_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'] è¿åå½åæ§è¡èæ¬çæ件åã
$_SERVER['GATEWAY_INTERFACE'] è¿åæå¡å¨ä½¿ç¨ç CGI è§èççæ¬ã
$_SERVER['SERVER_ADDR'] è¿åå½åè¿è¡èæ¬æå¨çæå¡å¨ç IP å°åã
$_SERVER['SERVER_NAME'] è¿åå½åè¿è¡èæ¬æå¨çæå¡å¨ç主æºåï¼æ¯å¦ www.w3school.com.cnï¼ã
$_SERVER['SERVER_SOFTWARE'] è¿åæå¡å¨æ è¯å­ç¬¦ä¸²ï¼æ¯å¦ Apache/2.2.24ï¼ã
$_SERVER['SERVER_PROTOCOL'] è¿å请æ±é¡µé¢æ¶éä¿¡åè®®çå称åçæ¬ï¼ä¾å¦ï¼âHTTP/1.0âï¼ã
$_SERVER['REQUEST_METHOD'] è¿å访é®é¡µé¢ä½¿ç¨ç请æ±æ¹æ³ï¼ä¾å¦ POSTï¼ã
$_SERVER['REQUEST_TIME'] è¿å请æ±å¼å§æ¶çæ¶é´æ³ï¼ä¾å¦ 1577687494ï¼ã
$_SERVER['QUERY_STRING'] è¿åæ¥è¯¢å­ç¬¦ä¸²ï¼å¦ææ¯éè¿æ¥è¯¢å­ç¬¦ä¸²è®¿é®æ­¤é¡µé¢ã
$_SERVER['HTTP_ACCEPT'] è¿åæ¥èªå½å请æ±ç请æ±å¤´ã
$_SERVER['HTTP_ACCEPT_CHARSET'] è¿åæ¥èªå½å请æ±ç Accept_Charset å¤´ï¼ ä¾å¦ utf-8,ISO-8859-1ï¼
$_SERVER['HTTP_HOST'] è¿åæ¥èªå½å请æ±ç Host 头ã
$_SERVER['HTTP_REFERER'] è¿åå½å页é¢çå®æ´ URLï¼ä¸å¯é ï¼å ä¸ºä¸æ¯ææç¨æ·ä»£çé½æ¯æï¼ã
$_SERVER['HTTPS'] æ¯å¦éè¿å®å¨ HTTP åè®®æ¥è¯¢èæ¬ã
$_SERVER['REMOTE_ADDR'] è¿åæµè§å½å页é¢çç¨æ·ç IP å°åã
$_SERVER['REMOTE_HOST'] è¿åæµè§å½å页é¢çç¨æ·ç主æºåã
$_SERVER['REMOTE_PORT'] è¿åç¨æ·æºå¨ä¸è¿æ¥å° Web æå¡å¨æ使ç¨ç端å£å·ã
$_SERVER['SCRIPT_FILENAME'] è¿åå½åæ§è¡èæ¬çç»å¯¹è·¯å¾ã
$_SERVER['SERVER_ADMIN'] 该å¼ææäº Apache æå¡å¨éç½®æ件中ç SERVER_ADMIN åæ°ã
$_SERVER['SERVER_PORT'] Web æå¡å¨ä½¿ç¨ç端å£ãé»è®¤å¼ä¸º â80âã
$_SERVER['SERVER_SIGNATURE'] è¿åæå¡å¨çæ¬åèæ主æºåã
$_SERVER['PATH_TRANSLATED'] å½åèæ¬æå¨æ件系ç»ï¼éæ档根ç®å½ï¼çåºæ¬è·¯å¾ã
$_SERVER['SCRIPT_NAME'] è¿åå½åèæ¬çè·¯å¾ã
$_SERVER['SCRIPT_URI'] è¿åå½å页é¢ç URIã

PHP $_REQUEST

PHP $_REQUEST ç¨äºæ¶é HTML 表åæ交çæ°æ®ã

ä¸é¢çä¾å­å±ç¤ºäºä¸ä¸ªåå«è¾å¥å­æ®µåæ交æé®ç表åãå½ç¨æ·éè¿ç¹å»æ交æé®æ¥æ交表åæ°æ®æ¶, 表åæ°æ®å°åéå° æ ç­¾ç 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 $_POST

PHP $_POST 广æ³ç¨äºæ¶éæ交 method="post" ç HTML 表ååç表åæ°æ®ã$_POST ä¹å¸¸ç¨äºä¼ éåéã

ä¸é¢çä¾å­å±ç¤ºäºä¸ä¸ªåå«è¾å¥å­æ®µåæ交æé®ç表åãå½ç¨æ·ç¹å»æ交æé®æ¥æ交æ°æ®åï¼è¡¨åæ°æ®ä¼åéå° æ ç­¾ç action å±æ§ä¸­æå®çæ件ãå¨æ¬ä¾ä¸­ï¼æ们æå®æ件æ¬èº«æ¥å¤ç表åæ°æ®ãå¦ææ¨å¸æ使ç¨å¦ä¸ä¸ª PHP 页é¢æ¥å¤ç表åæ°æ®ï¼è¯·ç¨æ´æ¹ä¸ºæ¨éæ©çæ件åãç¶åï¼æ们å¯ä»¥ä½¿ç¨è¶å¨å±åé $_POST æ¥æ¶éè¾å¥å­æ®µçå¼ï¼

å®ä¾

<html>
<body>

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

<?php 
$name = $_POST['fname'];
echo $name; 
?>

</body>
</html>

PHP $_GET

PHP $_GET ä¹å¯ç¨äºæ¶éæ交 HTML 表å (method="get") ä¹åç表åæ°æ®ã

$_GET ä¹å¯ä»¥æ¶é URL 中çåéçæ°æ®ã

å设æ们æä¸å¼ é¡µé¢å«æ带åæ°çè¶é¾æ¥ï¼

<html>
<body>

<a href="test_get.php?subject=PHP&web=W3school.com.cn" rel="external nofollow" >æµè¯ $GET</a>

</body>
</html>

å½ç¨æ·ç¹å»é¾æ¥ "æµè¯ $GET"ï¼åæ° "subject" å "web" 被åéå° "test_get.php"ï¼ç¶åæ¨å°±è½å¤éè¿ $_GET å¨ "test_get.php" 中访é®è¿äºå¼äºã

ä¸é¢çä¾å­æ¯ "test_get.php" 中ç代ç ï¼

å®ä¾

<html>
<body>

<?php 
echo "å¨ " . $_GET['web'] . " 学习 " . $_GET['subject'];
?>

</body>
</html>

以ä¸å°±æ¯æ¬æçå¨é¨å容ï¼å¸æ对大家ç学习ææ帮å©ï¼ä¹å¸æ大家å¤å¤æ¯æèæ¬ä¹å®¶ã

相关推荐