ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾PHP ±à³Ì£¾PHP5ÖÐÎÄÊÖ²á
PHP5ÖÐÎÄÊÖ²á

PHP ºÍ HTML

PHP ºÍ HTML ÓкܶàÏ໥×÷ÓãºPHP ÄÜÉú³É HTML£¬HTML ¿ÉÒÔÏò PHP ´«µÝÐÅÏ¢¡£ÔÚÔĶÁÕâЩ³£¼ûÎÊÌâ֮ǰ£¬ÏÈѧ»áÔõÑù´Ó PHP Ö®ÍâÈ¡µÃ±äÁ¿ºÜÖØÒª¡£´ËÖ÷ÌâµÄÊÖ²áÒ³Ò²°üÀ¨ºÜ¶àÀý×Ó¡£»¹Òª×ÐϸÁôÒâ register_globals ¶ÔÄãÒâζ×Åʲô¡£

  1. a½“aˆ‘e€še¿‡e!¡§a•i¼URL a¼ a€¼a—¶eœ€e|c”¡§a»€a1ˆc¼–c i¼e¡ì¡êc a–1a3•i¼Ÿ
  2. aˆ‘aœ¡§e¡¥•c”¡§ <input type="image"> a ‡eR¡ãi¼Œa½†a˜¡¥a2!aœ‰ $foo.x a’Œ $foo.y a˜e‡i¼ŒaRƒa»¬a“aaŽ»ao†i¼Ÿ
  3. a€Ža ¡¤aœ¡§ HTML cš„ <form> a£¬-a»oc«‹a•¡ãc»„i¼Ÿ
  4. a€Ža ¡¤a»Ža¡¥a¡èše€‰cš„ HTML cš„ select multiple a ‡eR¡ãa£¬-a¾—aˆ¡ãa‰€aœ‰c»“ažœi¼Ÿ
  5. a€Ža ¡¤a»Ž Javascript a¼ e€’a£¬€a£¬aa˜e‡aˆ¡ã PHPi¼Ÿ
  6. µ±ÎÒͨ¹ý±íµ¥£¯URL ´«ÖµÊ±ÐèÒªÓÃʲô±àÂ룯½âÂë·½·¨£¿

    ÔÚ¼¸¸ö»·½ÚÉϱàÂ뷽ʽºÜÖØÒª¡£¼Ù¶¨ÓÐ string $data£¬ÆäÖаüº¬ÁËÏëͨ¹ý·Ç±àÂ뷽ʽ´«µÝµÄ×Ö·û´®£¬ÄÇÕâÊÇÏà¹Ø²½Ö裺

    • HTML ½âÎö¡£ÒªÖ¸¶¨Ò»¸öÈÎÒâµÄ×Ö·û´®£¬±ØÐ뽫Æä·ÅÔÚË«ÒýºÅÖУ¬²¢Óà htmlspecialchars() ´¦ÀíÕû¸öÖµ¡£

    • URL£ºURL Óɼ¸²¿·Ö×é³É¡£Èç¹ûÏ£Íû×Ô¼ºµÄÊý¾Ý±»µ±×÷ÆäÖÐÒ»ÏîÀ´½âÊÍ£¬±ØÐëÓà urlencode() ¶ÔÆä±àÂë¡£

    Example#1 Òþ²ØµÄ HTML ±íµ¥µ¥Ôª

    <?php
        
    echo "<input type='hidden' value='" htmlspecialchars($data) . "' />\n";
    ?>

    Note: Óà urlencode() À´´¦Àí $data ÊÇ´íÎóµÄ£¬ÒòΪÊÇä¯ÀÀÆ÷µÄÔðÈÎÀ´ urlencode() Êý¾Ý¡£ËùÓÐÁ÷ÐеÄä¯ÀÀÆ÷¶¼ÄÜÕýÈ·´¦Àí¡£×¢Òâ²»ÂÛºÎÖÖ·½·¨£¨ÀýÈç GET »ò POST£©¶¼»áÕâÑù¡£²»¹ýÖ»»áÔÚÓà GET ÇëÇóʱעÒâµ½ÕâÒ»µã£¬ÒòΪ POST ÇëÇóͨ³£ÊÇÒþ²ØµÄ¡£

    Example#2 µÈ´ýÓû§±à¼­µÄÊý¾Ý

    <?php
        
    echo "<textarea name='mydata'>\n";
        echo 
    htmlspecialchars($data)."\n";
        echo 
    "</textarea>";
    ?>

    Note: Êý¾Ý»á°´ÕÕÔ¤ÆÚµÄÏÔʾÔÚä¯ÀÀÆ÷ÖУ¬ÒòΪä¯ÀÀÆ÷»á½âÊÍ HTML תÒå·ûºÅ¡£ µ±Ìύʱ£¬²»ÂÛÊÇ GET »òÕß POST ·½·¨£¬Êý¾Ý¶¼»á±»ä¯ÀÀÆ÷½øÐÐ urlencode À´´«Ê䣬²¢Ö±½Ó±» PHP urldecode¡£ËùÒÔ×îÖÕ²»ÐèÒª×Ô¼º´¦ÀíÈκΠurlencoding/urldecoding£¬È«¶¼ÊÇ×Ô¶¯´¦ÀíµÄ¡£

    Example#3 URL ÖеÄÀý×Ó

    <?php
        
    echo "<a href='" htmlspecialchars("/nextpage.php?stage=23&data=" .
            
    urlencode($data)) . "'>\n";
    ?>

    Note: ÊÂʵÉÏÕâÔÚ±àÔìÒ»¸ö HTML µÄ GET ÇëÇó£¬Òò´ËÐèÒªÊÖ¹¤¶ÔÊý¾Ý½øÐÐ urlencode()¡£

    Note: ÐèÒª¶ÔÕû¸ö URL ½øÐÐ htmlspecialchars()£¬ÒòΪ URL ÊÇ×÷Ϊ HTML ÊôÐÔµÄÒ»¸öÖµ³öÏֵġ£ÔÚ±¾ÀýÖУ¬ä¯ÀÀÆ÷»áÊ×ÏȶÔÖµ½øÐÐ un-htmlspecialchars()£¬È»ºóÔÙ´«µÝ´Ë URL¡£PHP ½«ÄÜÕýÈ·Àí½â URL£¬ÒòΪ¶ÔÊý¾Ý½øÐÐÁË urlencoded()¡£ ҪעÒâµ½ URL ÖÐµÄ & ±»Ìæ»»³ÉÁË &amp;¡£Èç¹ûÍüÁËÕâÒ»²½£¬¾¡¹Ü´ó¶àÊýä¯ÀÀÆ÷¶¼Äָܻ´£¬µ«Ò²²»×ÜÊÇÕâÑù¡£Òò´Ë¼´Ê¹ URL ²»ÊǶ¯Ì¬µÄ£¬Ò²ÐèÒª¶Ô URL ½øÐÐ htmlspecialchars()¡£

    ÎÒÔÚÊÔÓà <input type="image"> ±ê¼Ç£¬µ«ÊÇûÓÐ $foo.x ºÍ $foo.y ±äÁ¿£¬ËüÃÇÄÄÈ¥ÁË£¿

    µ±Ìá½»±íµ¥Ê±£¬¿ÉÒÔÓÃͼƬ´úÌæ±ê×¼µÄÌá½»°´Å¥£¬ÓÃÀàËÆÕâÑùµÄ±ê¼Ç£º

    <input type="image" src="image.gif" name="foo" />
    
    µ±Óû§µã»÷ÁËͼƬµÄÈκβ¿·Ö£¬¸Ã±íµ¥»á±»·¢Ë͵½·þÎñÆ÷²¢¼ÓÉÏÁ½¸ö¶îÍâµÄ±äÁ¿£ºfoo.x ºÍ foo.y¡£

    ÒòΪ foo.x ºÍ foo.y ÔÚ PHP Öлá³ÉΪ·Ç·¨µÄ±äÁ¿Ãû£¬ËüÃDZ»×Ô¶¯×ª»»³ÉÁË foo_x ºÍ foo_y¡£Ò²¾ÍÊÇÓÃÏ»®Ïß´úÌæÁ˵㡣Òò´Ë£¬¿ÉÒÔ°´ÕÕÔÚÀ´×Ô PHP Ö®ÍâµÄ±äÁ¿ÕâÒ»½ÚÖÐ˵Ã÷µÄÄÇÑù·ÃÎÊÕâЩ±äÁ¿¡£ÀýÈ磬$_GET['foo_x']¡£

    Note: ÇëÇó±äÁ¿ÃûÖеĿոñ±»×ª»»ÎªÏ»®Ïß¡£

    ÔõÑùÔÚ HTML µÄ <form> Öн¨Á¢Êý×飿

    ҪʹÄãµÄ <form> ½á¹û±»µ±³É array ·¢Ë͵½ PHP ½Å±¾£¬Òª¶Ô <input>£¬<select> »òÕß <textarea> µ¥ÔªÕâÑùÃüÃû£º

    <input name="MyArray[]" />
    <input name="MyArray[]" />
    <input name="MyArray[]" />
    <input name="MyArray[]" />
    
    ×¢Òâ±äÁ¿ÃûºóµÄ·½À¨ºÅ£¬ÕâʹÆä³ÉΪһ¸öÊý×é¡£¿ÉÒÔͨ¹ý¸ø²»Í¬µÄµ¥Ôª·ÖÅäÏàͬµÄÃû×ÖÀ´°Ñµ¥Ôª·Ö×éµ½²»Í¬µÄÊý×éÀ
    <input name="MyArray[]" />
    <input name="MyArray[]" />
    <input name="MyOtherArray[]" />
    <input name="MyOtherArray[]" />
    
    Õ⽫²úÉúÁ½¸öÊý×飬MyArray ºÍ MyOtherArray£¬²¢·¢Ë͸ø PHP ½Å±¾¡£»¹¿ÉÒÔ¸øÊý×é·ÖÅäÖ¸¶¨µÄ¼üÃû£º
    <input name="AnotherArray[]" />
    <input name="AnotherArray[]" />
    <input name="AnotherArray[email]" />
    <input name="AnotherArray[phone]" />
    
    AnotherArray Êý×齫°üº¬¼üÃû 0£¬1£¬email ºÍ phone¡£

    Note: Ö¸¶¨Êý×éµÄ¼üÃûÊÇ HTML µÄ¿ÉÑ¡Ïî¡£Èç¹û²»Ö¸¶¨¼üÃû£¬ÔòÊý×é±»°´ÕÕµ¥ÔªÔÚ±íµ¥ÖгöÏÖµÄ˳ÐòÌî³ä¡£µÚÒ»¸öÀý×Ó½«°üº¬¼üÃû 0£¬1£¬2 ºÍ 3¡£

    ²Î¼ûÊý×麯ÊýºÍÀ´×Ô PHP Ö®ÍâµÄ±äÁ¿¡£

    ÔõÑù´Ó¿É¶àÑ¡µÄ HTML µÄ select multiple ±ê¼ÇÖеõ½ËùÓнá¹û£¿

    ¿É¶àÑ¡µÄ select multiple ±ê¼ÇÊÇ HTML µÄÒ»¸ö¹¹Ô죬ÔÊÐíÓû§´ÓÒ»¸öÁбíÖÐÑ¡Ôñ¶à¸öÏîÄ¿¡£ÕâЩÏîÄ¿½Ó×ű»´«µÝ¸ø¸Ã±íµ¥ action ÖÐÖ¸¶¨µÄ´¦Àí³ÌÐò¡£ÎÊÌâÊÇËüÃǶ¼»á±»ÓÃͬÑùµÄÃû×Ö´«µÝ¡£ÀýÈ磺

    <select name="var" multiple="yes">
    
    ÿ¸ö±»Ñ¡ÏÕâÑù±»´«µÝµ½±íµ¥´¦Àí³ÌÐò£º
    var=option1
    var=option2
    var=option3
          
    ÿ¸öÑ¡Ï¸²¸ÇÇ°ÃæÒ»¸ö $var ±äÁ¿µÄÄÚÈÝ¡£½â¾ö·½°¸ÊÇÓà PHP µÄ¡°±íµ¥µ¥ÔªÊý×顱ÌØÐÔ¡£Ê¹Ó÷½·¨ÈçÏ£º
    <select name="var[]" multiple="yes">
    
    Õ⽫¸æËß PHP ½« $var µ±³ÉÊý×é¶Ô´ý£¬Ã¿¸ö¶Ô var[] µÄ¸³Öµ¶¼»á¸øÊý×éÔö¼ÓÒ»Ïî¡£µÚÒ»Ï³ÉΪ $var[0]£¬ÏÂÒ»¸öÊÇ $var[1]£¬µÈµÈ¡£¿ÉÒÔÓà count() º¯ÊýÀ´²â¶¨Ñ¡ÔñÁ˶àÉÙ¸öÏîÄ¿£¬±ØҪʱ¿ÉÒÔÓà sort() º¯ÊýÀ´¶ÔÑ¡ÏîµÄÊý×é½øÐÐÅÅÐò¡£

    ×¢ÒâÈç¹ûÔÚ JavaScript ÖÐͨ¹ýÃû×ÖÀ´ÒýÓõ¥Ôª£¬µ¥ÔªÃû×ÖÖÐµÄ [] ¿ÉÄÜ»áÔì³ÉÎÊÌâ¡£ÓÃ±íµ¥µ¥ÔªÖеÄÊý×ÖÐòºÅÀ´Ìæ´ú£¬»òÕß½«±äÁ¿ÃûÓõ¥ÒýºÅÀ¨ÆðÀ´²¢ÓÃÆä×÷Ϊµ¥ÔªÊý×éµÄË÷Òý£¬ÀýÈ磺

    variable = documents.forms[0].elements['var[]'];
          

    ÔõÑù´Ó Javascript ´«µÝÒ»¸ö±äÁ¿µ½ PHP£¿

    ÓÉÓÚ Javascript £¨Í¨³£Çé¿öÏ£©ÊÇ¿Í»§¶Ë¼¼Êõ£¬¶ø PHP £¨Í¨³£Çé¿öÏ£©ÊÇ·þÎñÆ÷¶Ë¼¼Êõ£¬¶øÇÒ HTTP ÊÇÒ»ÖÖ¡°ÎÞ״̬¡±Ð­Ò飬Òò´ËÁ½ÖÖÓïÑÔÖ®¼ä²»ÄÜÖ±½Ó¹²Ïí±äÁ¿¡£

    µ«ÊÇ£¬ÓпÉÄÜÔÚ¶þÕßÖ®¼ä´«µÝ±äÁ¿¡£Ò»ÖÖʵÏֵķ½·¨ÊÇÓà PHP Éú³É Javascript ´úÂ룬²¢ÈÃä¯ÀÀÆ÷×Ô¶¯Ë¢Ð£¬½«Ìض¨µÄ±äÁ¿´«µÝ»Ø PHP ½Å±¾¡£ÒÔÏÂÀý×ÓÏÔʾÁËÈçºÎÕâÑù×ö©¤©¤Èà PHP ´úÂëÈ¡µÃÏÔʾÆÁÄ»µÄ¸ß¶ÈºÍ¿í¶È£¬Í¨³£Ö»ÄÜÔÚ¿Í»§¶ËÕâô×ö¡£

    <?php
    if (isset($_GET['width']) AND isset($_GET['height'])) {
      
    // output the geometry variables
      
    echo "Screen width is: "$_GET['width'] ."<br />\n";
      echo 
    "Screen height is: "$_GET['height'] ."<br />\n";
    } else {
      
    // pass the geometry variables
      // (preserve the original query string
      //   -- post variables will need to handled differently)

      
    echo "<script language='javascript'>\n";
      echo 
    "  location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
                
    "&width=\" + screen.width + \"&height=\" + screen.height;\n";
      echo 
    "</script>\n";
      exit();
    }
    ?>