[摘要]PHP °²È« £¨1£© Ô­×...
PHP °²È« £¨1£©
Ô×÷£ºJohn Coggeshall 07/31/2003
ÔÎÄ£ºhttp://www.onlamp.com/pub/a/php/2003/07/31/php_foundations.html
ÔÚ×î½üµÄÁ½Æª×¨À¸ÎÄÕ£¨¡¶³£¼ûÑùʽ´íÎó£¨ÉÏ£©¡·ºÍ¡¶³£¼ûÑùʽ´íÎó£¨Ï£©¡·£©ÖÐÎÒÌÖÂÛÁËһЩÔÚ±àдPHP³Ìʽʱ³£¼ûµÄʹ³ÌʽÄÑÓÚÔĶÁºÍÒ×µ¼ÖÂBUGµÄ»µÏ°¹ß¡£
ÔÚ½ñÌìµÄÎÄÕÂÖУ¬ÎÒ½«×ªÏòÌÖÂÛÕâ¸öϵÁеÄÖص㣺ʹÓÃPHPʱ£¬°²È«ÎÊÌâµÄÖØÒªÐÔ¡£
¹Ø×¢°²È«ÎÊÌâµÄÖØÒªÐÔ
¿´µ½µÄÔ¶·ÇÈ«²¿
×èÖ¹Óû§¶ñÒâÆÆ»µÄãµÄ³Ìʽ×îÓÐЧȴ¾³£±»ºöÂԵķ½·¨ÊÇÔÚд´úÂëʱ¾Í¿¼ÂÇËüµÄ¿ÉÄÜÐÔ¡£ÁôÒâ´úÂëÖпÉÄܵݲȫÎÊÌâÊǺÜÖØÒªµÄ¡£¿¼ÂÇϱߵÄÖ¼ÔÚ¼ò»¯ÓÃPHPÖÐдÈë´óÁ¿Îı¾ÎļþµÄ¹ý³ÌµÄʵÀýº¯Êý£º
<?php
function write_text($filename, $text="") {
static $open_files = array();
// Èç¹ûÎļþÃû¿Õ£¬¹Ø±ÕÈ«²¿Îļþ
if ($filename == NULL) {
foreach($open_files as $fr) {
fclose($fr);
}
return true;
}
$index = md5($filename);
if(!isset($open_files[$index])) {
$open_files[$index] = fopen($filename, "a+");
if(!$open_files[$index]) return false;
}
fputs($open_files[$index], $text);
return true;
}?>
Õâ¸öº¯Êý´øÓÐÁ½¸öȱʡ²ÎÊý£¬ÎļþÃûºÍҪдÈëÎļþµÄÎı¾¡£
º¯Êý½«Ïȼì²éÎļþÊÇ·ñÒѱ»´ò¿ª£»Èç¹ûÊÇ£¬½«Ê¹ÓÃÔÀ´µÄÎļþ¾ä±ú¡£·ñÔò£¬½«×ÔÐд´½¨¡£ÔÚÕâÁ½ÖÖÇé¿öÖУ¬Îı¾¶¼»á±»Ð´ÈëÎļþ¡£
Èç¹û´«µÝ¸øº¯ÊýµÄÎļþÃûÊÇNULL£¬ÄÇôËùÓдò¿ªµÄÎļþ½«±»¹Ø±Õ¡£Ï±ßÌṩÁËÒ»¸öʹÓÃÉϵÄʵÀý¡£
Èç¹û¿ª·¢ÕßÒÔϱߵĸñʽÀ´Ð´Èë¶à¸öÎı¾Îļþ£¬ÄÇôÕâ¸öº¯Êý½«Çå³þºÍÒ׶ÁµÄ¶à¡£
ÈÃÎÒÃǼٶ¨Õâ¸öº¯Êý´æÔÚÓÚÒ»¸öµ¥¶ÀµÄÎļþÖУ¬Õâ¸öÎļþ°üº¬Á˵÷ÓÃÕâ¸öº¯ÊýµÄ´úÂë¡£
ϱßÊÇÒ»¸öÕâÑùµÄ³Ìʽ£¬ÎÒÃǽÐËüquotes.php£º
<html><body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="get">
Choose the nature of the quote:
<select name="quote" size="3">
<option value="funny">Humorous quotes</option>
<option value="political">Political quotes</option>
<option value="love">Romantic Quotes</option>
</select><br />
The quote: <input type="text" name="quote_text" size="30" />
<input type="submit" value="Save Quote" />
</form>
</body></html>
<?php
include_once('write_text.php');
$filename = "/home/web/quotes/{$_GET['quote']}";
$quote_msg = $_GET['quote_text'];
if (write_text($filename, $quote_msg)) {
echo "<center><hr><h2>Quote saved!</h2></center>";
} else {
echo "<center><hr><h2>Error writing quote</h2></center>";
}
write_text(NULL);
?>
ÈçͬÄã¿´µ½µÄ£¬Õâλ¿ª·¢ÕßʹÓÃÁËwrite_text()º¯ÊýÀ´´´½¨Ò»¸öÌåϵʹµÃÓû§¿ÉÒÔÌá½»ËûÃÇϲ»¶µÄ¸ñÑÔ£¬ÕâЩ¸ñÑÔ½«±»´æ·ÅÔÚÒ»¸öÎı¾ÎļþÖС£
²»ÐÒµÄÊÇ£¬¿ª·¢Õß¿ÉÄÜûÓÐÏëµ½£¬Õâ¸ö³ÌʽҲÔÊÐíÁ˶ñÒâÓû§Î£º¦web serverµÄ°²È«¡£
Ò²ÐíÏÖÔÚÄãÕýÄÓ×ÅÍ·Ïëמ¿¾¹Õâ¸ö¿´ÆðÀ´ºÜÎÞ¹¼µÄ³ÌʽÔõÑùÒýÈëÁË°²È«·çÏÕ¡£
Èç¹ûÄã¿´²»³öÀ´£¬¿¼ÂÇϱßÕâ¸öURL£¬¼ÇסÕâ¸ö³Ìʽ½Ð×öquotes.php£º
http://www.somewhere.com/fun/quotes.php?quote=different_file.dat"e_text=garbage+data
µ±Õâ¸öURL´«µÝ¸øweb server ʱ½«»á·¢Éúʲô£¿
ÏÔÈ»£¬quotes.php½«±»Ö´ÐУ¬µ«ÊÇ£¬²»Êǽ«Ò»¾ä¸ñÑÔдÈëµ½ÎÒÃÇÏ£ÍûµÄÈý¸öÎļþÖÐÖ®Ò»£¬Ïà·´µÄ£¬Ò»¸ö½Ð×ödifferent_file.datµÄÐÂÎļþ½«±»½¨Á¢£¬ÆäÖаüº¬Ò»¸ö×Ö·û´®garbage data¡£
ÏÔÈ»£¬Õâ²»ÊÇÎÒÃÇÏ£ÍûµÄÐÐΪ£¬¶ñÒâÓû§¿ÉÄÜͨ¹ý°ÑquoteÖ¸¶¨Îª../../../etc/passwdÀ´·ÃÎÊUNIXÃÜÂëÎļþ´Ó¶ø´´½¨Ò»¸öÕʺţ¨¾¡¹ÜÕâÐèÒªweb serverÒÔsuperuserÔËÐгÌʽ£¬Èç¹ûÊÇÕâÑùµÄ£¬ÄãÓ¦¸ÃÍ£Ö¹ÔĶÁ£¬ÂíÉÏÈ¥ÐÞ¸´Ëü£©¡£
Èç¹û/home/web/quotes/¿ÉÒÔͨ¹ýä¯ÀÀÆ÷·ÃÎÊ£¬¿ÉÄÜÕâ¸ö³Ìʽ×îÑÏÖصݲȫÎÊÌâÊÇËüÔÊÐíÈκÎÓû§Ð´ÈëºÍÔËÐÐÈÎÒâPHP³Ìʽ¡£Õ⽫´øÀ´ÎÞÇîµÄÂé·³¡£
ÕâÀïÓÐһЩ½â¾ö·½°¸¡£Èç¹ûÄãÖ»ÐèҪдÈëĿ¼ÏµÄһЩÎļþ£¬¿ÉÒÔ¿¼ÂÇʹÓÃÒ»¸öÏà¹ØµÄÊý×éÀ´´æ·ÅÎļþÃû¡£Èç¹ûÓû§ÊäÈëµÄÎļþ´æÔÚÓÚÕâ¸öÊý×éÖУ¬¾Í¿ÉÒÔ°²È«µÄдÈë¡£ÁíÒ»¸öÏë·¨ÊÇÈ¥µôËùÓеIJ»ÊÇÊý×ÖºÍ×ÖĸµÄ×Ö·ûÀ´È·±£Ã»ÓÐĿ¼·Ö¸î·ûºÅ¡£»¹ÓÐÒ»¸ö°ì·¨ÊǼì²éÎļþµÄÀ©Õ¹ÃûÀ´±£Ö¤Îļþ²»»á±»web serverÖ´ÐС£
ÔÔòºÜ¼òµ¥£¬×÷Ϊһ¸ö¿ª·¢ÕßÄã±ØÐë±È³ÌʽÔÚÄãÏ£ÍûµÄÇé¿öÏÂÔËÐÐʱ¿¼ÂǸü¶à¡£
Èç¹û·Ç·¨Êý¾Ý½øÈëµ½Ò»¸öformÔªËØÖлᷢÉúʲô£¿¶ñÒâÓû§ÊÇ·ñÄÜʹÄãµÄ³ÌʽÒÔ²»Ï£ÍûµÄ·½Ê½ÔËÐУ¿Ê²Ã´·½·¨ÄÜ×èÖ¹ÕâЩ¹¥»÷£¿ÄãµÄweb serverºÍPHP³ÌʽֻÓÐÔÚ×îÈõµÄ°²È«Á´½ÓÏ²Ű²È«£¬ËùÒÔÈ·ÈÏÕâЩ¿ÉÄܲ»°²È«µÄÁ´½ÓÊÇ·ñ°²È«ºÜÖØÒª¡£
³£¼ûµÄÉæ¼°°²È«µÄ´íÎó
ÕâÀï¸ø³öһЩҪµã£¬Ò»¸ö¿ÉÄÜΣ¼°°²È«µÄ±àÂëÉϵĺ͹ÜÀíÉϵÄʧÎóµÄ¼òÒª²»ÍêÕûÁбí
´íÎó1¡£ÐÅÀµÊý¾Ý
ÕâÊǹᴩÓÚÎÒ¹ØÓÚPHP³Ìʽ°²È«µÄÌÖÂÛµÄÖ÷Ì⣬Äã¾ö²»ÄÜÏàÐÅÒ»¸öÀ´×ÔÍⲿµÄÊý¾Ý¡£²»¹ÜËüÀ´×ÔÓû§Ìá½»±íµ¥£¬ÎļþϵͳµÄÎļþ»òÕß»·¾³±äÁ¿£¬ÈκÎÊý¾Ý¶¼²»Äܼòµ¥µÄÏ뵱ȻµÄ²ÉÓá£ËùÒÔÓû§ÊäÈë±ØÐë½øÐÐÑéÖ¤²¢½«Ö®¸ñʽ»¯ÒÔ±£Ö¤°²È«¡£
´íÎó2¡£ÔÚwebĿ¼Öд洢Ãô¸ÐÊý¾Ý
ÈκκÍËùÓеÄÃô¸ÐÊý¾Ý¶¼Ó¦¸Ã´æ·ÅÔÚ¶ÀÁ¢ÓÚÐèҪʹÓÃÊý¾ÝµÄ³ÌʽµÄÎļþÖУ¬²¢±£´æÔÚÒ»¸ö²»ÄÜͨ¹ýä¯ÀÀÆ÷·ÃÎʵÄĿ¼Ï¡£µ±ÐèҪʹÓÃÃô¸ÐÊý¾Ýʱ£¬ÔÙͨ¹ýinclude »ò requireÓï¾äÀ´°üº¬µ½Êʵ±µÄPHP³ÌʽÖС£
´íÎó3¡£²»Ê¹ÓÃÍƼöµÄ°²È«·À·¶´ëÊ©
PHPÊÖ²á°üº¬ÁËÔÚʹÓúͱàдPHP³Ìʽʱ¹ØÓÚ°²È«·À·¶µÄÍêÕûÕ½ڡ£ÊÖ²áÒ²£¨¼¸ºõ£©»ùÓÚ°¸ÀýÇå³þµÄ˵Ã÷ÁËʲôʱºò´æÔÚDZÔÚ°²È«·çÏÕºÍÔõô½«·çÏÕ½µµÍµ½×îµÍ¡£ÓÖÈ磬¶ñÒâÓû§ÒÀ¿¿¿ª·¢Õߺ͹ÜÀíÔ±µÄʧÎóµÃµ½¹ØÐĵݲȫÐÅÏ¢ÒÔ»ñȡϵͳµÄȨÏÞ¡£ÁôÒâÕâЩ¾¯¸æ²¢Êʵ±µÄ²ÉÈ¡´ëÊ©À´¼õС¶ñÒâÓû§¸øÄãµÄϵͳ´øÀ´ÕæÕýµÄÆÆ»µµÄ¿ÉÄÜÐÔ¡£ (出处:viphot)
网络的神奇作用吸引着越来越多的用户加入其中,正因如此,网络的承受能力也面临着越来越严峻的考验―从硬件上、软件上、所用标准上......,各项技术都需要适时应势,对应发展,这正是网络迅速走向进步的催化剂。
……