ÔÚPHPÖÐÓÃheader("location:test.php")½øÐÐÌøתҪעÒaÒÔϼ¸µa£º
1¡¢locationºÍ¡°:¡±ºÅ¼a²»ÄÜÓпոñ£¬*ñÔo»a³o´i¡£
2¡¢ÔÚÓÃheaderÇ°²»ÄÜÓÐÈκεÄÊa³o£¬°uÀ¨includeµÄÒ³ÃaeÖбeÇ(C)¡°?>¡±ºo²»ÄÜÓпոñ£¡£¡
3¡¢headerºoµÄPHP´uÂe»¹»a±»Ö´ÐС£
Ðø£º
ÎÊÌa£ºheaderº¯ÊýÇ°ÊaÈeÄÚÈÝ
Ò»°aÀ´ËµÔÚheaderº¯ÊýÇ°²»ÄÜÊa³ohtmlÄÚÈÝ£¬ÀaËƵĻ¹ÓÐsetcookie() ºÍ session º¯Êý£¬ÕaÐ(C)º¯ÊýÐeÒªÔÚÊa³oÁ÷ÖÐÔo¼ÓÏuϢͲ¿ÐÅÏ¢¡£Èç¹uÔÚheader()Ö´ÐÐ֮ǰÓÐechoµÈÓi¾a£¬µ±ºoÃaeÓoµ½header()ʱ£¬¾Í»a±¨³o ¡°Warning: Cannot modify header information ¨C headers already sent by ¡.¡±´iÎo¡£¾ÍÊÇ˵ÔÚÕaÐ(C)º¯ÊýµÄÇ°Ãae²»ÄÜÓÐÈκÎÎÄ×Ö¡¢¿ÕÐС¢»Ø³µµÈ£¬¶øÇÒ×iºÃÔÚheader()º¯Êýºo¼ÓÉÏexit()º¯Êý¡£ÀýÈçÏÂÃaeµÄ´iÎoд¨£¬ÔÚ Á½¸ophp´uÂe¶ÎÖ®¼aÓÐÒ»¸o¿ÕÐУº
//ÕaÀiÓ¦¸ÃÊÇÒ»¸o¿ÕÐÐ
ÔÒo£º
PHP½Å±¾¿ªÊ¼Ö´ÐРʱ,Ëu¿ÉÒÔͬʱ¢ËÍhttpÏuϢͲ¿(±eÌa)ÐÅÏ¢ºÍÖ÷ÌaÐÅÏ¢. httpÏuϢͲ¿(À´×Ô header() »oSetCookie() º¯Êý)²¢²»»aÁ¢¼´¢ËÍ,Ïa´,Ëu±»±£´aeµ½Ò»¸oÁбiÖÐ. ÕaÑu¾Í¿ÉÒÔÔÊÐiÄaÐ޸ıeÌaÐÅÏ¢,°uÀ¨È±Ê¡µÄ±eÌa(ÀýÈçContent-Type ±eÌa£(C).µ«ÊÇ,Ò»µ(C)½Å±¾¢ËÍÁËÈκÎDZeÌaµÄÊa³o£¨ÀýÈç,ʹÓà HTML »o print()µ÷ÓÃ),ÄÇôPHP¾Í±ØÐeÏÈ¢ËÍÍeËuÓеÄHeader,È»ºoÖÕÖ¹ HTTPheader.¶øºo¼ÌÐø¢ËÍÖ÷ÌaÊý¾Ý.´ÓÕaʱ¿ªÊ¼,ÈκÎÌi¼Ó»oÐÞ¸ÄHeaderÐÅÏ¢µÄÊÔͼ¶¼ÊDz»ÔÊÐiµÄ,²¢»a¢ËÍÉÏÊoµÄ´iÎoÏuÏ¢Ö®Ò»¡£
½a¾o½¨£º
ÐÞ¸Äphp.ini´o¿ª»º´ae(output_buffering),½«output_buffering=0Ð޸ijÉoutput_buffering=4096
»o ÕßÔÚ³ÌÐoÖÐʹÓûº´aeº¯Êýob_start()£¬ob_end_flush() µÈ¡£ÔÀiÊÇ£ºoutput_buffering±»ÆoÓÃʱ,Ôڽű¾¢ËÍÊa³oʱ£¬PHP²¢²»¢ËÍHTTPheader¡£Ïa´£¬Ëu½«´ËÊa³oͨ¹ý¹ÜµÀ£¨pipe£(C) ÊaÈeµ½¶¯Ì¬Ôo¼ÓµÄ»º´aeÖУ¨Ö»ÄÜÔÚPHP4.0ÖÐʹÓã¬Ëu¾ßÓÐÖÐÑe»¯µÄÊa³o»uÖÆ£(C)¡£ÄaÈÔÈ»¿ÉÒÔÐÞ¸Ä/Ìi¼Óheader£¬»oÕßÉeÖÃcookie£¬ÒoΪ headerʵ¼ÊÉϲ¢Ã»ÓТËÍ¡£µ±È«²¿½Å±¾ÖÕֹʱ£¬PHP½«×Ô¶¯¢ËÍHTTP headerµ½a¯ÀÀÆ÷£¬È»ºoÔÙ¢ËÍÊa³o»º³aÖеÄÄÚÈÝ¡£
ÒÔÉÏÕaƪPHP HeaderʧЧµÄÔÒoÖÎo¼°½a¾o½¨¾ÍÊÇС±aÖÏi¸ø´o¼ÒµÄÈ«²¿ÄÚÈÝÁË£¬Ï£ÍuÄܸø´o¼ÒÒ»¸o²Î¿¼£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£